summaryrefslogtreecommitdiff
path: root/notify-replace
diff options
context:
space:
mode:
Diffstat (limited to 'notify-replace')
-rwxr-xr-xnotify-replace37
1 files changed, 37 insertions, 0 deletions
diff --git a/notify-replace b/notify-replace
new file mode 100755
index 0000000..343a825
--- /dev/null
+++ b/notify-replace
@@ -0,0 +1,37 @@
+#!/biin/dash -e
+# Sends, remembers, and replaces notifications based on program name that's
+# passed as first argument to script.
+# Non-POSIX features used: flock(1)
+# TODO: consider replacing flock(1) with a POSIX-complaint alternative
+
+xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
+config_dir="$xdg_config_home/notify-replace"
+[ -d "$config_dir" ] || mkdir -p "$config_dir"
+
+if [ -z "$1" ]; then
+ # TODO: check if there's a better way to acquire name of currently
+ # executing script
+ # TODO: get rid of subshell
+ program=$(basename $0)
+ # TODO: shorten line
+ echo "$program: Need to provide notification id filename as first argument" 1>&2
+ exit 1
+fi
+lockdir="$config_dir/notify-replace.lock"
+notification_file="$config_dir/$1"
+shift
+
+# Exit if lock is in use. A lock is used to prevent multiple invocations
+# of script from replacing notification id at the same time
+exec 9>"$lockdir"
+flock -n 9 || exit 1
+
+if [ -s "$notification_file" ]; then
+ # replace previous notification
+ nid="$(notify-send -pr "$(cat "$notification_file")" "$@")"
+else
+ # send new notification
+ nid="$(notify-send -p "$@")"
+fi
+# Remember ID of sent notification.
+printf '%s' "$nid" >"$notification_file"