generated from me/template-mit
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:
1 parent
28c0f61696
commit
5fd554e7d5
1 file changed
+35
-9
+35
-9
@@ -1,21 +1,20 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# sunset-alarm -- Alert before or after sunset.
|
# sunset-alarm -- Alert before or after sunset.
|
||||||
# Displays a persistent alert when current time is within
|
# Displays an alert when current time is within (sunset - specified minutes).
|
||||||
# (sunset minus specified minutes) of midnight.
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Defaults
|
# Defaults
|
||||||
###############################################################################
|
###############################################################################
|
||||||
declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE=""
|
declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE="" PERSIST="" NOTIFIED=""
|
||||||
typeset -i SUNSET_EPOCH=0
|
typeset -i SUNSET_EPOCH=0 CURRENT_TIME=0
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Usage
|
# Usage
|
||||||
###############################################################################
|
###############################################################################
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
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.
|
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.
|
-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.
|
-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.
|
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.
|
Example: Show the alert 90 minutes before sunset in Colorado City AZ.
|
||||||
sunset-alarm -l 37:-113 -m 90
|
sunset-alarm -l 37:-113 -m 90
|
||||||
@@ -46,6 +47,7 @@ while [ $# -gt 0 ]; do
|
|||||||
-l) LATLONG="$2"; shift 2 ;;
|
-l) LATLONG="$2"; shift 2 ;;
|
||||||
-t) SUNSET_TIME="$2"; shift 2 ;;
|
-t) SUNSET_TIME="$2"; shift 2 ;;
|
||||||
-m) MINUTES="$2"; shift 2 ;;
|
-m) MINUTES="$2"; shift 2 ;;
|
||||||
|
-p) PERSIST="true"; shift 1 ;;
|
||||||
-h) usage ;;
|
-h) usage ;;
|
||||||
--) shift; break ;;
|
--) shift; break ;;
|
||||||
*) echo "ERROR: unknown argument '$1'" >&2; usage ;;
|
*) echo "ERROR: unknown argument '$1'" >&2; usage ;;
|
||||||
@@ -141,13 +143,13 @@ show_alert() {
|
|||||||
TITLE="Sunset Alarm ($ALERT)"
|
TITLE="Sunset Alarm ($ALERT)"
|
||||||
MESSAGE="It's ${MINUTES} minute${PLURAL} to $SUNSET."
|
MESSAGE="It's ${MINUTES} minute${PLURAL} to $SUNSET."
|
||||||
|
|
||||||
persistent="-t 0 --wait"
|
keep_alive="-t 0 --wait"
|
||||||
icon="-i weather-clear -u critical"
|
icon="-i weather-clear -u critical"
|
||||||
|
|
||||||
if command -v fyi >/dev/null 2>&1; then
|
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
|
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
|
elif command -v wall >/dev/null 2>&1; then
|
||||||
echo "${TITLE}: ${MESSAGE}" | wall
|
echo "${TITLE}: ${MESSAGE}" | wall
|
||||||
else
|
else
|
||||||
@@ -157,6 +159,18 @@ show_alert() {
|
|||||||
fi
|
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
|
# Main loop
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@@ -208,19 +222,31 @@ while true; do
|
|||||||
|
|
||||||
# Alert while current time is within the window (from window start until midnight).
|
# Alert while current time is within the window (from window start until midnight).
|
||||||
if [[ "$SUNSET_EPOCH" -ge 0 \
|
if [[ "$SUNSET_EPOCH" -ge 0 \
|
||||||
|
&& ( -z "$NOTIFIED" || -n "$PERSIST" ) \
|
||||||
&& "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \
|
&& "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \
|
||||||
&& "$CURRENT_TIME" -lt "$MIDNIGHT" ]]
|
&& "$CURRENT_TIME" -lt "$MIDNIGHT" ]]
|
||||||
then
|
then
|
||||||
echo "Alert time has been reached, show message."
|
echo "Alert time has been reached, show message."
|
||||||
show_alert
|
show_alert
|
||||||
|
NOTIFIED="true"
|
||||||
|
|
||||||
echo "Alert has been dismissed, sleeping for 5 minutes."
|
echo "Alert has been dismissed, sleeping for 5 minutes."
|
||||||
sleep 290
|
sleep 290
|
||||||
echo "Waking back up from notification dismissal."
|
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
|
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
|
if [[ "$CURRENT_TIME" -ge "$MIDNIGHT" ]]; then
|
||||||
LAST_DATE=""
|
LAST_DATE=""
|
||||||
|
NOTIFIED=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep 10
|
sleep 10
|
||||||
|
|||||||
Reference in new issue
Block a user