Skip to content

Commit ba0283c

Browse files
committed
fix(ci): fix SC2004 and apply shfmt formatting to tmux_session_picker.sh
- Remove $ from array index arithmetic (SC2004: unnecessary on arithmetic vars) - Apply shfmt --indent 2 --case-indent --binary-next-line formatting
1 parent 37a2f4f commit ba0283c

1 file changed

Lines changed: 96 additions & 91 deletions

File tree

‎tmux_session_picker.sh‎

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -40,105 +40,110 @@ _TMUX_SESS_PAIRS=(
4040
_TMUX_NOTABLE_PROCS=(claude vim nvim docker node python pwsh htop)
4141

4242
# === Implementation ===
43-
if command -v tmux >/dev/null 2>&1 &&
44-
[[ $- == *i* ]] && [[ -n "$SSH_TTY" ]] && [[ -z "$TMUX" ]] && [[ -z "$NOTMUX" ]]; then
43+
if command -v tmux >/dev/null 2>&1 \
44+
&& [[ $- == *i* ]] && [[ -n "$SSH_TTY" ]] && [[ -z "$TMUX" ]] && [[ -z "$NOTMUX" ]]; then
4545

46-
# Create sessions for valid paths
47-
for _pair in "${_TMUX_SESS_PAIRS[@]}"; do
48-
_sess_name="${_pair%%=*}"
49-
_sess_path="${_pair#*=}"
50-
if [[ -d "$_sess_path" ]] && ! tmux has-session -t "$_sess_name" 2>/dev/null; then
51-
tmux new-session -d -s "$_sess_name" -c "$_sess_path"
52-
fi
53-
done
46+
# Create sessions for valid paths
47+
for _pair in "${_TMUX_SESS_PAIRS[@]}"; do
48+
_sess_name="${_pair%%=*}"
49+
_sess_path="${_pair#*=}"
50+
if [[ -d "$_sess_path" ]] && ! tmux has-session -t "$_sess_name" 2>/dev/null; then
51+
tmux new-session -d -s "$_sess_name" -c "$_sess_path"
52+
fi
53+
done
5454

55-
# Detect notable processes in a tmux session via pstree
56-
_tmux_procs() {
57-
local sid=$1
58-
local pids procs=""
59-
pids=$(tmux list-panes -t "$sid" -F '#{pane_pid}' 2>/dev/null)
60-
for _pid in $pids; do
61-
local tree
62-
tree=$(pstree -A "$_pid" 2>/dev/null) || continue
63-
for _proc in "${_TMUX_NOTABLE_PROCS[@]}"; do
64-
[[ "$tree" == *"$_proc"* ]] && procs="${procs:+$procs, }$_proc"
65-
done
66-
done
67-
printf '%s' "${procs:--}"
68-
}
55+
# Detect notable processes in a tmux session via pstree
56+
_tmux_procs() {
57+
local sid=$1
58+
local pids procs=""
59+
pids=$(tmux list-panes -t "$sid" -F '#{pane_pid}' 2>/dev/null)
60+
for _pid in $pids; do
61+
local tree
62+
tree=$(pstree -A "$_pid" 2>/dev/null) || continue
63+
for _proc in "${_TMUX_NOTABLE_PROCS[@]}"; do
64+
[[ "$tree" == *"$_proc"* ]] && procs="${procs:+$procs, }$_proc"
65+
done
66+
done
67+
printf '%s' "${procs:--}"
68+
}
6969

70-
# Relative time since last activity in a session
71-
_tmux_age() {
72-
local sid=$1
73-
local now age_s
74-
now=$(date +%s)
75-
local last
76-
last=$(tmux display-message -t "$sid" -p '#{session_activity}' 2>/dev/null) || { printf '?'; return; }
77-
age_s=$(( now - last ))
78-
if (( age_s < 60 )); then printf 'just now'
79-
elif (( age_s < 3600 )); then printf '%dm ago' "$(( age_s / 60 ))"
80-
elif (( age_s < 86400 )); then printf '%dh ago' "$(( age_s / 3600 ))"
81-
else printf '%dd ago' "$(( age_s / 86400 ))"; fi
70+
# Relative time since last activity in a session
71+
_tmux_age() {
72+
local sid=$1
73+
local now age_s
74+
now=$(date +%s)
75+
local last
76+
last=$(tmux display-message -t "$sid" -p '#{session_activity}' 2>/dev/null) || {
77+
printf '?'
78+
return
8279
}
80+
age_s=$((now - last))
81+
if ((age_s < 60)); then
82+
printf 'just now'
83+
elif ((age_s < 3600)); then
84+
printf '%dm ago' "$((age_s / 60))"
85+
elif ((age_s < 86400)); then
86+
printf '%dh ago' "$((age_s / 3600))"
87+
else printf '%dd ago' "$((age_s / 86400))"; fi
88+
}
8389

84-
# Session picker with loop (re-displays after creating new sessions)
85-
while true; do
86-
_tmux_names=()
87-
_tmux_count=0
90+
# Session picker with loop (re-displays after creating new sessions)
91+
while true; do
92+
_tmux_names=()
93+
_tmux_count=0
8894

89-
printf '\n'
90-
printf '# %-16s %s %-20s %s\n' 'SESSION' 'WIN' 'PROCESSES' 'LAST ACCESS'
91-
printf -- '--- %-16s %s %-20s %s\n' '----------------' '---' '--------------------' '-------------------'
95+
printf '\n'
96+
printf '# %-16s %s %-20s %s\n' 'SESSION' 'WIN' 'PROCESSES' 'LAST ACCESS'
97+
printf -- '--- %-16s %s %-20s %s\n' '----------------' '---' '--------------------' '-------------------'
9298

93-
while IFS='|' read -r _sname _swins _satt; do
94-
(( _tmux_count++ ))
95-
_tmux_names[$_tmux_count]="$_sname"
96-
_label="${_sname}${_satt:+ *}"
97-
_procs=$(_tmux_procs "$_sname")
98-
_age=$(_tmux_age "$_sname")
99-
printf '%-4s %-16s %3s %-20s %s\n' "${_tmux_count})" "$_label" "$_swins" "$_procs" "$_age"
100-
done < <(tmux list-sessions -F '#{session_name}|#{session_windows}|#{?session_attached,*,}' 2>/dev/null)
99+
while IFS='|' read -r _sname _swins _satt; do
100+
((_tmux_count++))
101+
_tmux_names[_tmux_count]="$_sname"
102+
_label="${_sname}${_satt:+ *}"
103+
_procs=$(_tmux_procs "$_sname")
104+
_age=$(_tmux_age "$_sname")
105+
printf '%-4s %-16s %3s %-20s %s\n' "${_tmux_count})" "$_label" "$_swins" "$_procs" "$_age"
106+
done < <(tmux list-sessions -F '#{session_name}|#{session_windows}|#{?session_attached,*,}' 2>/dev/null)
101107

102-
printf '\n[1-%d] attach [n] new [s] skip\n' "$_tmux_count"
103-
printf '> '
104-
read -r _choice
108+
printf '\n[1-%d] attach [n] new [s] skip\n' "$_tmux_count"
109+
printf '> '
110+
read -r _choice
105111

106-
case "$_choice" in
107-
s|skip)
108-
break
109-
;;
110-
n|new)
111-
printf 'Session name: '
112-
read -r _new_name
113-
printf 'Start directory: '
114-
read -r _new_path
115-
_new_path="${_new_path:-$HOME}"
116-
if [[ -n "$_new_name" ]] && [[ -d "$_new_path" ]]; then
117-
tmux new-session -d -s "$_new_name" -c "$_new_path"
118-
else
119-
printf 'Invalid name or path.\n' >&2
120-
continue
121-
fi
122-
;;
123-
[0-9]*)
124-
if (( _choice >= 1 && _choice <= _tmux_count )); then
125-
exec tmux attach -t "${_tmux_names[_choice]}"
126-
else
127-
printf 'Invalid selection.\n' >&2
128-
fi
129-
;;
130-
"")
131-
;;
132-
*)
133-
# Try as session name directly
134-
if tmux has-session -t "$_choice" 2>/dev/null; then
135-
exec tmux attach -t "$_choice"
136-
else
137-
printf 'Unknown session: %s\n' "$_choice" >&2
138-
fi
139-
;;
140-
esac
141-
done
112+
case "$_choice" in
113+
s | skip)
114+
break
115+
;;
116+
n | new)
117+
printf 'Session name: '
118+
read -r _new_name
119+
printf 'Start directory: '
120+
read -r _new_path
121+
_new_path="${_new_path:-$HOME}"
122+
if [[ -n "$_new_name" ]] && [[ -d "$_new_path" ]]; then
123+
tmux new-session -d -s "$_new_name" -c "$_new_path"
124+
else
125+
printf 'Invalid name or path.\n' >&2
126+
continue
127+
fi
128+
;;
129+
[0-9]*)
130+
if ((_choice >= 1 && _choice <= _tmux_count)); then
131+
exec tmux attach -t "${_tmux_names[_choice]}"
132+
else
133+
printf 'Invalid selection.\n' >&2
134+
fi
135+
;;
136+
"") ;;
137+
*)
138+
# Try as session name directly
139+
if tmux has-session -t "$_choice" 2>/dev/null; then
140+
exec tmux attach -t "$_choice"
141+
else
142+
printf 'Unknown session: %s\n' "$_choice" >&2
143+
fi
144+
;;
145+
esac
146+
done
142147

143-
unset _pair _sess_name _sess_path _tmux_names _tmux_count _sname _swins _satt _choice _new_name _new_path _label _procs _age _pid _proc
148+
unset _pair _sess_name _sess_path _tmux_names _tmux_count _sname _swins _satt _choice _new_name _new_path _label _procs _age _pid _proc
144149
fi

0 commit comments

Comments
 (0)