-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlong-running-notifications.elv
93 lines (79 loc) · 2.29 KB
/
long-running-notifications.elv
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
91
92
93
var threshold = 10
var last-cmd-start-time = 0
var last-cmd = ""
var last-cmd-duration = 0
var notifier = auto
var notifications-to-try = [ macos libnotify text ]
var never-notify = [ vi vim emacs nano less more bat ]
var always-notify = [ ]
var notification-fns = [
&text= [
&check= { put $true }
¬ify= {|cmd dur start|
echo (styled "Command lasted "$dur"s" magenta) > /dev/tty
}
]
&libnotify= [
&check= { put ?(which notify-send >/dev/null 2>&1) }
¬ify= {|cmd duration start|
notify-send "Finished: "$cmd "Running time: "$duration"s"
}
]
&macos= [
&check= { put ?(which terminal-notifier >/dev/null 2>&1) }
¬ify= {|cmd duration start|
terminal-notifier -title "Finished: "$cmd -message "Running time: "$duration"s"
}
]
]
fn -choose-notification-fn {
each {|method-name|
var method = $notification-fns[$method-name]
if ($method[check]) {
put $method[notify]
return
}
} $notifications-to-try
fail "No valid notification mechanism was found"
}
fn -produce-notification {
if (not-eq (kind-of $notifier) fn) {
if (eq $notifier auto) {
set notifier = (-choose-notification-fn)
} elif (has-key $notification-fns $notifier) {
set notifier = $notification-fns[$notifier][notify]
} else {
fail "Invalid value for $long-running-notifications:notifier: "$notifier", please double check"
}
}
$notifier $last-cmd $last-cmd-duration $last-cmd-start-time
}
fn now {
put (date +%s)
}
fn -last-cmd-in-list {|list|
var cmd = (take 1 [(edit:wordify $last-cmd) ""])
has-value $list $cmd
}
fn -always-notify { -last-cmd-in-list $always-notify }
fn -never-notify { -last-cmd-in-list $never-notify }
fn before-readline-hook {
var -end-time = (now)
set last-cmd-duration = (- $-end-time $last-cmd-start-time)
if (or (-always-notify) (and (not (-never-notify)) (> $last-cmd-duration $threshold))) {
-produce-notification
}
}
fn after-readline-hook {|cmd|
set last-cmd = $cmd
set last-cmd-start-time = (now)
}
fn init {
# Set up the hooks
use ./prompt-hooks
prompt-hooks:add-before-readline $before-readline-hook~
prompt-hooks:add-after-readline $after-readline-hook~
# Initialize to avoid spurious notification when the module is loaded
set last-cmd-start-time = (now)
}
init