summaryrefslogtreecommitdiff
path: root/scripts/slib
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/slib')
-rwxr-xr-xscripts/slib49
1 files changed, 32 insertions, 17 deletions
diff --git a/scripts/slib b/scripts/slib
index 0e0de36..4e21eb6 100755
--- a/scripts/slib
+++ b/scripts/slib
@@ -1,7 +1,6 @@
#!/bin/sh
-# Avoid using full paths instead only use the program names.
-argv0="${0}"
+argv0=$(basename "${0}")
# @FUNCTION: err
# @USAGE: [-x] <message> ...
@@ -14,7 +13,6 @@ argv0="${0}"
# This function is intended for fatal errors; it always exits the script.
# @EXAMPLE:
# err "Invalid usage" "Try '${argv0} -h' for help."
-
err() {
if [ "${1}" != "-x" ]; then
printf "%s: " "${argv0}"
@@ -61,21 +59,38 @@ check_program() {
err "${1} must be installed"
}
+# @FUNCTION: send_notification
+# @USAGE: send_notification <message> ...
+# @DESCRIPTION:
+# Write a notification message to a temporary file and trigger a dwm notification signal.
+# The message is written to /tmp/noti.txt. If writing fails, a warning is printed to stderr.
+# After writing, a dwm signal is sent using `xsetroot -name "fsignal:1"`.
+#
+# @EXAMPLE:
+# Send a desktop notification with the message "Backup complete".
+#
+# send_notification "Backup complete"
+
+send_notification() {
+ echo "$@" > /tmp/noti.txt || echo "Warning: failed to write to notification file" >&2
+ xsetroot -name "fsignal:1"
+}
+
# @FUNCTION: run
-# @USAGE: [--reload-status] [--reload-compositor] [--on-success <cmd>] [--on-failure <cmd>] <command> [success-msg] [failure-msg]
+# @USAGE: [--reload-status] [--reload-compositor] [--success-notify <msg>] [--failure-notify <msg>] <command> [success-msg] [failure-msg]
# @DESCRIPTION:
# Safely execute a simple shell command, print optional success/failure messages,
-# and optionally reload status bars or restart the compositor.
+# and optionally reload the status bar or restart the compositor.
#
# Success messages are printed to stdout; failure messages are printed to stderr.
-# To specify a failure message, a success message must also be provided.
+# Optional desktop notifications can be sent using --success-notify and --failure-notify.
# Command output is not suppressed.
#
# Options:
# --reload-status Reload the status bar (via `slreload`).
# --reload-compositor Restart the compositor (kills and restarts `picom`).
-# --on-success <cmd> Run another command if the main command succeeds.
-# --on-failure <cmd> Run another command if the main command fails.
+# --success-notify <msg> Send a desktop notification on success.
+# --failure-notify <msg> Send a desktop notification on failure.
#
# Restrictions:
# - Does NOT use `eval`; only simple commands and arguments are supported.
@@ -83,7 +98,7 @@ check_program() {
# - This design prevents unintended execution and makes the function safe in scripts.
#
# Return value:
-# 0 if the command succeeds, 1 otherwise.
+# Exits with 0 if the command succeeds, 1 otherwise.
#
# @EXAMPLES:
# # Run xwallpaper and print the image path on success
@@ -92,10 +107,10 @@ check_program() {
# # Restart compositor and show a success message
# run --reload-compositor "xwallpaper --zoom ${image}" "Wallpaper updated" "Wallpaper failed"
#
-# # With success and failure hooks
-# run --on-success "notify-send 'OK'" \
-# --on-failure "notify-send 'Failed'" \
-# "cp config config.bak" "Backup complete" "Backup failed"
+# # With notification hooks
+# run --success-notify "Wallpaper set" \
+# --failure-notify "Wallpaper failed" \
+# "xwallpaper --zoom ${image}" "Wallpaper updated" "Wallpaper failed"
run() {
relstat=0
@@ -105,8 +120,8 @@ run() {
case "$1" in
--reload-status) relstat=1 ;;
--reload-compositor) compstat=1 ;;
- --on-success) success_command="${2}"; shift ;;
- --on-failure) failure_command="${2}"; shift ;;
+ --success-notify) success_msg="${2}"; shift ;;
+ --failure-notify) failure_msg="${2}"; shift ;;
*) break;
esac
shift
@@ -118,10 +133,10 @@ run() {
if ${1}; then
[ -n "${2}" ] && echo "${2}"
- [ -n "${success_command}" ] && sh -c "${success_command}"
+ [ -n "${success_msg}" ] && send_notification "${argv0}:" "${success_msg}"
else
[ -n "${3}" ] && err "${3}"
- [ -n "${failure_command}" ] && sh -c "${failure_command}"
+ [ -n "${failure_msg}" ] && send_notification "${argv0}:" "${failure_msg}"
exit 1
fi