-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.clj
90 lines (73 loc) · 2.79 KB
/
notify.clj
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(ns ralphie.notify
(:require
[babashka.process :as process :refer [$ check]]
[ralphie.zsh :as zsh]))
(def is-mac? (boolean (#{"darwin21"} (zsh/expand "$OSTYPE"))))
(comment
is-mac?)
(defn notify
([notice]
(cond (string? notice) (notify notice nil)
(map? notice)
(let [subject (some notice [:subject :notify/subject])
body (some notice [:body :notify/body])]
(notify subject body notice))
:else
(notify "Malformed ralphie.notify/notify call"
"Expected string or map.")))
([subject body & args]
(let [opts (or (some-> args first) {})
print? (:notify/print? opts)
replaces-process (some opts [:notify/id :replaces-process :notify/replaces-process])
exec-strs
(if is-mac?
["osascript" "-e" (str "display notification \""
(cond
(string? body) body
;; TODO escape stringified bodies for osascript's standards
(not body) "no body"
:else "unsupported body")
"\""
(when subject
(str " with title \"" subject "\"")))]
(cond->
["notify-send.py" subject]
body (conj body)
replaces-process
(conj "--replaces-process" replaces-process)))
;; _
;; (println exec-strs)
_ (when print?
;; TODO use dynamic global bool to print all notifs
(println subject (when body (str "\n" body))))
proc (process/process (conj exec-strs) {:out :string})]
;; we only check when --replaces-process is not passed
;; ... skips error messages if bad data is passed
;; ... also not sure when these get dealt with. is this a memory leak?
(when-not replaces-process
(-> proc check :out))
nil)))
(comment
(notify "subj" "body\nbody\nbody")
(notify {:subject "subj"})
(notify "subj" "body\nbodybodddd" {:replaces-process "blah"})
(some {:blah "nope" :notify/subject 3} [:notify/subject :subject])
(notify "nice")
(notify {:subject "subj" :body {:value "v" :label "laaaa"}})
(notify {:subject "subj" :body "BODY"})
(-> ($ notify-send subj body)
check)
(->
^{:out :string}
($ echo $OSTYPE)
check
:out)
(->
($ osascript -e "display notification \"Lorem ipsum dolor sit amet\" with title \"Title\"")
check
)
(->
(process/process ["osascript" "-e" "display notification \"Lorem ipsum\ndolor sit amet\" with title \"Title\""])
check
)
)