add prefill values to edit command and view after editing
This commit is contained in:
parent
6bf4dbe8c0
commit
ae2ef617b0
3 changed files with 43 additions and 23 deletions
|
|
@ -22,8 +22,12 @@
|
|||
(defn print-markdown [text]
|
||||
(shell (format "gum format \"%s\"" text)))
|
||||
|
||||
(defn prompt-for [placeholder]
|
||||
(str/trim (-> (shell {:out :string} (format "gum input --placeholder='%s'" placeholder)) :out)))
|
||||
(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]
|
||||
|
|
@ -39,5 +43,7 @@
|
|||
carry
|
||||
(prompt-for-many placeholder (conj carry val))))))
|
||||
|
||||
(defn prompt-for-long-form [placeholder]
|
||||
(-> (shell {:out :string} (format "gum write --placeholder='%s'" placeholder)) :out))
|
||||
(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)))
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
[babashka.http-client :as http]
|
||||
[clojure.pprint :refer [pprint]]
|
||||
[cli-cms.config :refer [config]]
|
||||
[cli-cms.view :as view]
|
||||
[cheshire.core :as json]
|
||||
[cli-cms.cli-utils :as utils]))
|
||||
|
||||
|
|
@ -10,15 +11,15 @@
|
|||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
||||
(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 []
|
||||
(utils/select '({:value "title" :item :title}
|
||||
{:value "markdown" :item :markdown}
|
||||
{:value "slug" :item :slug}
|
||||
{:value "tags" :item :tags}
|
||||
{:value "done" :item :done})))
|
||||
(utils/select (map #(hash-map :value (name %) :item %)'(
|
||||
:title
|
||||
:markdown
|
||||
:slug
|
||||
:remove-tag
|
||||
:add-tags
|
||||
:done
|
||||
))))
|
||||
|
||||
(defn send-patch [id patch]
|
||||
(http/patch (str (:backend-host config) "/api/snippet")
|
||||
|
|
@ -31,17 +32,23 @@
|
|||
(edit snippet {}))
|
||||
([snippet changes]
|
||||
(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)))
|
||||
: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)))
|
||||
:tags (let [new-tags (utils/prompt-for-many "tags")]
|
||||
(edit (assoc snippet :tags new-tags) (assoc changes :tags new-tags)))
|
||||
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown")]
|
||||
:remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
|
||||
tags (remove #(= % tag-to-remove) (:tags snippet))]
|
||||
(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)))
|
||||
:done changes)))
|
||||
|
||||
(defn run []
|
||||
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
||||
(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)))))
|
||||
|
|
|
|||
|
|
@ -11,10 +11,17 @@
|
|||
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
||||
(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 []
|
||||
(let [snippet-to-view (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
||||
(utils/color-print (str "title: " (:title snippet-to-view)) "4")
|
||||
(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))))
|
||||
(view (fetch-snippet (:id snippet-to-view)))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue