#!/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
