dotfiles/dotlib/post/cattle.sh

53 lines
1 KiB
Bash

cattle-reset-elapsed() {
(( last_cmd_at = SECONDS ))
}
cattle-increase-timeout() {
(( timeout += 3600 ))
}
cattle-ping-timer() {
kill -USR1 "$reaper_pid"
}
cattle-ping-reaper() {
kill -USR2 "$reaper_pid"
}
cattle-slay-reaper() {
kill -KILL "$reaper_pid"
}
cd() {
command cd "$@"
printf '\033]1337;CurrentDir=%s\007' "$PWD"
printf %s "$PWD" > /tmp/iterm2-prev-dir.txt
}
{
trap true EXIT # A permanently temporary workaround, since ^C outside of a
# command appears to propagate to the child process.
trap cattle-reset-elapsed USR1
trap cattle-increase-timeout USR2
last_cmd_at=0 timeout=3600
while sleep 10; do
(( elapsed = SECONDS - last_cmd_at ))
if (( elapsed > timeout )) && ! compgen -e | grep -Fx STY; then
kill -KILL "$$"
exit
fi
done
} 2> /dev/null &
reaper_pid=$! PROMPT_COMMAND+=(cattle-ping-timer)
trap cattle-slay-reaper EXIT
if [[ -f /tmp/iterm2-prev-dir.txt ]]; then
dir=$(</tmp/iterm2-prev-dir.txt)
if [[ -d $dir ]]; then
command cd "$dir"
fi
fi