1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 19:44:44 +00:00

ui: add menu filtering support

This commit is contained in:
tw93
2026-02-02 17:05:19 +08:00
parent e8f46a7a1c
commit aaa3a6ae5a
4 changed files with 155 additions and 729 deletions

View File

@@ -111,15 +111,13 @@ select_apps_for_uninstall() {
[[ $max_name_width -gt 60 ]] && max_name_width=60
local -a menu_options=()
# Prepare metadata (comma-separated) for sorting/filtering inside the menu
local epochs_csv=""
local sizekb_csv=""
local -a names_arr=()
local idx=0
for app_data in "${apps_data[@]}"; do
# Keep extended field 7 (size_kb) if present
IFS='|' read -r epoch _ display_name _ size last_used size_kb <<< "$app_data"
menu_options+=("$(format_app_display "$display_name" "$size" "$last_used" "$terminal_width" "$max_name_width")")
# Build csv lists (avoid trailing commas)
if [[ $idx -eq 0 ]]; then
epochs_csv="${epoch:-0}"
sizekb_csv="${size_kb:-0}"
@@ -127,8 +125,12 @@ select_apps_for_uninstall() {
epochs_csv+=",${epoch:-0}"
sizekb_csv+=",${size_kb:-0}"
fi
names_arr+=("$display_name")
((idx++))
done
# Use newline separator for names (safe for names with commas)
local names_newline
names_newline=$(printf '%s\n' "${names_arr[@]}")
# Clear loading message
if [[ $app_count -gt 100 ]]; then
@@ -143,8 +145,7 @@ select_apps_for_uninstall() {
# The menu will gracefully fallback if these are unset or malformed.
export MOLE_MENU_META_EPOCHS="$epochs_csv"
export MOLE_MENU_META_SIZEKB="$sizekb_csv"
# Optional: allow default sort override via env (date|name|size)
# export MOLE_MENU_SORT_DEFAULT="${MOLE_MENU_SORT_DEFAULT:-date}"
export MOLE_MENU_FILTER_NAMES="$names_newline"
# Use paginated menu - result will be stored in MOLE_SELECTION_RESULT
# Note: paginated_multi_select enters alternate screen and handles clearing

View File

@@ -89,13 +89,17 @@ paginated_multi_select() {
local top_index=0
local sort_mode="${MOLE_MENU_SORT_MODE:-${MOLE_MENU_SORT_DEFAULT:-date}}" # date|name|size
local sort_reverse="${MOLE_MENU_SORT_REVERSE:-false}"
local filter_text="" # Filter keyword
# Metadata (optional)
# epochs[i] -> last_used_epoch (numeric) for item i
# sizekb[i] -> size in KB (numeric) for item i
# filter_names[i] -> name for filtering (if not set, use items[i])
local -a epochs=()
local -a sizekb=()
local -a filter_names=()
local has_metadata="false"
local has_filter_names="false"
if [[ -n "${MOLE_MENU_META_EPOCHS:-}" ]]; then
while IFS= read -r v; do epochs+=("${v:-0}"); done < <(_pm_parse_csv_to_array "$MOLE_MENU_META_EPOCHS")
has_metadata="true"
@@ -104,6 +108,10 @@ paginated_multi_select() {
while IFS= read -r v; do sizekb+=("${v:-0}"); done < <(_pm_parse_csv_to_array "$MOLE_MENU_META_SIZEKB")
has_metadata="true"
fi
if [[ -n "${MOLE_MENU_FILTER_NAMES:-}" ]]; then
while IFS= read -r v; do filter_names+=("$v"); done <<< "$MOLE_MENU_FILTER_NAMES"
has_filter_names="true"
fi
# If no metadata, force name sorting and disable sorting controls
if [[ "$has_metadata" == "false" && "$sort_mode" != "name" ]]; then
@@ -232,13 +240,33 @@ paginated_multi_select() {
printf "%s%s\n" "$clear_line" "$line" >&2
}
# Rebuild the view_indices applying sort
# Rebuild the view_indices applying filter and sort
rebuild_view() {
# Sort (skip if no metadata)
local -a active_indices=()
if [[ -n "$filter_text" ]]; then
local filter_lower
filter_lower=$(printf "%s" "$filter_text" | LC_ALL=C tr '[:upper:]' '[:lower:]')
for id in "${orig_indices[@]}"; do
local filter_target
if [[ $has_filter_names == true && -n "${filter_names[id]:-}" ]]; then
filter_target="${filter_names[id]}"
else
filter_target="${items[id]}"
fi
local target_lower
target_lower=$(printf "%s" "$filter_target" | LC_ALL=C tr '[:upper:]' '[:lower:]')
if [[ "$target_lower" == *"$filter_lower"* ]]; then
active_indices+=("$id")
fi
done
else
active_indices=("${orig_indices[@]}")
fi
# Sort filtered results
if [[ "$has_metadata" == "false" ]]; then
# No metadata: just use original indices
view_indices=("${orig_indices[@]}")
elif [[ ${#orig_indices[@]} -eq 0 ]]; then
view_indices=("${active_indices[@]}")
elif [[ ${#active_indices[@]} -eq 0 ]]; then
view_indices=()
else
# Build sort key
@@ -262,7 +290,7 @@ paginated_multi_select() {
tmpfile=$(mktemp 2> /dev/null) || tmpfile=""
if [[ -n "$tmpfile" ]]; then
local k id
for id in "${orig_indices[@]}"; do
for id in "${active_indices[@]}"; do
case "$sort_mode" in
date) k="${epochs[id]:-0}" ;;
size) k="${sizekb[id]:-0}" ;;
@@ -280,7 +308,7 @@ paginated_multi_select() {
rm -f "$tmpfile"
else
# Fallback: no sorting
view_indices=("${orig_indices[@]}")
view_indices=("${active_indices[@]}")
fi
fi
@@ -321,19 +349,42 @@ paginated_multi_select() {
fi
}
draw_header() {
printf "\033[1;1H" >&2
if [[ -n "$filter_text" ]]; then
printf "\r\033[2K${PURPLE_BOLD}%s${NC} ${YELLOW}/ Filter: ${filter_text}_${NC} ${GRAY}(%d/%d)${NC}\n" "${title}" "${#view_indices[@]}" "$total_items" >&2
elif [[ -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
printf "\r\033[2K${PURPLE_BOLD}%s${NC} ${YELLOW}/ Filter: _ ${NC}${GRAY}(type to search)${NC}\n" "${title}" >&2
else
printf "\r\033[2K${PURPLE_BOLD}%s${NC} ${GRAY}%d/%d selected${NC}\n" "${title}" "$selected_count" "$total_items" >&2
fi
}
# Handle filter character input (reduces code duplication)
# Returns 0 if character was handled, 1 if not in filter mode
handle_filter_char() {
local char="$1"
if [[ -z "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
return 1
fi
if [[ "$char" =~ ^[[:print:]]$ ]]; then
filter_text+="$char"
rebuild_view
cursor_pos=0
top_index=0
need_full_redraw=true
fi
return 0
}
# Draw the complete menu
draw_menu() {
# Recalculate items_per_page dynamically to handle window resize
items_per_page=$(_pm_calculate_items_per_page)
local clear_line=$'\r\033[2K'
printf "\033[H" >&2
local clear_line="\r\033[2K"
# Use cached selection count (maintained incrementally on toggle)
# No need to loop through all items anymore!
# Header only
printf "${clear_line}${PURPLE_BOLD}%s${NC} ${GRAY}%d/%d selected${NC}\n" "${title}" "$selected_count" "$total_items" >&2
draw_header
# Visible slice
local visible_total=${#view_indices[@]}
@@ -410,15 +461,19 @@ paginated_multi_select() {
local refresh="${GRAY}R Refresh${NC}"
local sort_ctrl="${GRAY}S ${sort_status}${NC}"
local order_ctrl="${GRAY}O ${reverse_arrow}${NC}"
local filter_ctrl="${GRAY}/ Filter${NC}"
if [[ "$has_metadata" == "true" ]]; then
if [[ -n "$filter_text" ]]; then
local -a _segs_filter=("${GRAY}Backspace${NC}" "${GRAY}ESC Clear${NC}")
_print_wrapped_controls "$sep" "${_segs_filter[@]}"
elif [[ "$has_metadata" == "true" ]]; then
# With metadata: show sort controls
local term_width="${COLUMNS:-}"
[[ -z "$term_width" ]] && term_width=$(tput cols 2> /dev/null || echo 80)
[[ "$term_width" =~ ^[0-9]+$ ]] || term_width=80
# Full controls
local -a _segs=("$nav" "$space_select" "$enter" "$refresh" "$sort_ctrl" "$order_ctrl" "$exit")
local -a _segs=("$nav" "$space_select" "$enter" "$refresh" "$sort_ctrl" "$order_ctrl" "$filter_ctrl" "$exit")
# Calculate width
local total_len=0 seg_count=${#_segs[@]}
@@ -429,7 +484,7 @@ paginated_multi_select() {
# Level 1: Remove "Space Select" if too wide
if [[ $total_len -gt $term_width ]]; then
_segs=("$nav" "$enter" "$refresh" "$sort_ctrl" "$order_ctrl" "$exit")
_segs=("$nav" "$enter" "$refresh" "$sort_ctrl" "$order_ctrl" "$filter_ctrl" "$exit")
total_len=0
seg_count=${#_segs[@]}
@@ -440,14 +495,14 @@ paginated_multi_select() {
# Level 2: Remove sort label if still too wide
if [[ $total_len -gt $term_width ]]; then
_segs=("$nav" "$enter" "$refresh" "$order_ctrl" "$exit")
_segs=("$nav" "$enter" "$refresh" "$order_ctrl" "$filter_ctrl" "$exit")
fi
fi
_print_wrapped_controls "$sep" "${_segs[@]}"
else
# Without metadata: basic controls
local -a _segs_simple=("$nav" "$space_select" "$enter" "$refresh" "$exit")
local -a _segs_simple=("$nav" "$space_select" "$enter" "$refresh" "$filter_ctrl" "$exit")
_print_wrapped_controls "$sep" "${_segs_simple[@]}"
fi
printf "${clear_line}" >&2
@@ -473,52 +528,62 @@ paginated_multi_select() {
case "$key" in
"QUIT")
cleanup
return 1
if [[ -n "$filter_text" || -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
filter_text=""
unset MOLE_READ_KEY_FORCE_CHAR
rebuild_view
cursor_pos=0
top_index=0
need_full_redraw=true
else
cleanup
return 1
fi
;;
"UP")
if [[ ${#view_indices[@]} -eq 0 ]]; then
:
elif [[ $cursor_pos -gt 0 ]]; then
# Simple cursor move - only redraw affected rows
local old_cursor=$cursor_pos
((cursor_pos--))
local new_cursor=$cursor_pos
# Calculate terminal row positions (+3: row 1=header, row 2=blank, row 3=first item)
if [[ -n "$filter_text" || -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
draw_header
fi
local old_row=$((old_cursor + 3))
local new_row=$((new_cursor + 3))
# Quick redraw: update only the two affected rows
printf "\033[%d;1H" "$old_row" >&2
render_item "$old_cursor" false
printf "\033[%d;1H" "$new_row" >&2
render_item "$new_cursor" true
# CRITICAL: Move cursor to footer to avoid visual artifacts
printf "\033[%d;1H" "$((items_per_page + 4))" >&2
prev_cursor_pos=$cursor_pos
continue # Skip full redraw
continue
elif [[ $top_index -gt 0 ]]; then
# Scroll up - redraw visible items only
((top_index--))
# Redraw all visible items (faster than full screen redraw)
if [[ -n "$filter_text" || -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
draw_header
fi
local start_idx=$top_index
local end_idx=$((top_index + items_per_page - 1))
local visible_total=${#view_indices[@]}
[[ $end_idx -ge $visible_total ]] && end_idx=$((visible_total - 1))
for ((i = start_idx; i <= end_idx; i++)); do
local row=$((i - start_idx + 3)) # +3 for header
local row=$((i - start_idx + 3))
printf "\033[%d;1H" "$row" >&2
local is_current=false
[[ $((i - start_idx)) -eq $cursor_pos ]] && is_current=true
render_item $((i - start_idx)) $is_current
done
# Move cursor to footer
printf "\033[%d;1H" "$((items_per_page + 4))" >&2
prev_cursor_pos=$cursor_pos
@@ -537,28 +602,27 @@ paginated_multi_select() {
[[ $visible_count -gt $items_per_page ]] && visible_count=$items_per_page
if [[ $cursor_pos -lt $((visible_count - 1)) ]]; then
# Simple cursor move - only redraw affected rows
local old_cursor=$cursor_pos
((cursor_pos++))
local new_cursor=$cursor_pos
# Calculate terminal row positions (+3: row 1=header, row 2=blank, row 3=first item)
if [[ -n "$filter_text" || -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
draw_header
fi
local old_row=$((old_cursor + 3))
local new_row=$((new_cursor + 3))
# Quick redraw: update only the two affected rows
printf "\033[%d;1H" "$old_row" >&2
render_item "$old_cursor" false
printf "\033[%d;1H" "$new_row" >&2
render_item "$new_cursor" true
# CRITICAL: Move cursor to footer to avoid visual artifacts
printf "\033[%d;1H" "$((items_per_page + 4))" >&2
prev_cursor_pos=$cursor_pos
continue # Skip full redraw
continue
elif [[ $((top_index + visible_count)) -lt ${#view_indices[@]} ]]; then
# Scroll down - redraw visible items only
((top_index++))
visible_count=$((${#view_indices[@]} - top_index))
[[ $visible_count -gt $items_per_page ]] && visible_count=$items_per_page
@@ -566,21 +630,23 @@ paginated_multi_select() {
cursor_pos=$((visible_count - 1))
fi
# Redraw all visible items (faster than full screen redraw)
if [[ -n "$filter_text" || -n "${MOLE_READ_KEY_FORCE_CHAR:-}" ]]; then
draw_header
fi
local start_idx=$top_index
local end_idx=$((top_index + items_per_page - 1))
local visible_total=${#view_indices[@]}
[[ $end_idx -ge $visible_total ]] && end_idx=$((visible_total - 1))
for ((i = start_idx; i <= end_idx; i++)); do
local row=$((i - start_idx + 3)) # +3 for header
local row=$((i - start_idx + 3))
printf "\033[%d;1H" "$row" >&2
local is_current=false
[[ $((i - start_idx)) -eq $cursor_pos ]] && is_current=true
render_item $((i - start_idx)) $is_current
done
# Move cursor to footer
printf "\033[%d;1H" "$((items_per_page + 4))" >&2
prev_cursor_pos=$cursor_pos
@@ -630,8 +696,9 @@ paginated_multi_select() {
fi
;;
"CHAR:s" | "CHAR:S")
if [[ "$has_metadata" == "true" ]]; then
# Cycle sort mode (only if metadata available)
if handle_filter_char "${key#CHAR:}"; then
: # Handled as filter input
elif [[ "$has_metadata" == "true" ]]; then
case "$sort_mode" in
date) sort_mode="name" ;;
name) sort_mode="size" ;;
@@ -642,8 +709,9 @@ paginated_multi_select() {
fi
;;
"CHAR:j")
# Down navigation (vim style)
if [[ ${#view_indices[@]} -gt 0 ]]; then
if handle_filter_char "${key#CHAR:}"; then
: # Handled as filter input
elif [[ ${#view_indices[@]} -gt 0 ]]; then
local absolute_index=$((top_index + cursor_pos))
local last_index=$((${#view_indices[@]} - 1))
if [[ $absolute_index -lt $last_index ]]; then
@@ -659,8 +727,9 @@ paginated_multi_select() {
fi
;;
"CHAR:k")
# Up navigation (vim style)
if [[ ${#view_indices[@]} -gt 0 ]]; then
if handle_filter_char "${key#CHAR:}"; then
: # Handled as filter input
elif [[ ${#view_indices[@]} -gt 0 ]]; then
if [[ $cursor_pos -gt 0 ]]; then
((cursor_pos--))
need_full_redraw=true
@@ -671,13 +740,17 @@ paginated_multi_select() {
fi
;;
"CHAR:r" | "CHAR:R")
# Trigger Refresh signal
cleanup
return 10
if handle_filter_char "${key#CHAR:}"; then
: # Handled as filter input
else
cleanup
return 10
fi
;;
"CHAR:o" | "CHAR:O")
if [[ "$has_metadata" == "true" ]]; then
# O toggles reverse order
if handle_filter_char "${key#CHAR:}"; then
: # Handled as filter input
elif [[ "$has_metadata" == "true" ]]; then
if [[ "$sort_reverse" == "true" ]]; then
sort_reverse="false"
else
@@ -687,6 +760,25 @@ paginated_multi_select() {
need_full_redraw=true
fi
;;
"CHAR:/" | "CHAR:?")
export MOLE_READ_KEY_FORCE_CHAR=1
need_full_redraw=true
;;
"DELETE")
if [[ -n "$filter_text" ]]; then
filter_text="${filter_text%?}"
if [[ -z "$filter_text" ]]; then
unset MOLE_READ_KEY_FORCE_CHAR
fi
rebuild_view
cursor_pos=0
top_index=0
need_full_redraw=true
fi
;;
"CHAR:"*)
handle_filter_char "${key#CHAR:}" || true
;;
"ENTER")
# Smart Enter behavior
# 1. Check if any items are already selected