-
Notifications
You must be signed in to change notification settings - Fork 136
/
makoctl
executable file
·202 lines (192 loc) · 4.97 KB
/
makoctl
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh -eu
usage() {
echo "Usage: makoctl <command> [options...]"
echo ""
echo "Commands:"
echo " dismiss [-n id] Dismiss the notification with the"
echo " given id, or the last notification"
echo " if none is given"
echo " [-a|--all] Dismiss all notifications"
echo " [-g|--group] Dismiss all the notifications"
echo " in the last notification's group"
echo " restore Restore the most recently expired"
echo " notification from the history buffer"
echo " invoke [-n id] [action] Invoke an action on the notification"
echo " with the given id, or the last"
echo " notification if none is given"
echo " menu [-n id] <prog> [arg ...] Use <prog> [args ...] to select one"
echo " action to be invoked on the notification"
echo " with the given id, or the last"
echo " notification if none is given"
echo " list List notifications"
echo " history List history"
echo " reload Reload the configuration file"
echo " mode List modes"
echo " mode [-a mode]... [-r mode]... Add/remove modes"
echo " mode [-t mode]... Toggle modes (add if not present, remove if present)"
echo " mode -s mode... Set modes"
echo " help Show this help"
}
BUSCTL=busctl
if ! type $BUSCTL >/dev/null 2>&1; then
BUSCTL=basuctl
fi
if ! type $BUSCTL >/dev/null 2>&1; then
echo >&2 "$0: busctl or basuctl is required"
exit 1
fi
call() {
$BUSCTL -j --user call org.freedesktop.Notifications /fr/emersion/Mako \
fr.emersion.Mako -- "$@"
}
require_jq() {
if ! type jq >/dev/null 2>&1; then
echo >&2 "$0: jq is required to use this command"
exit 1
fi
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
case "$1" in
"dismiss")
all=0
group=0
id=0
while [ $# -gt 1 ]; do
case "$2" in
"-a"|"--all")
all=1
;;
"-g"|"--group")
group=1
;;
"-n")
if [ $# -lt 3 ]; then
echo >&2 "$0: Expected <id> after '-n'"
exit 1
fi
id=$3
shift
;;
*)
echo >&2 "$0: Unrecognized option: $2"
exit 1
;;
esac
shift
done
if [ $all -eq 1 ]; then
call DismissAllNotifications
else
call DismissNotification "ub" "$id" "$group"
fi
;;
"invoke")
id=0
if [ $# -gt 1 ] && [ "$2" = "-n" ]; then
id="$3"
shift 2
fi
action="default"
if [ $# -gt 1 ]; then
action="$2"
fi
call InvokeAction "us" "$id" "$action"
;;
"restore")
call RestoreNotification
;;
"menu")
shift 1
require_jq
if [ $# -gt 1 ] && [ "$1" = "-n" ]; then
id="$2"
actions="$(call ListNotifications | jq --arg id "$id" -re '.data[0][] | select(.id.data==($id | tonumber)) | .actions.data')"
shift 2
else
actions="$(call ListNotifications | jq -re '.data[0][0].actions.data')"
id="0"
fi
if [ "$(jq -rn "$actions | length")" -eq "0" ]; then
echo >&2 "$0: No actions found"
exit 1
fi
sel="$(jq -rn "$actions|values[]" | "$@")"
sel="$(jq -rn --arg sel "$sel" "$actions|to_entries[]|select(.value == \$sel).key")"
if [ -z "$sel" ]; then
echo >&2 "$0: No action selected"
exit 1
else
call InvokeAction "us" "$id" "$sel"
fi
;;
"list")
call ListNotifications
;;
"history")
call ListHistory
;;
"reload")
call Reload
;;
"set-mode") # deprecated
call SetMode "s" "$2"
;;
"mode")
shift 1
require_jq
modes="$(call ListModes | jq '.data[0]')"
add_remove_toggle_flag=0
set_flag=0
while getopts a:r:t:s name; do
case "$name" in
a)
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" '. += [$mode]')"
;;
r)
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" 'del(.[] | select(. == $mode))')"
;;
t)
add_remove_toggle_flag=1
modes="$(echo "$modes" | jq --arg mode "$OPTARG" '. |= if any(.[]; . == $mode) then map(select(. != $mode)) else . + [$mode] end')"
;;
s)
set_flag=1
;;
?)
exit 1
esac
done
shift $(($OPTIND - 1))
if [ "$add_remove_toggle_flag" = 1 ] && [ "$set_flag" = 1 ]; then
echo >&2 "makoctl: -a/-r/-t and -s cannot be used together"
exit 1
fi
if [ "$set_flag" = 1 ]; then
modes="$(jq -n '$ARGS.positional' --args "$@")"
elif [ $# -gt 0 ]; then
echo >&2 "makoctl: positional arguments can only be used with -s"
exit 1
fi
if [ "$add_remove_toggle_flag" = 1 ] || [ "$set_flag" = 1 ]; then
modes="$(echo "$modes" | jq '. | unique')"
modes_len="$(echo "$modes" | jq '. | length')"
modes_args=$(echo "$modes" | jq -r '@sh')
# Required to behave properly when mode names contain spaces
eval set -- $modes_args
call SetModes "as" "$modes_len" "$@"
fi
echo "$modes" | jq -r '.[]'
;;
"help"|"--help"|"-h")
usage
;;
*)
echo "makoctl: unrecognized command '$1'"
exit 1
;;
esac