diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-08-25 19:35:46 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-08-25 19:35:46 +0200 |
| commit | 8543890f2e90af75a9bed7fa692ac92fc43285a4 (patch) | |
| tree | a1649e7a14b951e84f9141904b2e5ed5bf9f6094 /hooks/on_quit | |
| parent | f8a687cbb855ed4e3aa54128624101a21ba5a8c3 (diff) | |
| download | ttym-8543890f2e90af75a9bed7fa692ac92fc43285a4.tar.gz ttym-8543890f2e90af75a9bed7fa692ac92fc43285a4.zip | |
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
Diffstat (limited to 'hooks/on_quit')
| -rwxr-xr-x | hooks/on_quit | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/hooks/on_quit b/hooks/on_quit index 44957e8..f89f6cf 100755 --- a/hooks/on_quit +++ b/hooks/on_quit @@ -7,22 +7,30 @@ # $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. +# $2 is what really ran, not what you asked for: a 25m countdown +# abandoned after four minutes reports 240. Compare against the START +# line written by on_start to see follow-through. +# +# If your build has the logging patch, `timer -r` already records this +# session -- it logs every run, completed or not. What it cannot show is +# that the run was cut short, which is what the record below adds. mode=$1 seconds=$2 comment=$3 -log=${XDG_STATE_HOME:-$HOME/.local/state}/ttym -mkdir -p "$log" 2>/dev/null || exit 0 +# --- session record ------------------------------------------------------ +state=${XDG_STATE_HOME:-$HOME/.local/state}/ttym +if mkdir -p "$state" 2>/dev/null; then + printf '%s\tQUIT\t%s\t%s\t%s\n' \ + "$(date +%Y-%m-%dT%H:%M:%S)" "$mode" "$seconds" "$comment" \ + >> "$state/sessions.tsv" +fi + +# --- leave focus mode ---------------------------------------------------- +command -v dunstctl > /dev/null 2>&1 && dunstctl set-paused false +[ "$mode" = countdown ] && command -v playerctl > /dev/null 2>&1 && playerctl play 2>/dev/null +[ "$mode" = countdown ] && command -v mpc > /dev/null 2>&1 && mpc -q play 2>/dev/null -printf '%s\t%s\t%s\t%s\n' \ - "$(date +%Y-%m-%dT%H:%M:%S)" "$mode" "$seconds" "$comment" \ - >> "$log/abandoned.tsv" +# --- timewarrior --------------------------------------------------------- +# command -v timew > /dev/null 2>&1 && timew stop > /dev/null 2>&1 -# 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 +exit 0 |
