Skip to content

Commit

Permalink
Fix maybe-alert not sending notifications to my computer anymore.
Browse files Browse the repository at this point in the history
Apparently --urgency=low is too low for a popup on my laptop 🤷

Also, touch it up to use better practices.
  • Loading branch information
JeffFaer committed Jan 10, 2024
1 parent f06b3f5 commit 7f4a59a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions maybe-alert
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
#
# Run notify-send if the command takes longer than $1 seconds

if [[ -z $DISPLAY ]]; then

set -euo pipefail
[[ -n "${DEBUG:-}" ]] && set -x

if [[ -z "${DISPLAY}" ]]; then
exec "${@:2}"
fi

max=$1
command="${*:2}"
begin=$(date +%s)
begin="$(date +%s)"
alerted=0

_alert() {
local exit_status=$?

if ((!alerted)); then
alerted=1
local end=$(date +%s)
local end="$(date +%s)"

local elapsed=$((end - begin))
if [[ $exit_status != 0 || $elapsed -ge $max ]]; then
icon=$([[ $exit_status == 0 ]] && echo info || echo error)
summary="Command took $(display-seconds $elapsed) to complete."
if [[ ${exit_status} != 0 || ${elapsed} -ge ${max} ]]; then
icon=$([[ ${exit_status} == 0 ]] && echo info || echo error)
summary="Command took $(display-seconds "${elapsed}") to complete."

notify-send --urgency=low -i "$icon" "$summary" "$command"
notify-send -i "${icon}" "${summary}" "${command}"
fi
fi

exit $exit_status
exit ${exit_status}
}
trap '_alert' INT QUIT TERM EXIT

Expand Down

0 comments on commit 7f4a59a

Please sign in to comment.