init repo with utils file from cms project
This commit is contained in:
commit
a9c4cca71e
4 changed files with 68 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
.clj-kondo
|
||||
.lsp
|
||||
17
README.md
Normal file
17
README.md
Normal file
|
|
@ -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"}}}
|
||||
```
|
||||
2
bb.edn
Normal file
2
bb.edn
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{:paths ["src"]
|
||||
:deps {}}
|
||||
47
src/gum_utils/core.clj
Normal file
47
src/gum_utils/core.clj
Normal file
|
|
@ -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)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue