Skip to content

Instantly share code, notes, and snippets.

@prestancedesign
Last active December 8, 2020 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prestancedesign/d2f6ba223e418298618966937062dda0 to your computer and use it in GitHub Desktop.
Save prestancedesign/d2f6ba223e418298618966937062dda0 to your computer and use it in GitHub Desktop.
A TODO cli with Clojure (Babashka)
#!/usr/bin/env bb
(require '[clojure.string :refer [split]])
(require '[clojure.java.io :refer [writer reader]])
(require '[clojure.pprint :refer [print-table]])
(def file-location (System/getenv "TODO_LOCATION"))
(defn now [] (new java.util.Date))
(defn add-content
"appends content to todo file"
[file-location text-content]
(with-open [file (writer file-location :append true)]
(.write file (str text-content "\t" (now) "\n"))))
(defn print-helper
"Converts line content to a row obj"
[line-content]
(let [[todo created_at] (split line-content #"\t")]
{:todo todo :created_at (or created_at "UNKNOWN")}))
(defn read-content
"reads content from todo file"
[file-location]
(with-open [file (reader file-location)]
(let [file-content (slurp file)]
(print-table
(map print-helper
(split file-content #"\n"))))))
(let [args *command-line-args*]
(when (nil? file-location)
(throw (AssertionError. "empty $TODO_LOCATION")))
(case (first args)
"a" (do
(add-content file-location (second args))
(read-content file-location))
"ls" (read-content file-location)
(println "Choose either a or ls")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment