diff options
Diffstat (limited to 'tmuxrdr/plugin/bin/track-focus')
| -rwxr-xr-x | tmuxrdr/plugin/bin/track-focus | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tmuxrdr/plugin/bin/track-focus b/tmuxrdr/plugin/bin/track-focus new file mode 100755 index 00000000..4132891f --- /dev/null +++ b/tmuxrdr/plugin/bin/track-focus @@ -0,0 +1,36 @@ +#!/bin/sh +# Event hook for tab.focused and workspace.focused: keeps the current and +# previous tab of each workspace, and the current and previous workspace, so +# `last window` and `last session` know where to go. Hooks run concurrently, +# so updates take a mkdir lock. +. "$(dirname "$0")/../lib.sh" + +dir="$(state_dir)" +ev="${HERDR_PLUGIN_EVENT_JSON:-}" +[ -n "$ev" ] || exit 0 + +field() { + printf '%s' "$ev" | jq -r --arg k "$1" '[.. | objects | .[$k]? | strings][0] // empty' +} + +# record NAME VALUE: VALUE becomes NAME.current, the old one NAME.previous. +record() { + [ -n "$2" ] || return 0 + cur="$(cat "$dir/$1.current" 2>/dev/null || true)" + [ "$cur" = "$2" ] && return 0 + [ -n "$cur" ] && printf '%s\n' "$cur" >"$dir/$1.previous" + printf '%s\n' "$2" >"$dir/$1.current" +} + +tries=0 +until mkdir "$dir/.lock" 2>/dev/null; do + tries=$((tries + 1)) + [ "$tries" -gt 40 ] && break + sleep 0.05 +done +trap 'rmdir "$dir/.lock" 2>/dev/null' EXIT + +case "${HERDR_PLUGIN_EVENT:-}" in +tab.focused) record "tab.$(key_of "$(field workspace_id)")" "$(field tab_id)" ;; +workspace.focused) record workspace "$(field workspace_id)" ;; +esac |
