From 8543890f2e90af75a9bed7fa692ac92fc43285a4 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Tue, 25 Aug 2026 19:35:46 +0200 Subject: hooks: replace the examples with ones that do what the binary cannot The previous examples largely duplicated features of the patched build. on_done sent a desktop notification and played a sound, both of which the notify patch already does, so a user who copied it got two of each. on_quit recorded abandoned sessions, but the logging patch calls write_log() unconditionally at the end of every run: abandoned sessions were already in the log behind timer -r. The new set does two things nothing else can: - Intent versus outcome. The log stores elapsed seconds only, so a finished 25m and an abandoned 30m that ran 25m are indistinguishable in it. The duration actually requested is visible to on_start and nowhere else, so the examples write START/DONE/QUIT records to $XDG_STATE_HOME/ttym/sessions.tsv. - Focus mode. During a countdown, pause dunst notifications and any playerctl/mpc playback; restore on done or quit. Countdown only, so a stopwatch cannot silence notifications indefinitely. A timewarrior bridge is included but commented out. Two behaviours documented in the file comments, both read off the code rather than assumed: notify_and_sound() runs before the on_done hook, so with dunst paused the completion popup is queued and appears when the hook unpauses -- delayed, not lost; and nothing restores dunst if the process is killed with -9. Every external command is guarded with command -v, so the examples degrade to the session record alone on a machine without those tools. Each hook works standalone -- the man page and README now say so explicitly, since installing only on_quit is a normal setup. All three pass sh -n and shellcheck, and were tested across all three paths: completed countdown, abandoned countdown, stopped stopwatch, with dunstctl is-paused confirmed false afterwards. Claude-Session: https://claude.ai/code/session_01APLBs8RB1FcUaC4viVkzbP --- hooks/on_done | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'hooks/on_done') diff --git a/hooks/on_done b/hooks/on_done index 955cd13..cfe57f2 100755 --- a/hooks/on_done +++ b/hooks/on_done @@ -1,31 +1,36 @@ #!/bin/sh # ttym on_done -- runs when a countdown reaches zero. # -# $1 mode always "countdown" (a stopwatch never completes) +# $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. +# Nothing here duplicates the notify patch: if your build has it, the +# desktop notification and sound have already fired by the time this +# runs. Use this for what the binary does not do. +# +# timer waits for this before the completion bell and the alert loop, so +# background anything slow with &. -seconds=$2 comment=$3 -mins=$((seconds / 60)) +mode=$1 seconds=$2 comment=$3 -# 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}" & +# --- session record ------------------------------------------------------ +state=${XDG_STATE_HOME:-$HOME/.local/state}/ttym +if mkdir -p "$state" 2>/dev/null; then + printf '%s\tDONE\t%s\t%s\t%s\n' \ + "$(date +%Y-%m-%dT%H:%M:%S)" "$mode" "$seconds" "$comment" \ + >> "$state/sessions.tsv" 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 +# --- leave focus mode ---------------------------------------------------- +# Note the ordering: the notify patch emits its completion popup *before* +# this hook runs, so with dunst paused that popup is queued and appears +# the moment the next line unpauses. Delayed, not lost. +command -v dunstctl > /dev/null 2>&1 && dunstctl set-paused false +command -v playerctl > /dev/null 2>&1 && playerctl play 2>/dev/null +command -v mpc > /dev/null 2>&1 && mpc -q play 2>/dev/null -# Push to your phone (uncomment and set your topic): -# curl -fsS -d "${mins}m done${comment:+ -- $comment}" ntfy.sh/your-topic & +# --- timewarrior --------------------------------------------------------- +# command -v timew > /dev/null 2>&1 && timew stop > /dev/null 2>&1 -wait +exit 0 -- cgit v1.3