commit a9c4cca71ea6350ba4cf1f47c2c4880237adfc33 Author: Travis Shears Date: Fri Jul 4 14:32:47 2025 +0200 init repo with utils file from cms project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b5c564 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.clj-kondo +.lsp diff --git a/README.md b/README.md new file mode 100644 index 0000000..5dbc496 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Gum Utils + +A Clojure library providing convenient wrapper functions for the [Gum](https://github.com/charmbracelet/gum) CLI tool, designed for use with Babashka. + +## Prerequisites + +- [Babashka](https://github.com/babashka/babashka) installed +- [Gum](https://github.com/charmbracelet/gum) installed + +## Installation + +Add this to your `deps.edn`: + +```clojure +{:deps {io.github.yourusername/gum-utils {:git/url "https://github.com/yourusername/gum-utils" + :sha "latest-commit-sha"}}} +``` diff --git a/bb.edn b/bb.edn new file mode 100644 index 0000000..4439192 --- /dev/null +++ b/bb.edn @@ -0,0 +1,2 @@ +{:paths ["src"] + :deps {}} diff --git a/src/gum_utils/core.clj b/src/gum_utils/core.clj new file mode 100644 index 0000000..51a6ad7 --- /dev/null +++ b/src/gum_utils/core.clj @@ -0,0 +1,47 @@ +(ns gum-utils.core + (:require + [clojure.string :as str] + [babashka.process :refer [shell]])) + +(defn select + "Select from a list of options using gum filter. + Args: + l list of {:item any :value string}" + [l] + (let [values (map :value l) + selected-value (str/trim (:out (apply shell {:out :string} (concat '("gum" "filter") values)))) + selected-item (:item (first (filter #(= (:value %) selected-value) l)))] + selected-item)) + +(defn color-print + ([text] (color-print text "5")) + ([text color] + (shell (format "gum style --foreground %s \"%s\"" color text)))) + +(defn print-markdown [text] + (shell (format "gum format \"%s\"" text))) + +(defn prompt-for [placeholder & {:keys [prefill]}] + (let [cmd (if prefill + (format "gum input --placeholder='%s' --value='%s'" placeholder prefill) + (format "gum input --placeholder='%s'" placeholder))] + (str/trim (-> (shell {:out :string} cmd) :out)))) + +(defn prompt-for-many + ([placeholder] + (let [val (prompt-for (str placeholder ", enter done to finish"))] + (println val) + (if (= val "done") + nil + (prompt-for-many placeholder `(~val))))) + ([placeholder carry] + (let [val (prompt-for (str placeholder ", enter done to finish"))] + (println val) + (if (= val "done") + carry + (prompt-for-many placeholder (conj carry val)))))) + +(defn prompt-for-long-form [placeholder & {:keys [prefill]}] + (if prefill + (-> (shell {:out :string :extra-env {"GUM_WRITE_VALUE" prefill}} (format "gum write --placeholder='%s'" placeholder)) :out) + (-> (shell {:out :string} (format "gum write --placeholder='%s'" placeholder)) :out)))