mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 17:24:45 +00:00
🐛 Restoring arrow logic
This commit is contained in:
@@ -11,7 +11,6 @@ readonly BLUE="${ESC}[0;34m"
|
|||||||
readonly YELLOW="${ESC}[1;33m"
|
readonly YELLOW="${ESC}[1;33m"
|
||||||
readonly PURPLE="${ESC}[0;35m"
|
readonly PURPLE="${ESC}[0;35m"
|
||||||
readonly RED="${ESC}[0;31m"
|
readonly RED="${ESC}[0;31m"
|
||||||
readonly GRAY="${ESC}[0;90m"
|
|
||||||
readonly NC="${ESC}[0m"
|
readonly NC="${ESC}[0m"
|
||||||
|
|
||||||
# Logging configuration
|
# Logging configuration
|
||||||
@@ -89,51 +88,41 @@ show_cursor() {
|
|||||||
|
|
||||||
# Keyboard input handling (simple and robust)
|
# Keyboard input handling (simple and robust)
|
||||||
read_key() {
|
read_key() {
|
||||||
# Robust parser that accumulates pending bytes to avoid leaking raw 'A'/'B'
|
local key rest
|
||||||
local first rest
|
# Use macOS bash 3.2 compatible read syntax
|
||||||
IFS= read -r -s -n 1 first || return 1
|
IFS= read -r -s -n 1 key || return 1
|
||||||
|
|
||||||
# Enter (some terminals send empty before newline in -n1 mode)
|
# Some terminals can yield empty on Enter with -n1; treat as ENTER
|
||||||
if [[ -z "$first" || "$first" == $'\n' || "$first" == $'\r' ]]; then
|
if [[ -z "$key" ]]; then
|
||||||
echo "ENTER"; return 0
|
echo "ENTER"
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
case "$first" in
|
case "$key" in
|
||||||
' ') echo "SPACE"; return 0 ;;
|
$'\n'|$'\r') echo "ENTER" ;;
|
||||||
'q'|'Q') echo "QUIT"; return 0 ;;
|
' ') echo "SPACE" ;;
|
||||||
'a'|'A') echo "ALL"; return 0 ;;
|
'q'|'Q') echo "QUIT" ;;
|
||||||
'n'|'N') echo "NONE"; return 0 ;;
|
'a'|'A') echo "ALL" ;;
|
||||||
'?') echo "HELP"; return 0 ;;
|
'n'|'N') echo "NONE" ;;
|
||||||
$'\x7f'|$'\b') echo "BACKSPACE"; return 0 ;;
|
'?') echo "HELP" ;;
|
||||||
$'\x1b')
|
$'\x1b')
|
||||||
# Collect rest of possible escape sequence quickly (non-blocking)
|
# Read the next two bytes within 1s; works well on macOS bash 3.2
|
||||||
local buf=""
|
if IFS= read -r -s -n 2 -t 1 rest 2>/dev/null; then
|
||||||
local count=0
|
case "$rest" in
|
||||||
while IFS= read -r -s -n 1 -t 0.005 rest 2>/dev/null; do
|
"[A") echo "UP" ;;
|
||||||
buf+="$rest"; ((count++))
|
"[B") echo "DOWN" ;;
|
||||||
# Stop if final byte of a simple CSI seq
|
"[C") echo "RIGHT" ;;
|
||||||
[[ "$rest" =~ [A-Za-z~] ]] && break
|
"[D") echo "LEFT" ;;
|
||||||
[[ $count -ge 5 ]] && break
|
*) echo "OTHER" ;;
|
||||||
done
|
esac
|
||||||
case "$buf" in
|
else
|
||||||
"[A") echo "UP" ;;
|
# ESC pressed alone - treat as quit
|
||||||
"[B") echo "DOWN" ;;
|
echo "QUIT"
|
||||||
"[C") echo "RIGHT" ;;
|
fi
|
||||||
"[D") echo "LEFT" ;;
|
|
||||||
"") echo "OTHER" ;; # Bare ESC -> ignore
|
|
||||||
*) echo "OTHER" ;;
|
|
||||||
esac
|
|
||||||
return 0
|
|
||||||
;;
|
;;
|
||||||
|
*) echo "OTHER" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
echo "OTHER"
|
|
||||||
}
|
}
|
||||||
# Drain any pending input bytes (used to swallow rapid trackpad scroll sequences)
|
|
||||||
drain_pending_input() {
|
|
||||||
while IFS= read -r -s -t 0 -n 1 _; do :; done
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Menu display helper
|
# Menu display helper
|
||||||
show_menu_option() {
|
show_menu_option() {
|
||||||
|
|||||||
@@ -116,8 +116,6 @@ EOF
|
|||||||
|
|
||||||
# Main interaction loop
|
# Main interaction loop
|
||||||
while true; do
|
while true; do
|
||||||
# Drain any burst scroll input so only one navigation step occurs
|
|
||||||
type drain_pending_input >/dev/null 2>&1 && drain_pending_input
|
|
||||||
draw_menu
|
draw_menu
|
||||||
local key=$(read_key)
|
local key=$(read_key)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user