A peer-to-peer stack for code collaboration
{:module 'prelude/io-utils
:doc "IO-related utilities"
:exports '[fzf-select! edit-in-editor! get-git-config! set-git-config!
get-git-commit-data! get-git-username! process-git-with-exit!
base-path!
]}
(import prelude/io '[shell-with-stdout! write-file! process-with-stdout!] :unqualified)
(import prelude/strings '[unlines] :unqualified)
(def fzf-select!
"Select one of many strings with `fzf`.
Requires that `fzf` be on the path.
Returns `[:just x]` where `x` is the selected string, or `:nothing` if nothing
was selected."
(fn [xs]
(def result (process-with-stdout! "fzf" [] (string-append (unlines xs) "\n")))
(match result
[] :nothing
['x] [:just (string-replace "\n" "" x)])))
(def fzf-select-with-preview!
"Like `fzf-select!`, but includes a preview defined by `prev-fn`."
(fn [xs prev-fn]
(def temp-file (first (process-with-stdout! "mktemp" [] "")))
(def d (dict-from-seq (map (fn [x] [x (prev-fn x)]) xs)))
(def contents
(string-append
"(def the-dict " (show d) ")"
"(put-str! (lookup (first (get-args!)) the-dict))"))
(write-file! temp-file contents)
(def preview ["--preview" (string-append "radicle " temp-file " {}")])
(def result (process-with-stdout! "fzf" preview (unlines xs)))
(match result
[] :nothing
['x] [:just (string-replace "\n" "" x)])))
(def edit-in-editor!
"Open `$EDITOR` on a file prepopulated with `orig`. Returns the contents of the edited file when the editor exits."
(fn [orig]
(def file (unlines (shell-with-stdout! "mktemp" "")))
(write-file! file orig)
(shell-no-stdin! (string-append "${EDITOR:-nano} " file) "")
(read-file! file)))
(def get-git-config!
"Get the value associated with a key in git config."
(fn [key]
(unlines (process-with-stdout! "git" ["config" "--get" key] ""))))
(def get-git-username!
"Get the user name stored in git config."
(fn []
(match (get-git-config! "user.name")
"" (do
(put-str! (string-append
"git config user.name is not set. "
"Set the user name and rerun your command."))
(exit! 1))
'name name)))
(def get-git-commit-data!
"Get data from a `commit` via `show` specified by `format`"
(fn [format commit]
(def git-args ["show" "-s" (string-append "--format=" format) commit])
(match (process-with-stdout-stderr-exitcode! "git" git-args "")
['stdout _ :ok] (unlines stdout)
_ (do
(put-str!
(string-append "git show -s --format=" format " " commit " failed."))
(exit! 1)))))
(def set-git-config!
"Set the value associated with a key in git config."
(fn [key value]
(process-with-stdout! "git" ["config" key value] "")))
(def process-git-with-exit!
"Processes a git command `args`. If it fails, the message `msg` is shown and the process exits, otherwise `:ok` is passed."
(fn [args msg]
(match (process! "git" args "")
:ok :ok
_ (do
(put-str! msg)
(exit! 1)))))
(def base-path!
"Returns the base path for storage of radicle related config files. By default
this is `$HOME/.config/radicle`. This can be adjusted by setting
`$XDG_CONFIG_HOME`."
(fn []
(def xgd-config-home (unlines (shell-with-stdout! "echo $XDG_CONFIG_HOME" "")))
(def home (unlines (shell-with-stdout! "echo $HOME" "")))
(def config-path
(if (eq? xgd-config-home "")
(string-append home "/.config")
xgd-config-home))
(string-append config-path "/radicle")))