-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmux-pane-titles
More file actions
executable file
·53 lines (48 loc) · 1.74 KB
/
tmux-pane-titles
File metadata and controls
executable file
·53 lines (48 loc) · 1.74 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/zsh
#
# tmux-pane-titles
#
# Collect unique directory basenames from all panes in the current tmux window
# and set as the window name. Called by tmux hooks and zsh chpwd.
#
target_window=""
target_args=()
should_debounce=false
# Background tmux hooks pass a window id explicitly. This avoids relying on the
# current client/window at execution time, which may already have changed.
# Debounce: when called with --debounce (e.g. from window-layout-changed hook),
# wait briefly and skip if a newer invocation has started. This prevents redundant
# runs during rapid pane closes or resize operations.
if [[ "$1" == "--debounce" ]]; then
should_debounce=true
shift
fi
target_window="$1"
if [[ -n "$target_window" ]]; then
target_args=(-t "$target_window")
fi
if $should_debounce; then
_debounce_dir="/tmp/tmux-pane-titles"
mkdir -p "$_debounce_dir" 2>/dev/null
_win_id=$(tmux display-message -p "${target_args[@]}" '#{session_id}-#{window_id}' 2>/dev/null) || exit 0
_stamp="$$"
echo "$_stamp" > "$_debounce_dir/$_win_id" 2>/dev/null
sleep 0.15
[[ "$(cat "$_debounce_dir/$_win_id" 2>/dev/null)" == "$_stamp" ]] || exit 0
fi
panes=$(tmux list-panes "${target_args[@]}" -F '#{b:pane_current_path} #{@ssh_host} #{@git_worktree} #{@git_branch}' 2>/dev/null) || exit 0
title=$(printf '%s\n' "$panes" | awk -F'\t' '{
dir = $1
ssh = $2
wt = $3
br = $4
host = ssh; sub(/^[^@]*@/, "", host)
if (length(host) > 10) host = substr(host, 1, 10) "..."
if (ssh != "") label = "ssh:" host
else if (wt != "") label = "wt:" wt
else if (br != "") label = "b:" br
else label = dir
if (!seen[label]++) { printf sep label; sep = "," }
}')
[[ -n "$title" ]] && tmux rename-window "${target_args[@]}" -- "$title" 2>/dev/null
exit 0