@@ -18,44 +18,49 @@ ssh() {
1818 # Open TTY; bail quietly if it fails
1919 exec {fd}<> /dev/tty || return 0
2020
21- # Raw-ish mode so reply isn't echoed/line-buffered
22- stty_orig=$( stty -g < & $fd )
23- stty -echo -icanon time 0 min 0 < & $fd
24-
25- # Ask: terminal replies DCS "1$r q<Ps> q" ST (success)
26- printf ' %s' " $DECRQSS " > /dev/tty
27-
28- buf=' '
29- # Read until we see ST (ESC \) anywhere in the buffer (cap ~2s)
30- for i in {1..100}; do
31- IFS= read -r -u $fd -k 1 -t 0.01 chunk || chunk=' '
32- [[ -n " $chunk " ]] && buf+=" $chunk "
33- [[ $buf == * " $ST " * ]] && break
34- done
35-
36- # Restore TTY and close FD
37- stty " $stty_orig " < & $fd
38- : {fd}>& -
39-
40- # Extract first DCS block and require DECRQSS success "1$r"
41- [[ $buf == * " $DCS " * " $ST " * ]] || return 0
42- dcs_reply=${buf#* " $DCS " }
43- dcs_reply=${dcs_reply%% " $ST " * }
44-
45- # Must be a success reply
46- [[ $dcs_reply == 1' $r' * ]] || return 0
47-
48- ps=' '
49- if [[ $dcs_reply == ' 1$r q' [0-6]' q' * ]]; then
50- ps=" ${dcs_reply# ' 1$r q' } "
51- ps=" ${ps%% ' q' * } "
52- fi
53-
54- # Validate 0..6 and print
55- if [[ $ps = < -> && $ps -ge 0 && $ps -le 6 ]]; then
56- print -r -- " $ps "
57- fi
58- return 0
21+ { # --- try block ---
22+
23+ # Raw-ish mode so reply isn't echoed/line-buffered
24+ stty_orig=$( stty -g < & $fd )
25+ stty -echo -icanon time 0 min 0 < & $fd
26+
27+ # Ask: terminal replies DCS "1$r q<Ps> q" ST (success)
28+ printf ' %s' " $DECRQSS " > /dev/tty
29+
30+ buf=' '
31+ # Read until we see ST (ESC \) anywhere in the buffer (cap ~2s)
32+ for i in {1..100}; do
33+ IFS= read -r -u $fd -k 1 -t 0.01 chunk || chunk=' '
34+ [[ -n " $chunk " ]] && buf+=" $chunk "
35+ [[ $buf == * " $ST " * ]] && break
36+ done
37+
38+ # Extract first DCS block and require DECRQSS success "1$r"
39+ [[ $buf == * " $DCS " * " $ST " * ]] || return 0
40+ dcs_reply=${buf#* " $DCS " }
41+ dcs_reply=${dcs_reply%% " $ST " * }
42+
43+ # Must be a success reply
44+ [[ $dcs_reply == 1' $r' * ]] || return 0
45+
46+ ps=' '
47+ if [[ $dcs_reply == ' 1$r q' [0-6]' q' * ]]; then
48+ ps=" ${dcs_reply# ' 1$r q' } "
49+ ps=" ${ps%% ' q' * } "
50+ fi
51+
52+ # Validate 0..6 and print
53+ if [[ $ps = < -> && $ps -ge 0 && $ps -le 6 ]]; then
54+ print -r -- " $ps "
55+ fi
56+ return 0
57+ } always {
58+ # --- always cleanup, even on early return ---
59+
60+ # Restore TTY and close FD
61+ stty " $stty_orig " < & $fd 2> /dev/null
62+ : {fd}>& -
63+ }
5964 }
6065
6166 # Map tmux's cursor-style option to DECSCUSR Ps
@@ -164,9 +169,6 @@ ssh() {
164169 command tmux set-option -t " $current_pane_id " -wq " @original-cursor-style" " $cursor_style "
165170 fi
166171
167- # Remember for restore-on-exit
168- command tmux set-option -t " $current_pane_id " -wq " @original-cursor-style" " $cursor_style "
169-
170172 # Compute remote host and update window name
171173 local remote_host_name
172174 remote_host_name=$( command ssh -G " $@ " 2> /dev/null | awk ' /^host / {print $2}' 2> /dev/null )
0 commit comments