1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-16 06:01:12 +00:00

fix(ui): stop treating space as search filter in paginated menu

This commit is contained in:
tw93
2026-02-05 19:50:16 +08:00
parent a0d5b476d3
commit 30777dafa3

View File

@@ -5,13 +5,13 @@ set -euo pipefail
# Terminal control functions # Terminal control functions
enter_alt_screen() { enter_alt_screen() {
if command -v tput > /dev/null 2>&1 && [[ -t 1 ]]; then if command -v tput >/dev/null 2>&1 && [[ -t 1 ]]; then
tput smcup 2> /dev/null || true tput smcup 2>/dev/null || true
fi fi
} }
leave_alt_screen() { leave_alt_screen() {
if command -v tput > /dev/null 2>&1 && [[ -t 1 ]]; then if command -v tput >/dev/null 2>&1 && [[ -t 1 ]]; then
tput rmcup 2> /dev/null || true tput rmcup 2>/dev/null || true
fi fi
} }
@@ -22,13 +22,13 @@ _pm_get_terminal_height() {
# Try stty size first (most reliable, real-time) # Try stty size first (most reliable, real-time)
# Use </dev/tty to ensure we read from terminal even if stdin is redirected # Use </dev/tty to ensure we read from terminal even if stdin is redirected
if [[ -t 0 ]] || [[ -t 2 ]]; then if [[ -t 0 ]] || [[ -t 2 ]]; then
height=$(stty size < /dev/tty 2> /dev/null | awk '{print $1}') height=$(stty size </dev/tty 2>/dev/null | awk '{print $1}')
fi fi
# Fallback to tput # Fallback to tput
if [[ -z "$height" || $height -le 0 ]]; then if [[ -z "$height" || $height -le 0 ]]; then
if command -v tput > /dev/null 2>&1; then if command -v tput >/dev/null 2>&1; then
height=$(tput lines 2> /dev/null || echo "24") height=$(tput lines 2>/dev/null || echo "24")
else else
height=24 height=24
fi fi
@@ -109,7 +109,7 @@ paginated_multi_select() {
has_metadata="true" has_metadata="true"
fi fi
if [[ -n "${MOLE_MENU_FILTER_NAMES:-}" ]]; then if [[ -n "${MOLE_MENU_FILTER_NAMES:-}" ]]; then
while IFS= read -r v; do filter_names+=("$v"); done <<< "$MOLE_MENU_FILTER_NAMES" while IFS= read -r v; do filter_names+=("$v"); done <<<"$MOLE_MENU_FILTER_NAMES"
has_filter_names="true" has_filter_names="true"
fi fi
@@ -138,7 +138,7 @@ paginated_multi_select() {
if [[ -n "${MOLE_PRESELECTED_INDICES:-}" ]]; then if [[ -n "${MOLE_PRESELECTED_INDICES:-}" ]]; then
local cleaned_preselect="${MOLE_PRESELECTED_INDICES//[[:space:]]/}" local cleaned_preselect="${MOLE_PRESELECTED_INDICES//[[:space:]]/}"
local -a initial_indices=() local -a initial_indices=()
IFS=',' read -ra initial_indices <<< "$cleaned_preselect" IFS=',' read -ra initial_indices <<<"$cleaned_preselect"
for idx in "${initial_indices[@]}"; do for idx in "${initial_indices[@]}"; do
if [[ "$idx" =~ ^[0-9]+$ && $idx -ge 0 && $idx -lt $total_items ]]; then if [[ "$idx" =~ ^[0-9]+$ && $idx -ge 0 && $idx -lt $total_items ]]; then
# Only count if not already selected (handles duplicates) # Only count if not already selected (handles duplicates)
@@ -152,16 +152,16 @@ paginated_multi_select() {
# Preserve original TTY settings so we can restore them reliably # Preserve original TTY settings so we can restore them reliably
local original_stty="" local original_stty=""
if [[ -t 0 ]] && command -v stty > /dev/null 2>&1; then if [[ -t 0 ]] && command -v stty >/dev/null 2>&1; then
original_stty=$(stty -g 2> /dev/null || echo "") original_stty=$(stty -g 2>/dev/null || echo "")
fi fi
restore_terminal() { restore_terminal() {
show_cursor show_cursor
if [[ -n "${original_stty-}" ]]; then if [[ -n "${original_stty-}" ]]; then
stty "${original_stty}" 2> /dev/null || stty sane 2> /dev/null || stty echo icanon 2> /dev/null || true stty "${original_stty}" 2>/dev/null || stty sane 2>/dev/null || stty echo icanon 2>/dev/null || true
else else
stty sane 2> /dev/null || stty echo icanon 2> /dev/null || true stty sane 2>/dev/null || stty echo icanon 2>/dev/null || true
fi fi
if [[ "${external_alt_screen:-false}" == false ]]; then if [[ "${external_alt_screen:-false}" == false ]]; then
leave_alt_screen leave_alt_screen
@@ -187,7 +187,7 @@ paginated_multi_select() {
trap handle_interrupt INT TERM trap handle_interrupt INT TERM
# Setup terminal - preserve interrupt character # Setup terminal - preserve interrupt character
stty -echo -icanon intr ^C 2> /dev/null || true stty -echo -icanon intr ^C 2>/dev/null || true
if [[ $external_alt_screen == false ]]; then if [[ $external_alt_screen == false ]]; then
enter_alt_screen enter_alt_screen
# Clear screen once on entry to alt screen # Clear screen once on entry to alt screen
@@ -208,7 +208,7 @@ paginated_multi_select() {
local -a segs=("$@") local -a segs=("$@")
local cols="${COLUMNS:-}" local cols="${COLUMNS:-}"
[[ -z "$cols" ]] && cols=$(tput cols 2> /dev/null || echo 80) [[ -z "$cols" ]] && cols=$(tput cols 2>/dev/null || echo 80)
[[ "$cols" =~ ^[0-9]+$ ]] || cols=80 [[ "$cols" =~ ^[0-9]+$ ]] || cols=80
_strip_ansi_len() { _strip_ansi_len() {
@@ -287,7 +287,7 @@ paginated_multi_select() {
# Create temporary file for sorting # Create temporary file for sorting
local tmpfile local tmpfile
tmpfile=$(mktemp 2> /dev/null) || tmpfile="" tmpfile=$(mktemp 2>/dev/null) || tmpfile=""
if [[ -n "$tmpfile" ]]; then if [[ -n "$tmpfile" ]]; then
local k id local k id
for id in "${active_indices[@]}"; do for id in "${active_indices[@]}"; do
@@ -296,14 +296,14 @@ paginated_multi_select() {
size) k="${sizekb[id]:-0}" ;; size) k="${sizekb[id]:-0}" ;;
name | *) k="${items[id]}|${id}" ;; name | *) k="${items[id]}|${id}" ;;
esac esac
printf "%s\t%s\n" "$k" "$id" >> "$tmpfile" printf "%s\t%s\n" "$k" "$id" >>"$tmpfile"
done done
view_indices=() view_indices=()
while IFS=$'\t' read -r _key _id; do while IFS=$'\t' read -r _key _id; do
[[ -z "$_id" ]] && continue [[ -z "$_id" ]] && continue
view_indices+=("$_id") view_indices+=("$_id")
done < <(LC_ALL=C sort -t $'\t' $sort_key -- "$tmpfile" 2> /dev/null) done < <(LC_ALL=C sort -t $'\t' $sort_key -- "$tmpfile" 2>/dev/null)
rm -f "$tmpfile" rm -f "$tmpfile"
else else
@@ -469,7 +469,7 @@ paginated_multi_select() {
elif [[ "$has_metadata" == "true" ]]; then elif [[ "$has_metadata" == "true" ]]; then
# With metadata: show sort controls # With metadata: show sort controls
local term_width="${COLUMNS:-}" local term_width="${COLUMNS:-}"
[[ -z "$term_width" ]] && term_width=$(tput cols 2> /dev/null || echo 80) [[ -z "$term_width" ]] && term_width=$(tput cols 2>/dev/null || echo 80)
[[ "$term_width" =~ ^[0-9]+$ ]] || term_width=80 [[ "$term_width" =~ ^[0-9]+$ ]] || term_width=80
# Full controls # Full controls
@@ -657,14 +657,6 @@ paginated_multi_select() {
fi fi
;; ;;
"SPACE") "SPACE")
# In filter mode with active text, treat space as search character
if [[ -n "$filter_text" ]]; then
filter_text+=" "
rebuild_view
cursor_pos=0
need_full_redraw=true
continue
fi
local idx=$((top_index + cursor_pos)) local idx=$((top_index + cursor_pos))
if [[ $idx -lt ${#view_indices[@]} ]]; then if [[ $idx -lt ${#view_indices[@]} ]]; then
local real="${view_indices[idx]}" local real="${view_indices[idx]}"