-
Notifications
You must be signed in to change notification settings - Fork 1
/
snotify
executable file
·112 lines (94 loc) · 2.02 KB
/
snotify
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
#!/bin/sh -e
help() {
cat - >&2 <<EOF
snotify - a simple notification system
snotify [-ndr]
snotify is a simple notification system designed for use with status
bars et al. and based around keeping notifications in a single file
located at ~/.snotify (snotify manages this file, don't modify it
manually).
The default behavior when supplied no arguments is to display the most
recent notification.
The following options are recognized:
-n msg...
Add notification msg. Multiple arguments to this option will be
joined by the first character of the IFS (usually whitespace).
-d
Delete the most recent notification
-r
Delete all notifications
-
Concatenate stdin onto the end of the notification file
EOF
}
case "$1" in --help|-h) help; exit 0;; esac
: "${SNOTIFY_FILE:="$HOME"/.snotify}"
case "$SNOTIFY_FILE" in -*) SNOTIFY_FILE=./"$SNOTIFY_FILE" ;; esac
LOCK=$SNOTIFY_FILE.lock
if command -v flock > /dev/null 2>&1; then
has_flock=1
else
unset -v has_flock
fi
lock() {
# tries a good lock implementation, falls back to something dirtier
if [ "$has_flock" ]; then
exec 9> "$LOCK"
flock 9
else
while ! mkdir "$LOCK"; do
sleep 0.1
done
fi
}
unlock() {
if [ "$has_flock" ]; then
flock -u 9
else
rmdir "$LOCK"
fi
}
if [ ! "$has_flock" ]; then
trap 'trap - EXIT QUIT HUP INT TERM ABRT; rmdir "$LOCK"' EXIT QUIT HUP INT TERM ABRT
fi
if [ "$*" ]; then
case "$1" in
-n)
lock
shift 1
# avoid recording duplicate messages
if ! grep -Fxqe "$*" "$SNOTIFY_FILE"; then
printf '%s\n' "$*" >> "$SNOTIFY_FILE"
fi
;;
-d)
lock
new=$(head -n -1 "$SNOTIFY_FILE")
cat > "$SNOTIFY_FILE" <<-EOF
$new
EOF
;;
-r)
rm "$SNOTIFY_FILE" "$LOCK"
;;
-)
while read -r line; do
lock
# avoid recording duplicate messages
if grep -Fxqe "$line" "$SNOTIFY_FILE"; then
continue
fi
printf '%s\n' "$line" >> "$SNOTIFY_FILE"
unlock
done
;;
*)
help
exit 1
;;
esac
else
# shouldn't be necessary to lock here
: >> "$SNOTIFY_FILE"
tail -n 1 "$SNOTIFY_FILE"
fi