add prefill values to edit command and view after editing

This commit is contained in:
Travis Shears 2025-06-16 11:34:10 +02:00
parent 6bf4dbe8c0
commit ae2ef617b0
3 changed files with 43 additions and 23 deletions

View file

@ -22,8 +22,12 @@
(defn print-markdown [text] (defn print-markdown [text]
(shell (format "gum format \"%s\"" text))) (shell (format "gum format \"%s\"" text)))
(defn prompt-for [placeholder] (defn prompt-for [placeholder & {:keys [prefill]}]
(str/trim (-> (shell {:out :string} (format "gum input --placeholder='%s'" placeholder)) :out))) (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 (defn prompt-for-many
([placeholder] ([placeholder]
@ -39,5 +43,7 @@
carry carry
(prompt-for-many placeholder (conj carry val)))))) (prompt-for-many placeholder (conj carry val))))))
(defn prompt-for-long-form [placeholder] (defn prompt-for-long-form [placeholder & {:keys [prefill]}]
(-> (shell {:out :string} (format "gum write --placeholder='%s'" placeholder)) :out)) (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)))

View file

@ -3,6 +3,7 @@
[babashka.http-client :as http] [babashka.http-client :as http]
[clojure.pprint :refer [pprint]] [clojure.pprint :refer [pprint]]
[cli-cms.config :refer [config]] [cli-cms.config :refer [config]]
[cli-cms.view :as view]
[cheshire.core :as json] [cheshire.core :as json]
[cli-cms.cli-utils :as utils])) [cli-cms.cli-utils :as utils]))
@ -10,15 +11,15 @@
(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 fetch-snippet [id]
;; (let [res (http/get (str (:backend-host config) "/api/snippet") {:query-params {"id" id}})]
;; (json/parse-string (:body res) true)))
(defn prompt-for-edit-type [] (defn prompt-for-edit-type []
(utils/select '({:value "title" :item :title} (utils/select (map #(hash-map :value (name %) :item %)'(
{:value "markdown" :item :markdown} :title
{:value "slug" :item :slug} :markdown
{:value "tags" :item :tags} :slug
{:value "done" :item :done}))) :remove-tag
:add-tags
: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")
@ -31,17 +32,23 @@
(edit snippet {})) (edit snippet {}))
([snippet changes] ([snippet changes]
(case (prompt-for-edit-type) (case (prompt-for-edit-type)
:title (let [new-title (utils/prompt-for "title")] :title (let [new-title (utils/prompt-for "title" :prefill (:title snippet))]
(edit (assoc snippet :title new-title) (assoc changes :title new-title))) (edit (assoc snippet :title new-title) (assoc changes :title new-title)))
:slug (let [new-slug (utils/prompt-for "slug")] :slug (let [new-slug (utils/prompt-for "slug" :prefill (:slug snippet))]
(edit (assoc snippet :slug new-slug) (assoc changes :slug new-slug))) (edit (assoc snippet :slug new-slug) (assoc changes :slug new-slug)))
:tags (let [new-tags (utils/prompt-for-many "tags")] :remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
(edit (assoc snippet :tags new-tags) (assoc changes :tags new-tags))) tags (remove #(= % tag-to-remove) (:tags snippet))]
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown")] (edit (assoc snippet :tags tags) (assoc changes :tags tags)))
:add-tags (let [
new-tags (utils/prompt-for-many "tags")
tags (concat new-tags (:tags snippet))]
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
: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)))
(defn run [] (defn run []
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] (let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
(send-patch (:id snippet-to-edit) (edit snippet-to-edit)) (send-patch (:id snippet-to-edit) (edit snippet-to-edit))
(println "Snippet updated successfully"))) (println "Snippet updated successfully")
(view/view (view/fetch-snippet (:id snippet-to-edit)))))

View file

@ -11,10 +11,17 @@
(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 fetch-snippet [id]
(let [res (http/get (str (:backend-host config) "/api/snippet") {:query-params {"id" id}})]
(json/parse-string (:body res) true)))
(defn view [snippet]
(utils/color-print (str "title: " (:title snippet)) "4")
(utils/color-print (str "slug: " (:slug snippet)) "4")
(utils/color-print (str "tags: " (str/join ", " (:tags snippet))) "4")
(utils/color-print "markdown: " "4")
(utils/print-markdown (:markdown snippet)))
(defn run [] (defn run []
(let [snippet-to-view (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))] (let [snippet-to-view (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
(utils/color-print (str "title: " (:title snippet-to-view)) "4") (view (fetch-snippet (:id snippet-to-view)))))
(utils/color-print (str "slug: " (:slug snippet-to-view)) "4")
(utils/color-print (str "tags: " (str/join ", " (:tags snippet-to-view))) "4")
(utils/color-print "markdown: " "4")
(utils/print-markdown (:markdown snippet-to-view))))