-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3blocks-task
executable file
·54 lines (48 loc) · 1.9 KB
/
i3blocks-task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env bb
(ns script
(:require [clojure.string :as str]
[cheshire.core :as json]
[clojure.java.shell :refer [sh]]))
(defn str->epoch
[datetime]
(str (/ (.toEpochMilli
(.toInstant (java.time.ZonedDateTime/parse
datetime
(java.time.format.DateTimeFormatter/ofPattern
"uuuuMMdd'T'HHmmssX"))))
1000)))
;; TODO: Move `timeago` from bash to clojure:
(defn epoch->timeago
[epoch]
(let [{timeago-exit :exit, timeago-err :err, timeago-out :out}
(try (sh "timeago" epoch) (catch java.lang.Exception e {:exit 1}))]
(if (zero? timeago-exit) (str " ( " timeago-out " )") "")))
(defn format-output
[count-total active-id active-description active-start]
(str "Total: "
(str/trim-newline count-total)
(if (nil? active-id)
""
(str " Active: [ "
active-id
" ] "
active-description
(epoch->timeago (str->epoch active-start))))))
(let [{count-exit :exit, count-err :err, count-out :out}
(sh "task" "count" "status:pending")
{active-exit :exit, active-err :err, active-out :out}
(sh "task" "+ACTIVE" "export")]
(if (and (zero? count-exit) (zero? active-exit))
(let [{:keys [id description start]} (first (json/parse-string active-out
true))]
(println
(json/generate-string
(let [output (format-output count-out id description start)
count-output (count output)]
{:full_text output,
:short_text (if (> count-output 59)
(str (subs output 0 (min 59 count-output)) "...")
output),
:color "#fcc0cf"}))))
(do (println "ERROR:" count-err) (System/exit 1))))
; vim: set filetype=clojure: