From 589e48304f66a828ea1991b857e957491ebfca98 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Wed, 11 Jun 2025 09:42:42 +0200 Subject: [PATCH] add view command and color test --- src/cli_cms/cli_utils.clj | 9 +++++++-- src/cli_cms/main.clj | 10 +++++++++- src/cli_cms/view.clj | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/cli_cms/view.clj diff --git a/src/cli_cms/cli_utils.clj b/src/cli_cms/cli_utils.clj index f228159..29bf3b4 100644 --- a/src/cli_cms/cli_utils.clj +++ b/src/cli_cms/cli_utils.clj @@ -14,8 +14,13 @@ selected-item (:item (first (filter #(= (:value %) selected-value) l)))] selected-item)) -;; (defn print [text] -;; (shell (format "gum style --foreground 212 %s" text))) +(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] (str/trim (-> (shell {:out :string} (format "gum input --placeholder='%s'" placeholder)) :out))) diff --git a/src/cli_cms/main.clj b/src/cli_cms/main.clj index d5f24fd..8176820 100644 --- a/src/cli_cms/main.clj +++ b/src/cli_cms/main.clj @@ -2,11 +2,19 @@ (:require [cli-cms.create :as create] [cli-cms.edit :as edit] - [cli-cms.delete :as delete])) + [cli-cms.delete :as delete] + [cli-cms.cli-utils :as utils] + [cli-cms.view :as view])) + +(defn color-test [] + (doseq [color (range 0 255)] + (utils/color-print (str "COLOR --- " color) (str color)))) (defn -main [& args] (case (first args) "create" (create/run) "delete" (delete/run) "edit" (edit/run) + "view" (view/run) + "color-test" (color-test) (println "Missing command. Try create, edit, or delete."))) diff --git a/src/cli_cms/view.clj b/src/cli_cms/view.clj new file mode 100644 index 0000000..fbf9263 --- /dev/null +++ b/src/cli_cms/view.clj @@ -0,0 +1,20 @@ +(ns cli-cms.view + (:require + [babashka.http-client :as http] + [clojure.pprint :refer [pprint]] + [cli-cms.config :refer [config]] + [cheshire.core :as json] + [clojure.string :as str] + [cli-cms.cli-utils :as utils])) + +(defn fetch-snippets [] + (let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))] + (json/parse-string (:body res) true))) + +(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))))