From 7ac88d233143ebb186c4f4e448483abab7bd38e7 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 26 Aug 2019 10:39:11 +0200 Subject: [PATCH] overview and rm --- main.janet | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/main.janet b/main.janet index 6f012f4..b05bf32 100644 --- a/main.janet +++ b/main.janet @@ -82,7 +82,7 @@ (file/close f))) (defn request-val [label] - (print label) + (print label ":") (readline)) (defn new-id [] @@ -98,12 +98,66 @@ :title (request-val "Title")}) save-entries)) +(defn col-length [entries col] + (or 0 + (max ;(map (fn [entry] + (length (entry col))) + entries)))) + +(def vt100-codes + {:underlined "\e[4m" + :lightgray "\e[100m" + :bold "\e[1m" + :clear "\e[0m"}) + +(defn overview [] + (let [entries (load-entries) + len-id (max (col-length entries :id) + (length "Id")) + len-title (max (col-length entries :title) + (length "Title")) + spaces (fn [l m] (string/repeat " " (max 0 (- m l))))] + (print (vt100-codes :underlined) + "Id" (spaces 2 len-id) + " " + "Title" (spaces 5 len-title) + (vt100-codes :clear)) + (var ix 0) + (loop [entry :in entries + :let [id (entry :id) + title (entry :title)]] + (print (if (odd? ix) + (vt100-codes :lightgray) + (vt100-codes :bold)) + id (spaces (length id) len-id) + " " + title (spaces (length title) len-title) + (vt100-codes :clear)) + (set ix (inc ix))))) + +(defn rm [] + (let [id (request-val "Id") + to-delete (->> (load-entries) + (filter (fn [e] + (= id (e :id)))) + first) + confirm (fn [] + (not= "n" (request-val + (string "Confirm deletion of " + (to-delete :title) + " (Y/n)"))))] + (when (and to-delete + (confirm)) + (->> (load-entries) + (filter (fn [e] (not= id (e :id)))) + (save-entries))))) + (def structure {"add" add - "rm" (fn [] (print "rm")) + "rm" rm "mod" (fn [] (print "mod")) "help" help - "" (fn [] (print "overview")) + "" overview "quit" quit}) (defn main [& args]