diff options
Diffstat (limited to 'hooks')
| -rwxr-xr-x | hooks/on_done | 31 | ||||
| -rwxr-xr-x | hooks/on_quit | 28 | ||||
| -rwxr-xr-x | hooks/on_start | 26 |
3 files changed, 85 insertions, 0 deletions
diff --git a/hooks/on_done b/hooks/on_done new file mode 100755 index 0000000..955cd13 --- /dev/null +++ b/hooks/on_done @@ -0,0 +1,31 @@ +#!/bin/sh +# ttym on_done -- runs when a countdown reaches zero. +# +# $1 mode always "countdown" (a stopwatch never completes) +# $2 seconds the full duration that elapsed +# $3 comment the -c text, or an empty string +# +# timer waits for this to exit before ringing the bell and entering the +# alert loop, so background anything slow. + +seconds=$2 comment=$3 +mins=$((seconds / 60)) + +# Desktop notification. Gives the base build what the notify patch adds. +if command -v notify-send > /dev/null 2>&1; then + notify-send -u critical "Timer" "${mins}m done${comment:+ -- $comment}" & +fi + +# A sound, first player that exists wins. Backgrounded so it never blocks. +for player in paplay aplay mpv ffplay; do + if command -v "$player" > /dev/null 2>&1; then + sound=/usr/share/sounds/freedesktop/stereo/complete.oga + [ -r "$sound" ] && "$player" "$sound" > /dev/null 2>&1 & + break + fi +done + +# Push to your phone (uncomment and set your topic): +# curl -fsS -d "${mins}m done${comment:+ -- $comment}" ntfy.sh/your-topic & + +wait diff --git a/hooks/on_quit b/hooks/on_quit new file mode 100755 index 0000000..44957e8 --- /dev/null +++ b/hooks/on_quit @@ -0,0 +1,28 @@ +#!/bin/sh +# ttym on_quit -- runs when a session ends early: q, Ctrl+C, SIGTERM, +# SIGQUIT or SIGHUP -- and whenever a stopwatch is stopped, since a +# stopwatch has no natural end. +# +# $1 mode countdown | stopwatch +# $2 seconds time actually elapsed, excluding time spent paused +# $3 comment the -c text, or an empty string +# +# A countdown abandoned after two seconds reports 2, not the duration +# you asked for. That is what makes this useful for tracking follow- +# through rather than intent. + +mode=$1 seconds=$2 comment=$3 + +log=${XDG_STATE_HOME:-$HOME/.local/state}/ttym +mkdir -p "$log" 2>/dev/null || exit 0 + +printf '%s\t%s\t%s\t%s\n' \ + "$(date +%Y-%m-%dT%H:%M:%S)" "$mode" "$seconds" "$comment" \ + >> "$log/abandoned.tsv" + +# Restore the terminal title set by on_start. +case $TERM in +xterm*|rxvt*|tmux*|screen*|alacritty|foot*) + printf '\033]0;%s\007' "${SHELL##*/}" > /dev/tty + ;; +esac diff --git a/hooks/on_start b/hooks/on_start new file mode 100755 index 0000000..441d6f1 --- /dev/null +++ b/hooks/on_start @@ -0,0 +1,26 @@ +#!/bin/sh +# ttym on_start -- runs when a session begins. +# +# $1 mode countdown | stopwatch +# $2 seconds requested duration for a countdown, 0 for a stopwatch +# $3 comment the -c text, or an empty string +# +# stdin, stdout and stderr are /dev/null: printing here goes nowhere and +# cannot corrupt the redrawn progress line. timer waits for this to exit, +# so keep it quick or background the slow part with &. + +mode=$1 seconds=$2 comment=$3 + +# Name the terminal window after what is running. +case $TERM in +xterm*|rxvt*|tmux*|screen*|alacritty|foot*) + if [ "$mode" = countdown ]; then + printf '\033]0;timer %sm %s\007' "$((seconds / 60))" "$comment" > /dev/tty + else + printf '\033]0;stopwatch %s\007' "$comment" > /dev/tty + fi + ;; +esac + +# Pause music while a countdown runs (uncomment to use): +# [ "$mode" = countdown ] && playerctl pause 2>/dev/null |
