Make the recurring notifications optional. Sleep exactly as many seconds as needed until the next work needs to be done.

This commit is contained in:
me committed 2026-11-03 19:22:19 -07:00
1 parent 28c0f61696
commit 5fd554e7d5
1 file changed
+35 -9
+35 -9
View File
@@ -1,21 +1,20 @@
#!/bin/sh
# sunset-alarm -- Alert before or after sunset.
# Displays a persistent alert when current time is within
# (sunset minus specified minutes) of midnight.
# Displays an alert when current time is within (sunset - specified minutes).
###############################################################################
# Defaults
###############################################################################
declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE=""
typeset -i SUNSET_EPOCH=0
declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE="" PERSIST="" NOTIFIED=""
typeset -i SUNSET_EPOCH=0 CURRENT_TIME=0
###############################################################################
# Usage
###############################################################################
usage() {
cat <<EOF
Usage: sunset-alarm (-l LAT:LONG | -t HH24:MI) (-m MINUTES) [-h]
Usage: sunset-alarm (-l LAT:LONG | -t HH24:MI) (-m MINUTES) [-p] [-h]
Display a notification when a certain time related to sunset has been reached.
@@ -25,6 +24,8 @@ Parameters:
-t) Takes a hard-coded 24-hour formatted time for guessing when sunset is.
-m) Takes the number of minutes that the alert should appear before sunset.
Using a negative time is allowed if you prefer having the alert after.
-p) Persist the notification, showing back up every 5 minutes to midnight.
-h) Display this usage.
Example: Show the alert 90 minutes before sunset in Colorado City AZ.
sunset-alarm -l 37:-113 -m 90
@@ -46,6 +47,7 @@ while [ $# -gt 0 ]; do
-l) LATLONG="$2"; shift 2 ;;
-t) SUNSET_TIME="$2"; shift 2 ;;
-m) MINUTES="$2"; shift 2 ;;
-p) PERSIST="true"; shift 1 ;;
-h) usage ;;
--) shift; break ;;
*) echo "ERROR: unknown argument '$1'" >&2; usage ;;
@@ -141,13 +143,13 @@ show_alert() {
TITLE="Sunset Alarm ($ALERT)"
MESSAGE="It's ${MINUTES} minute${PLURAL} to $SUNSET."
persistent="-t 0 --wait"
keep_alive="-t 0 --wait"
icon="-i weather-clear -u critical"
if command -v fyi >/dev/null 2>&1; then
fyi $icon $persistent "$TITLE" "$MESSAGE"
fyi $icon $keep_alive "$TITLE" "$MESSAGE"
elif command -v notify-send >/dev/null 2>&1; then
notify-send $icon $persistent "$TITLE" "$MESSAGE"
notify-send $icon $keep_alive "$TITLE" "$MESSAGE"
elif command -v wall >/dev/null 2>&1; then
echo "${TITLE}: ${MESSAGE}" | wall
else
@@ -157,6 +159,18 @@ show_alert() {
fi
}
###############################################################################
# Sleep until a certain amount of time
###############################################################################
rest() {
if [[ "$CURRENT_TIME" -ge 0 ]]; then
TARGET="$1"
sleep_seconds=$(( TARGET - CURRENT_TIME ))
echo "Sleeping ${sleep_seconds}s until $(date -d @$TARGET +%H:%M:%S)."
sleep "$sleep_seconds"
fi
}
###############################################################################
# Main loop
###############################################################################
@@ -208,19 +222,31 @@ while true; do
# Alert while current time is within the window (from window start until midnight).
if [[ "$SUNSET_EPOCH" -ge 0 \
&& ( -z "$NOTIFIED" || -n "$PERSIST" ) \
&& "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \
&& "$CURRENT_TIME" -lt "$MIDNIGHT" ]]
then
echo "Alert time has been reached, show message."
show_alert
NOTIFIED="true"
echo "Alert has been dismissed, sleeping for 5 minutes."
sleep 290
echo "Waking back up from notification dismissal."
elif [[ "$SUNSET_EPOCH" == 0 ]]; then
echo "ERROR: Sunset time has not been calculated yet, cannot alert." >&2
elif [[ -n "$NOTIFIED" && -z "$PERSIST" ]]; then
echo "Already notified for today ($TODAY)."
rest $MIDNIGHT
else
echo "Not time to notify yet."
rest $ALERT_WINDOW_START
fi
# Reset when we've passed midnight so we fetch new time tomorrow.
# Reset when we've passed midnight so we fetch the new time for the day.
if [[ "$CURRENT_TIME" -ge "$MIDNIGHT" ]]; then
LAST_DATE=""
NOTIFIED=""
fi
sleep 10