switch to external gum util lib
This commit is contained in:
parent
b95271aa0f
commit
3d9bf15cfc
7 changed files with 16 additions and 67 deletions
3
bb.edn
3
bb.edn
|
|
@ -1,2 +1,3 @@
|
||||||
{:paths ["src"]
|
{:paths ["src"]
|
||||||
:deps {}}
|
:deps {com.travisshears/gum-utils {:git/url "https://git.travisshears.com/travisshears/gum-utils"
|
||||||
|
:git/sha "748b21d358b62db0476bc3577cb5398acc533ba1"}}}
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
(ns cli-cms.cli-utils
|
|
||||||
(:require
|
|
||||||
[clojure.string :as str]
|
|
||||||
[clojure.pprint :refer [pprint]]
|
|
||||||
[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)))
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
(ns cli-cms.create
|
(ns cli-cms.create
|
||||||
(:require
|
(:require
|
||||||
[cli-cms.cli-utils :as utils]
|
[com.travisshears.gum-utils :as utils]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[cli-cms.config :refer [config]]
|
[cli-cms.config :refer [config]]
|
||||||
[babashka.http-client :as http]
|
[babashka.http-client :as http]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
(ns cli-cms.delete
|
(ns cli-cms.delete
|
||||||
(:require
|
(:require
|
||||||
[cli-cms.cli-utils :as utils]
|
[com.travisshears.gum-utils :as utils]
|
||||||
[cli-cms.config :refer [config]]
|
[cli-cms.config :refer [config]]
|
||||||
[clojure.pprint :refer [pprint]]
|
[clojure.pprint :refer [pprint]]
|
||||||
[babashka.http-client :as http]
|
[babashka.http-client :as http]
|
||||||
|
|
|
||||||
|
|
@ -5,21 +5,19 @@
|
||||||
[cli-cms.config :refer [config]]
|
[cli-cms.config :refer [config]]
|
||||||
[cli-cms.view :as view]
|
[cli-cms.view :as view]
|
||||||
[cheshire.core :as json]
|
[cheshire.core :as json]
|
||||||
[cli-cms.cli-utils :as utils]))
|
[com.travisshears.gum-utils :as utils]))
|
||||||
|
|
||||||
(defn fetch-snippets []
|
(defn fetch-snippets []
|
||||||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
||||||
(json/parse-string (:body res) true)))
|
(json/parse-string (:body res) true)))
|
||||||
|
|
||||||
(defn prompt-for-edit-type []
|
(defn prompt-for-edit-type []
|
||||||
(utils/select (map #(hash-map :value (name %) :item %)'(
|
(utils/select (map #(hash-map :value (name %) :item %) '(:title
|
||||||
:title
|
:markdown
|
||||||
:markdown
|
:slug
|
||||||
:slug
|
:remove-tag
|
||||||
:remove-tag
|
:add-tags
|
||||||
:add-tags
|
:done))))
|
||||||
:done
|
|
||||||
))))
|
|
||||||
|
|
||||||
(defn send-patch [id patch]
|
(defn send-patch [id patch]
|
||||||
(http/patch (str (:backend-host config) "/api/snippet")
|
(http/patch (str (:backend-host config) "/api/snippet")
|
||||||
|
|
@ -39,10 +37,9 @@
|
||||||
:remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
|
:remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
|
||||||
tags (remove #(= % tag-to-remove) (:tags snippet))]
|
tags (remove #(= % tag-to-remove) (:tags snippet))]
|
||||||
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
||||||
:add-tags (let [
|
:add-tags (let [new-tags (utils/prompt-for-many "tags")
|
||||||
new-tags (utils/prompt-for-many "tags")
|
tags (concat new-tags (:tags snippet))]
|
||||||
tags (concat new-tags (:tags snippet))]
|
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
||||||
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
|
||||||
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown" :prefill (:markdown snippet))]
|
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown" :prefill (:markdown snippet))]
|
||||||
(edit (assoc snippet :markdown new-markdown) (assoc changes :markdown new-markdown)))
|
(edit (assoc snippet :markdown new-markdown) (assoc changes :markdown new-markdown)))
|
||||||
:done changes)))
|
:done changes)))
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
[cli-cms.create :as create]
|
[cli-cms.create :as create]
|
||||||
[cli-cms.edit :as edit]
|
[cli-cms.edit :as edit]
|
||||||
[cli-cms.delete :as delete]
|
[cli-cms.delete :as delete]
|
||||||
[cli-cms.cli-utils :as utils]
|
[com.travisshears.gum-utils :as utils]
|
||||||
[cli-cms.view :as view]))
|
[cli-cms.view :as view]))
|
||||||
|
|
||||||
(defn color-test []
|
(defn color-test []
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
[cli-cms.config :refer [config]]
|
[cli-cms.config :refer [config]]
|
||||||
[cheshire.core :as json]
|
[cheshire.core :as json]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[cli-cms.cli-utils :as utils]))
|
[com.travisshears.gum-utils :as utils]))
|
||||||
|
|
||||||
(defn fetch-snippets []
|
(defn fetch-snippets []
|
||||||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue