From aaa3a6ae5a6aa3b3f1b73cccb88b0776bb995e9d Mon Sep 17 00:00:00 2001 From: tw93 Date: Mon, 2 Feb 2026 17:05:19 +0800 Subject: [PATCH] ui: add menu filtering support --- bin/uninstall_lib.sh | 666 --------------------------------------- lib/core/ui.sh | 15 +- lib/ui/app_selector.sh | 11 +- lib/ui/menu_paginated.sh | 192 ++++++++--- 4 files changed, 155 insertions(+), 729 deletions(-) delete mode 100755 bin/uninstall_lib.sh diff --git a/bin/uninstall_lib.sh b/bin/uninstall_lib.sh deleted file mode 100755 index 26a94cc..0000000 --- a/bin/uninstall_lib.sh +++ /dev/null @@ -1,666 +0,0 @@ -#!/bin/bash -# Mole - Uninstall Module -# Interactive application uninstaller with keyboard navigation -# -# Usage: -# uninstall.sh # Launch interactive uninstaller -# uninstall.sh --force-rescan # Rescan apps and refresh cache - -set -euo pipefail - -# Fix locale issues (avoid Perl warnings on non-English systems) -export LC_ALL=C -export LANG=C - -# Get script directory and source common functions -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "$SCRIPT_DIR/../lib/core/common.sh" -source "$SCRIPT_DIR/../lib/ui/menu_paginated.sh" -source "$SCRIPT_DIR/../lib/ui/app_selector.sh" -source "$SCRIPT_DIR/../lib/uninstall/batch.sh" - -# Note: Bundle preservation logic is now in lib/core/common.sh - -# Initialize global variables -selected_apps=() # Global array for app selection -declare -a apps_data=() -declare -a selection_state=() -total_items=0 -files_cleaned=0 -total_size_cleaned=0 - -# Compact the "last used" descriptor for aligned summaries -format_last_used_summary() { - local value="$1" - - case "$value" in - "" | "Unknown") - echo "Unknown" - return 0 - ;; - "Never" | "Recent" | "Today" | "Yesterday" | "This year" | "Old") - echo "$value" - return 0 - ;; - esac - - if [[ $value =~ ^([0-9]+)[[:space:]]+days?\ ago$ ]]; then - echo "${BASH_REMATCH[1]}d ago" - return 0 - fi - if [[ $value =~ ^([0-9]+)[[:space:]]+weeks?\ ago$ ]]; then - echo "${BASH_REMATCH[1]}w ago" - return 0 - fi - if [[ $value =~ ^([0-9]+)[[:space:]]+months?\ ago$ ]]; then - echo "${BASH_REMATCH[1]}m ago" - return 0 - fi - if [[ $value =~ ^([0-9]+)[[:space:]]+month\(s\)\ ago$ ]]; then - echo "${BASH_REMATCH[1]}m ago" - return 0 - fi - if [[ $value =~ ^([0-9]+)[[:space:]]+years?\ ago$ ]]; then - echo "${BASH_REMATCH[1]}y ago" - return 0 - fi - echo "$value" -} - -# Scan applications and collect information -scan_applications() { - # Simplified cache: only check timestamp (24h TTL) - local cache_dir="$HOME/.cache/mole" - local cache_file="$cache_dir/app_scan_cache" - local cache_ttl=86400 # 24 hours - local force_rescan="${1:-false}" - - ensure_user_dir "$cache_dir" - - # Check if cache exists and is fresh - if [[ $force_rescan == false && -f "$cache_file" ]]; then - local cache_age=$(($(get_epoch_seconds) - $(get_file_mtime "$cache_file"))) - [[ $cache_age -eq $(get_epoch_seconds) ]] && cache_age=86401 # Handle missing file - if [[ $cache_age -lt $cache_ttl ]]; then - # Cache hit - return immediately - # Show brief flash of cache usage if in interactive mode - if [[ -t 2 ]]; then - echo -e "${GREEN}Loading from cache...${NC}" >&2 - # Small sleep to let user see it (optional, but good for "feeling" the speed vs glitch) - sleep 0.3 - fi - echo "$cache_file" - return 0 - fi - fi - - # Cache miss - prepare for scanning - local inline_loading=false - if [[ -t 1 && -t 2 ]]; then - inline_loading=true - # Clear screen for inline loading - printf "\033[2J\033[H" >&2 - fi - - local temp_file - temp_file=$(create_temp_file) - - # Pre-cache current epoch to avoid repeated calls - local current_epoch - current_epoch=$(get_epoch_seconds) - - # First pass: quickly collect all valid app paths and bundle IDs (NO mdls calls) - local -a app_data_tuples=() - local -a app_dirs=( - "/Applications" - "$HOME/Applications" - ) - local vol_app_dir - local nullglob_was_set=0 - shopt -q nullglob && nullglob_was_set=1 - shopt -s nullglob - for vol_app_dir in /Volumes/*/Applications; do - [[ -d "$vol_app_dir" && -r "$vol_app_dir" ]] || continue - if [[ -d "/Applications" && "$vol_app_dir" -ef "/Applications" ]]; then - continue - fi - if [[ -d "$HOME/Applications" && "$vol_app_dir" -ef "$HOME/Applications" ]]; then - continue - fi - app_dirs+=("$vol_app_dir") - done - if [[ $nullglob_was_set -eq 0 ]]; then - shopt -u nullglob - fi - - for app_dir in "${app_dirs[@]}"; do - if [[ ! -d "$app_dir" ]]; then continue; fi - - while IFS= read -r -d '' app_path; do - if [[ ! -e "$app_path" ]]; then continue; fi - - local app_name - app_name=$(basename "$app_path" .app) - - # Skip nested apps (e.g. inside Wrapper/ or Frameworks/ of another app) - # Check if parent path component ends in .app (e.g. /Foo.app/Bar.app or /Foo.app/Contents/Bar.app) - # This prevents false positives like /Old.apps/Target.app - local parent_dir - parent_dir=$(dirname "$app_path") - if [[ "$parent_dir" == *".app" || "$parent_dir" == *".app/"* ]]; then - continue - fi - - # Get bundle ID only (fast, no mdls calls in first pass) - local bundle_id="unknown" - if [[ -f "$app_path/Contents/Info.plist" ]]; then - bundle_id=$(defaults read "$app_path/Contents/Info.plist" CFBundleIdentifier 2> /dev/null || echo "unknown") - fi - - # Skip system critical apps (input methods, system components) - if should_protect_from_uninstall "$bundle_id"; then - continue - fi - - # Store tuple: app_path|app_name|bundle_id (display_name will be resolved in parallel later) - app_data_tuples+=("${app_path}|${app_name}|${bundle_id}") - done < <(command find "$app_dir" -name "*.app" -maxdepth 3 -print0 2> /dev/null) - done - - # Second pass: process each app with parallel size calculation - local app_count=0 - local total_apps=${#app_data_tuples[@]} - # Bound parallelism - for metadata queries, can go higher since it's mostly waiting - local max_parallel - max_parallel=$(get_optimal_parallel_jobs "io") - if [[ $max_parallel -lt 8 ]]; then - max_parallel=8 - elif [[ $max_parallel -gt 32 ]]; then - max_parallel=32 - fi - local pids=() - # inline_loading variable already set above (line ~92) - - # Process app metadata extraction function - process_app_metadata() { - local app_data_tuple="$1" - local output_file="$2" - local current_epoch="$3" - - IFS='|' read -r app_path app_name bundle_id <<< "$app_data_tuple" - - # Get localized display name (moved from first pass for better performance) - local display_name="$app_name" - if [[ -f "$app_path/Contents/Info.plist" ]]; then - # Try to get localized name from system metadata (best for i18n) - local md_display_name - md_display_name=$(run_with_timeout 0.05 mdls -name kMDItemDisplayName -raw "$app_path" 2> /dev/null || echo "") - - # Get bundle names - local bundle_display_name - bundle_display_name=$(plutil -extract CFBundleDisplayName raw "$app_path/Contents/Info.plist" 2> /dev/null) - local bundle_name - bundle_name=$(plutil -extract CFBundleName raw "$app_path/Contents/Info.plist" 2> /dev/null) - - # Priority order for name selection (prefer localized names): - # 1. System metadata display name (kMDItemDisplayName) - respects system language - # 2. CFBundleDisplayName - usually localized - # 3. CFBundleName - fallback - # 4. App folder name - last resort - - if [[ -n "$md_display_name" && "$md_display_name" != "(null)" && "$md_display_name" != "$app_name" ]]; then - display_name="$md_display_name" - elif [[ -n "$bundle_display_name" && "$bundle_display_name" != "(null)" ]]; then - display_name="$bundle_display_name" - elif [[ -n "$bundle_name" && "$bundle_name" != "(null)" ]]; then - display_name="$bundle_name" - fi - fi - - # Parallel size calculation - local app_size="N/A" - local app_size_kb="0" - if [[ -d "$app_path" ]]; then - # Get size in KB, then format for display - app_size_kb=$(get_path_size_kb "$app_path") - app_size=$(bytes_to_human "$((app_size_kb * 1024))") - fi - - # Get last used date - local last_used="Never" - local last_used_epoch=0 - - if [[ -d "$app_path" ]]; then - # Try mdls first with short timeout (0.1s) for accuracy, fallback to mtime for speed - local metadata_date - metadata_date=$(run_with_timeout 0.1 mdls -name kMDItemLastUsedDate -raw "$app_path" 2> /dev/null || echo "") - - if [[ "$metadata_date" != "(null)" && -n "$metadata_date" ]]; then - last_used_epoch=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "$metadata_date" "+%s" 2> /dev/null || echo "0") - fi - - # Fallback if mdls failed or returned nothing - if [[ "$last_used_epoch" -eq 0 ]]; then - last_used_epoch=$(get_file_mtime "$app_path") - fi - - if [[ $last_used_epoch -gt 0 ]]; then - local days_ago=$(((current_epoch - last_used_epoch) / 86400)) - - if [[ $days_ago -eq 0 ]]; then - last_used="Today" - elif [[ $days_ago -eq 1 ]]; then - last_used="Yesterday" - elif [[ $days_ago -lt 7 ]]; then - last_used="${days_ago} days ago" - elif [[ $days_ago -lt 30 ]]; then - local weeks_ago=$((days_ago / 7)) - [[ $weeks_ago -eq 1 ]] && last_used="1 week ago" || last_used="${weeks_ago} weeks ago" - elif [[ $days_ago -lt 365 ]]; then - local months_ago=$((days_ago / 30)) - [[ $months_ago -eq 1 ]] && last_used="1 month ago" || last_used="${months_ago} months ago" - else - local years_ago=$((days_ago / 365)) - [[ $years_ago -eq 1 ]] && last_used="1 year ago" || last_used="${years_ago} years ago" - fi - fi - fi - - # Write to output file atomically - # Fields: epoch|app_path|display_name|bundle_id|size_human|last_used|size_kb - echo "${last_used_epoch}|${app_path}|${display_name}|${bundle_id}|${app_size}|${last_used}|${app_size_kb}" >> "$output_file" - } - - export -f process_app_metadata - - # Create a temporary file to track progress - local progress_file="${temp_file}.progress" - echo "0" > "$progress_file" - - # Start a background spinner that reads progress from file - local spinner_pid="" - ( - # shellcheck disable=SC2329 # Function invoked indirectly via trap - cleanup_spinner() { exit 0; } - trap cleanup_spinner TERM INT EXIT - local spinner_chars="|/-\\" - local i=0 - while true; do - local completed=$(cat "$progress_file" 2> /dev/null || echo 0) - local c="${spinner_chars:$((i % 4)):1}" - if [[ $inline_loading == true ]]; then - printf "\033[H\033[2K%s Scanning applications... %d/%d\n" "$c" "$completed" "$total_apps" >&2 - else - printf "\r\033[K%s Scanning applications... %d/%d" "$c" "$completed" "$total_apps" >&2 - fi - ((i++)) - sleep 0.1 2> /dev/null || sleep 1 - done - ) & - spinner_pid=$! - - # Process apps in parallel batches - for app_data_tuple in "${app_data_tuples[@]}"; do - ((app_count++)) - - # Launch background process - process_app_metadata "$app_data_tuple" "$temp_file" "$current_epoch" & - pids+=($!) - - # Update progress to show scanning progress (use app_count as it increments smoothly) - echo "$app_count" > "$progress_file" - - # Wait if we've hit max parallel limit - if ((${#pids[@]} >= max_parallel)); then - wait "${pids[0]}" 2> /dev/null - pids=("${pids[@]:1}") # Remove first pid - fi - done - - # Wait for remaining background processes - for pid in "${pids[@]}"; do - wait "$pid" 2> /dev/null - done - - # Stop the spinner and clear the line - if [[ -n "$spinner_pid" ]]; then - kill -TERM "$spinner_pid" 2> /dev/null || true - wait "$spinner_pid" 2> /dev/null || true - fi - if [[ $inline_loading == true ]]; then - printf "\033[H\033[2K" >&2 - else - echo -ne "\r\033[K" >&2 - fi - rm -f "$progress_file" - - # Check if we found any applications - if [[ ! -s "$temp_file" ]]; then - echo "No applications found to uninstall" >&2 - rm -f "$temp_file" - return 1 - fi - - # Sort by last used (oldest first) and cache the result - # Show brief processing message for large app lists - if [[ $total_apps -gt 50 ]]; then - if [[ $inline_loading == true ]]; then - printf "\033[H\033[2KProcessing %d applications...\n" "$total_apps" >&2 - else - printf "\rProcessing %d applications... " "$total_apps" >&2 - fi - fi - - sort -t'|' -k1,1n "$temp_file" > "${temp_file}.sorted" || { - rm -f "$temp_file" - return 1 - } - rm -f "$temp_file" - - # Clear processing message - if [[ $total_apps -gt 50 ]]; then - if [[ $inline_loading == true ]]; then - printf "\033[H\033[2K" >&2 - else - printf "\r\033[K" >&2 - fi - fi - - # Save to cache (simplified - no metadata) - ensure_user_file "$cache_file" - cp "${temp_file}.sorted" "$cache_file" 2> /dev/null || true - - # Return sorted file - if [[ -f "${temp_file}.sorted" ]]; then - echo "${temp_file}.sorted" - else - return 1 - fi -} - -load_applications() { - local apps_file="$1" - - if [[ ! -f "$apps_file" || ! -s "$apps_file" ]]; then - log_warning "No applications found for uninstallation" - return 1 - fi - - # Clear arrays - apps_data=() - selection_state=() - - # Read apps into array, skip non-existent apps - while IFS='|' read -r epoch app_path app_name bundle_id size last_used size_kb; do - # Skip if app path no longer exists - [[ ! -e "$app_path" ]] && continue - - apps_data+=("$epoch|$app_path|$app_name|$bundle_id|$size|$last_used|${size_kb:-0}") - selection_state+=(false) - done < "$apps_file" - - if [[ ${#apps_data[@]} -eq 0 ]]; then - log_warning "No applications available for uninstallation" - return 1 - fi - - return 0 -} - -# Cleanup function - restore cursor and clean up -cleanup() { - # Restore cursor using common function - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - fi - if [[ -n "${sudo_keepalive_pid:-}" ]]; then - kill "$sudo_keepalive_pid" 2> /dev/null || true - wait "$sudo_keepalive_pid" 2> /dev/null || true - sudo_keepalive_pid="" - fi - show_cursor - exit "${1:-0}" -} - -# Set trap for cleanup on exit -trap cleanup EXIT INT TERM - -main() { - local force_rescan=false - for arg in "$@"; do - case "$arg" in - "--debug") - export MO_DEBUG=1 - ;; - "--force-rescan") - force_rescan=true - ;; - esac - done - - local use_inline_loading=false - if [[ -t 1 && -t 2 ]]; then - use_inline_loading=true - fi - - # Hide cursor during operation - hide_cursor - - # Main interaction loop - while true; do - # Simplified: always check if we need alt screen for scanning - # (scan_applications handles cache internally) - local needs_scanning=true - local cache_file="$HOME/.cache/mole/app_scan_cache" - if [[ $force_rescan == false && -f "$cache_file" ]]; then - local cache_age=$(($(get_epoch_seconds) - $(get_file_mtime "$cache_file"))) - [[ $cache_age -eq $(get_epoch_seconds) ]] && cache_age=86401 # Handle missing file - [[ $cache_age -lt 86400 ]] && needs_scanning=false - fi - - # Only enter alt screen if we need scanning (shows progress) - if [[ $needs_scanning == true && $use_inline_loading == true ]]; then - # Only enter if not already active - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" != "1" ]]; then - enter_alt_screen - export MOLE_ALT_SCREEN_ACTIVE=1 - export MOLE_INLINE_LOADING=1 - export MOLE_MANAGED_ALT_SCREEN=1 - fi - printf "\033[2J\033[H" >&2 - else - # If we don't need scanning but have alt screen from previous iteration, keep it? - # Actually, scan_applications might output to stderr. - # Let's just unset the flags if we don't need scanning, but keep alt screen if it was active? - # No, select_apps_for_uninstall will handle its own screen management. - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN MOLE_ALT_SCREEN_ACTIVE - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - fi - fi - - # Scan applications - local apps_file="" - if ! apps_file=$(scan_applications "$force_rescan"); then - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - printf "\033[2J\033[H" >&2 - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN - fi - return 1 - fi - - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - printf "\033[2J\033[H" >&2 - fi - - if [[ ! -f "$apps_file" ]]; then - # Error message already shown by scan_applications - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN - fi - return 1 - fi - - # Load applications - if ! load_applications "$apps_file"; then - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN - fi - rm -f "$apps_file" - return 1 - fi - - # Interactive selection using paginated menu - set +e - select_apps_for_uninstall - local exit_code=$? - set -e - - if [[ $exit_code -ne 0 ]]; then - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN - fi - show_cursor - clear_screen - printf '\033[2J\033[H' >&2 # Also clear stderr - rm -f "$apps_file" - - # Handle Refresh (code 10) - if [[ $exit_code -eq 10 ]]; then - force_rescan=true - continue - fi - - # User cancelled selection, exit the loop - return 0 - fi - - # Always clear on exit from selection, regardless of alt screen state - if [[ "${MOLE_ALT_SCREEN_ACTIVE:-}" == "1" ]]; then - leave_alt_screen - unset MOLE_ALT_SCREEN_ACTIVE - unset MOLE_INLINE_LOADING MOLE_MANAGED_ALT_SCREEN - fi - - # Restore cursor and clear screen (output to both stdout and stderr for reliability) - show_cursor - clear_screen - printf '\033[2J\033[H' >&2 # Also clear stderr in case of mixed output - local selection_count=${#selected_apps[@]} - if [[ $selection_count -eq 0 ]]; then - echo "No apps selected" - rm -f "$apps_file" - # Loop back or exit? If select_apps_for_uninstall returns 0 but empty selection, - # it technically shouldn't happen based on that function's logic. - continue - fi - # Show selected apps with clean alignment - echo -e "${BLUE}${ICON_CONFIRM}${NC} Selected ${selection_count} apps:" - local -a summary_rows=() - local max_name_width=0 - local max_size_width=0 - local max_last_width=0 - # First pass: get actual max widths for all columns - for selected_app in "${selected_apps[@]}"; do - IFS='|' read -r _ _ app_name _ size last_used _ <<< "$selected_app" - [[ ${#app_name} -gt $max_name_width ]] && max_name_width=${#app_name} - local size_display="$size" - [[ -z "$size_display" || "$size_display" == "0" || "$size_display" == "N/A" ]] && size_display="Unknown" - [[ ${#size_display} -gt $max_size_width ]] && max_size_width=${#size_display} - local last_display=$(format_last_used_summary "$last_used") - [[ ${#last_display} -gt $max_last_width ]] && max_last_width=${#last_display} - done - ((max_size_width < 5)) && max_size_width=5 - ((max_last_width < 5)) && max_last_width=5 - - # Calculate name width: use actual max, but constrain by terminal width - # Fixed elements: "99. " (4) + " " (2) + " | Last: " (11) = 17 - local term_width=$(tput cols 2> /dev/null || echo 100) - local available_for_name=$((term_width - 17 - max_size_width - max_last_width)) - - # Dynamic minimum for better spacing on wide terminals - local min_name_width=24 - if [[ $term_width -ge 120 ]]; then - min_name_width=50 - elif [[ $term_width -ge 100 ]]; then - min_name_width=42 - elif [[ $term_width -ge 80 ]]; then - min_name_width=30 - fi - - # Constrain name width: dynamic min, max min(actual_max, available, 60) - local name_trunc_limit=$max_name_width - [[ $name_trunc_limit -lt $min_name_width ]] && name_trunc_limit=$min_name_width - [[ $name_trunc_limit -gt $available_for_name ]] && name_trunc_limit=$available_for_name - [[ $name_trunc_limit -gt 60 ]] && name_trunc_limit=60 - - # Reset for second pass - max_name_width=0 - - for selected_app in "${selected_apps[@]}"; do - IFS='|' read -r epoch app_path app_name bundle_id size last_used size_kb <<< "$selected_app" - - local display_name="$app_name" - if [[ ${#display_name} -gt $name_trunc_limit ]]; then - display_name="${display_name:0:$((name_trunc_limit - 3))}..." - fi - [[ ${#display_name} -gt $max_name_width ]] && max_name_width=${#display_name} - - local size_display="$size" - if [[ -z "$size_display" || "$size_display" == "0" || "$size_display" == "N/A" ]]; then - size_display="Unknown" - fi - - local last_display - last_display=$(format_last_used_summary "$last_used") - - summary_rows+=("$display_name|$size_display|$last_display") - done - - ((max_name_width < 16)) && max_name_width=16 - - local index=1 - for row in "${summary_rows[@]}"; do - IFS='|' read -r name_cell size_cell last_cell <<< "$row" - printf "%d. %-*s %*s | Last: %s\n" "$index" "$max_name_width" "$name_cell" "$max_size_width" "$size_cell" "$last_cell" - ((index++)) - done - - # Execute batch uninstallation (handles confirmation) - batch_uninstall_applications - - # Cleanup current apps file - rm -f "$apps_file" - - # Pause before looping back - echo -e "${GRAY}Press Enter to return to application list, ESC to exit...${NC}" - local key - IFS= read -r -s -n1 key || key="" - drain_pending_input # Clean up any escape sequence remnants - case "$key" in - $'\e' | q | Q) - show_cursor - return 0 - ;; - *) - # Continue loop - ;; - esac - - # Reset force_rescan to false for subsequent loops, - # but relying on batch_uninstall's cache deletion for actual update - force_rescan=false - done -} - -# Run main function diff --git a/lib/core/ui.sh b/lib/core/ui.sh index 536082b..e7c2597 100755 --- a/lib/core/ui.sh +++ b/lib/core/ui.sh @@ -171,24 +171,25 @@ read_key() { $'\n' | $'\r') echo "ENTER" ;; $'\x7f' | $'\x08') echo "DELETE" ;; $'\x1b') - # Check if this is an escape sequence (arrow keys) or ESC key - if IFS= read -r -s -n 1 -t 0.1 rest 2> /dev/null; then + if IFS= read -r -s -n 1 -t 1 rest 2> /dev/null; then if [[ "$rest" == "[" ]]; then - if IFS= read -r -s -n 1 -t 0.1 rest2 2> /dev/null; then + if IFS= read -r -s -n 1 -t 1 rest2 2> /dev/null; then case "$rest2" in "A") echo "UP" ;; "B") echo "DOWN" ;; "C") echo "RIGHT" ;; "D") echo "LEFT" ;; "3") - IFS= read -r -s -n 1 -t 0.1 rest3 2> /dev/null + IFS= read -r -s -n 1 -t 1 rest3 2> /dev/null [[ "$rest3" == "~" ]] && echo "DELETE" || echo "OTHER" ;; *) echo "OTHER" ;; esac - else echo "QUIT"; fi + else + echo "QUIT" + fi elif [[ "$rest" == "O" ]]; then - if IFS= read -r -s -n 1 -t 0.1 rest2 2> /dev/null; then + if IFS= read -r -s -n 1 -t 1 rest2 2> /dev/null; then case "$rest2" in "A") echo "UP" ;; "B") echo "DOWN" ;; @@ -198,11 +199,9 @@ read_key() { esac else echo "OTHER"; fi else - # Not an escape sequence, it's ESC key echo "QUIT" fi else - # No following characters, it's ESC key echo "QUIT" fi ;; diff --git a/lib/ui/app_selector.sh b/lib/ui/app_selector.sh index 5c5238a..285ef04 100755 --- a/lib/ui/app_selector.sh +++ b/lib/ui/app_selector.sh @@ -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 diff --git a/lib/ui/menu_paginated.sh b/lib/ui/menu_paginated.sh index 18c1503..05316cd 100755 --- a/lib/ui/menu_paginated.sh +++ b/lib/ui/menu_paginated.sh @@ -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