aboutsummaryrefslogtreecommitdiff
path: root/hooks/on_quit
blob: 44957e8d579ec1803813283c0e3d8b7f295f008d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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