fix(config-toggle): use read -k 3 for ESC sequences, clear INT trap on exit
Replace two-read-k-1 ESC handling with a single read -k 3 so that a bare ESC returns immediately instead of blocking the shell indefinitely waiting for a second byte. Add trap - INT in the cleanup block so the signal handler does not leak into the global session after config-toggle exits.
This commit is contained in:
@@ -81,40 +81,30 @@ function config-toggle --description 'Interactive TUI for toggling opinionated c
|
|||||||
__config_toggle_draw $cur_row $cur_scope $vars
|
__config_toggle_draw $cur_row $cur_scope $vars
|
||||||
|
|
||||||
# ── Event loop ────────────────────────────────────────
|
# ── Event loop ────────────────────────────────────────
|
||||||
|
# read -k 3 reads UP TO 3 chars and returns immediately when input is
|
||||||
|
# exhausted — it does not block waiting for exactly 3 chars. A bare ESC
|
||||||
|
# sends 1 byte and read -k 3 returns at once with a 1-char string.
|
||||||
|
# Arrow keys send the 3-byte sequence ESC [ A/B, matched as \e\[A / \e\[B.
|
||||||
while true
|
while true
|
||||||
read -k 1 -l key
|
read -k 3 -l key
|
||||||
|
|
||||||
switch $key
|
switch $key
|
||||||
case \e
|
case \e\[A # Up arrow (ESC [ A)
|
||||||
# Arrow key: ESC [ A/B — read next 2 chars one at a time.
|
set cur_row (math "max(0, $cur_row - 1)")
|
||||||
# Terminal sends escape sequences as an atomic burst, so
|
case \e\[B # Down arrow (ESC [ B)
|
||||||
# read -k 1 returns each char immediately.
|
set cur_row (math "min(6, $cur_row + 1)")
|
||||||
# If the char after ESC is not '[', treat as bare Escape → quit.
|
case \e # Bare Escape (1 byte) — exit
|
||||||
read -k 1 -l ch1
|
break
|
||||||
if test "$ch1" != '['
|
|
||||||
# Bare Escape (or unrecognised sequence) — exit
|
|
||||||
break
|
|
||||||
end
|
|
||||||
read -k 1 -l ch2
|
|
||||||
switch $ch2
|
|
||||||
case A # Up arrow
|
|
||||||
set cur_row (math "max(0, $cur_row - 1)")
|
|
||||||
case B # Down arrow
|
|
||||||
set cur_row (math "min(6, $cur_row + 1)")
|
|
||||||
end
|
|
||||||
|
|
||||||
case k
|
case k
|
||||||
set cur_row (math "max(0, $cur_row - 1)")
|
set cur_row (math "max(0, $cur_row - 1)")
|
||||||
case j
|
case j
|
||||||
set cur_row (math "min(6, $cur_row + 1)")
|
set cur_row (math "min(6, $cur_row + 1)")
|
||||||
|
case \t # Tab — switch scope
|
||||||
case \t # Tab — switch scope
|
|
||||||
if test $cur_scope = universal
|
if test $cur_scope = universal
|
||||||
set cur_scope session
|
set cur_scope session
|
||||||
else
|
else
|
||||||
set cur_scope universal
|
set cur_scope universal
|
||||||
end
|
end
|
||||||
|
|
||||||
case q Q
|
case q Q
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -125,5 +115,6 @@ function config-toggle --description 'Interactive TUI for toggling opinionated c
|
|||||||
end
|
end
|
||||||
|
|
||||||
# ── Cleanup ───────────────────────────────────────────
|
# ── Cleanup ───────────────────────────────────────────
|
||||||
printf '\e[?25h' # restore cursor
|
trap - INT # remove the signal handler
|
||||||
|
printf '\e[?25h' # restore cursor
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user