Skip to content

Instantly share code, notes, and snippets.

@kolja
Created August 1, 2024 08:47
Show Gist options
  • Save kolja/e5c1cda9dfd7be3beda4e00741ed0b7d to your computer and use it in GitHub Desktop.
Save kolja/e5c1cda9dfd7be3beda4e00741ed0b7d to your computer and use it in GitHub Desktop.
wrapper for the remind command line calendar
#!/usr/bin/env bb
(require '[clojure.string :as str]
'[babashka.process :refer [shell process]]
'[clojure.tools.cli :refer [parse-opts]])
(def cli-opts [["-h" "--help" "Show help"]
["-y" "--year YEAR" "Year (default to current year)"]])
(defn usage [options-summary]
(->> [""
"Wrapper for the rem command line calendar tool"
""
(str " Usage:\t" (fs/file-name *file*) " <month> <options>")
""
options-summary]
(str/join \newline)))
(defn exit [message code]
(if (= code 0)
(println message)
(binding [*out* *err*] (println message)))
(System/exit code))
(def month-map {"jan" 1 "feb" 2 "mar" 3 "mär" 3 "apr" 4 "may" 5 "mai" 5 "jun" 6 "jul" 7 "aug" 8 "sep" 9 "oct" 10 "okt" 10 "nov" 11 "dec" 12 "dez" 12})
(defn month-to-number
"Converts a month name to a number"
[month]
(let [month-num (try
(Integer/parseInt month)
(catch Exception e
(month-map (subs (str/lower-case month) 0 3) 0)))]
(when (or (nil? month-num)
(not (<= 1 month-num 12)))
(exit (str "Invalid month: " month) 1))
month-num))
(defn -main [& args]
(let [{:keys [options arguments summary errors]} (parse-opts args cli-opts)
cols (str/trim-newline (get (shell {:out :string} "tput" "cols") :out))
year (or
(get options :year)
(-> (java.time.LocalDate/now) .getYear))
month (if (zero? (count arguments)) nil (month-to-number (first arguments)))
command (if month
[{:out :string} "rem" "-ccl" (str "-w" cols) (str year "-" month "-1")]
[{:out :string} "rem" "-mccl+2" (str "-w" cols)])]
(when (get options :help)
(exit (usage summary) 0))
(-> (apply shell command)
:out
print)
))
(apply -main *command-line-args*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment