-
Notifications
You must be signed in to change notification settings - Fork 1
/
snotify
executable file
·95 lines (79 loc) · 1.92 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
#!/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
# read stdin before grabbing lock, since reading stdin may take a while
if [ "$1" = '-' ]; then
STDIN=$(cat -)
[ "$STDIN" ] || exit 1
fi
# try to use a good lock implementation, fall back to something dirtier
LOCK=$SNOTIFY_FILE.lock
if command -v flock > /dev/null 2>&1; then
exec 9> "$LOCK"
flock 9
else
while ! mkdir "$LOCK"; do
sleep 0.1
done
trap 'trap - EXIT QUIT HUP INT TERM ABRT; rmdir "$LOCK"' EXIT QUIT HUP INT TERM ABRT
fi
if [ "$*" ]; then
case "$1" in
-n)
shift 1
# avoid recording duplicate messages
if grep -Fxqe "$*" "$SNOTIFY_FILE"; then
exit 0
fi
printf '%s\n' "$*" >> "$SNOTIFY_FILE";
;;
-d)
new=$(head -n -1 "$SNOTIFY_FILE")
cat > "$SNOTIFY_FILE" <<-EOF
$new
EOF
;;
-r)
rm "$SNOTIFY_FILE"
;;
-)
# avoid recording duplicate messages
printf '%s\n' "$STDIN" | while read -r line; do
if grep -Fxqe "$line" "$SNOTIFY_FILE"; then
continue
fi
printf '%s\n' "$line" >> "$SNOTIFY_FILE"
done
;;
*)
help
exit 1
;;
esac
else
: >> "$SNOTIFY_FILE"
tail -n 1 "$SNOTIFY_FILE"
fi