diff --git a/GUIDE.md b/GUIDE.md index 4497fdb..41e5bb0 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -19,7 +19,7 @@ 3. 找到"实用工具"文件夹 4. 双击"终端"图标 -> 💡 小提示:这个窗口看起来可能有点专业,但别担心,接下来的操作都很简单! +> 小提示:这个窗口看起来可能有点专业,但别担心,接下来的操作都很简单! --- @@ -51,11 +51,11 @@ brew install tw93/tap/mole ``` -> 💡 什么是 Homebrew?一个 Mac 软件管理工具。如果你不知道这是什么,请使用方法一。 +> 什么是 Homebrew?一个 Mac 软件管理工具。如果你不知道这是什么,请使用方法一。 > -> ⚠️ **重要:** 只选择一种方法安装!不要同时用两种方法,会产生冲突。 +> **重要:** 只选择一种方法安装!不要同时用两种方法,会产生冲突。 > -> ⚠️ 注意:第一次安装可能会要求你输入 Mac 的登录密码(输入时不会显示任何字符,这是正常的) +> 注意:第一次安装可能会要求你输入 Mac 的登录密码(输入时不会显示任何字符,这是正常的) --- @@ -80,7 +80,7 @@ brew install tw93/tap/mole ## 第四步:常见操作 -### ⚠️ 重要提示(请先阅读) +### 重要提示 **首次使用强烈建议:** @@ -204,4 +204,4 @@ mo analyze - [提交问题反馈](https://github.com/tw93/mole/issues) - [完整使用文档](./README.md) -**祝你使用愉快!如果觉得有用,欢迎分享给朋友 ✨** +**祝你使用愉快!如果觉得有用,欢迎分享给朋友~** diff --git a/README.md b/README.md index 550b6a8..ea2c92d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Mole

-

🐹 Dig deep like a mole to clean your Mac.

+

Dig deep like a mole to clean your Mac.

@@ -18,10 +18,10 @@ ## Features -- 🐦 **Deep System Cleanup** - Remove hidden caches, logs, and temp files in one sweep -- 📦 **Thorough Uninstall** - 22+ locations cleaned vs 1 standard, beats CleanMyMac/Lemon -- 📊 **Interactive Disk Analyzer** - Navigate folders like a file manager, find and delete large files instantly -- ⚡️ **Fast & Lightweight** - Terminal-based, zero bloat, arrow-key navigation with pagination +- **Deep System Cleanup** - Remove hidden caches, logs, and temp files in one sweep +- **Thorough Uninstall** - 22+ locations cleaned vs 1 standard, beats CleanMyMac/Lemon +- **Interactive Disk Analyzer** - Navigate folders like a file manager, find and delete large files instantly +- **Fast & Lightweight** - Terminal-based, zero bloat, arrow-key navigation with pagination ## Quick Start @@ -86,8 +86,8 @@ $ mo clean ✓ Spotify cache (3.1GB) ==================================================================== -🎉 CLEANUP COMPLETE! -💾 Space freed: 95.50GB | Free space now: 223.5GB +CLEANUP COMPLETE! +Space freed: 95.50GB | Free space now: 223.5GB ==================================================================== ``` @@ -96,13 +96,13 @@ $ mo clean ```bash $ mo uninstall -🗑️ Select Apps to Remove +Select Apps to Remove ═══════════════════════════ ▶ ☑ Adobe Creative Cloud (12.4G) | Old ☐ WeChat (2.1G) | Recent ☐ Final Cut Pro (3.8G) | Recent -🗑️ Uninstalling: Adobe Creative Cloud +Uninstalling: Adobe Creative Cloud ✓ Removed application # /Applications/ ✓ Cleaned 52 related files # ~/Library/ across 12 locations - Support files & caches # Application Support, Caches @@ -112,8 +112,8 @@ $ mo uninstall - System files with sudo # /Library/, Launch daemons ==================================================================== -🎉 UNINSTALLATION COMPLETE! -💾 Space freed: 12.8GB +UNINSTALLATION COMPLETE! +Space freed: 12.8GB ==================================================================== ``` @@ -122,7 +122,7 @@ $ mo uninstall ```bash $ mo analyze -📊 Analyzing: /Users/You +Analyzing: /Users/You ═══════════════════════════════════════════════════════ Total: 156.8GB @@ -132,7 +132,7 @@ Total: 156.8GB ├─ 📁 Downloads 32.6GB │ ├─ 📄 Xcode-14.3.1.dmg 12.3GB │ ├─ 📄 backup_2023.zip 8.6GB -│ └─ 📦 old_projects.tar.gz 5.2GB +│ └─ 📄 old_projects.tar.gz 5.2GB ├─ 📁 Movies 28.9GB │ ├─ 📄 vacation_2023.mov 15.4GB │ └─ 📄 screencast_raw.mp4 8.8GB diff --git a/bin/analyze.sh b/bin/analyze.sh index f8e804e..9439c61 100755 --- a/bin/analyze.sh +++ b/bin/analyze.sh @@ -4,6 +4,10 @@ set -euo pipefail +# Fix locale issues (avoid Perl warnings on non-English systems) +export LC_ALL=C +export LANG=C + # Get script directory for sourcing libraries SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LIB_DIR="$(dirname "$SCRIPT_DIR")/lib" @@ -19,6 +23,14 @@ readonly MIN_LARGE_FILE_SIZE="1000000000" # 1GB readonly MIN_MEDIUM_FILE_SIZE="100000000" # 100MB readonly MIN_SMALL_FILE_SIZE="10000000" # 10MB +# Emoji badges for list displays only +readonly BADGE_DIR="🍞" +readonly BADGE_FILE="📔" +readonly BADGE_MEDIA="🌁" +readonly BADGE_BUNDLE="🥜" +readonly BADGE_LOG="📝" +readonly BADGE_APP="🐣" + # Global state declare -a SCAN_RESULTS=() declare -a DIR_RESULTS=() @@ -36,6 +48,7 @@ declare VIEW_MODE="overview" # overview, detail, files # Cleanup on exit cleanup() { show_cursor + # Cleanup temp files using glob pattern (analyze uses many temp files) rm -f "$TEMP_PREFIX"* 2>/dev/null || true if [[ -n "$SCAN_PID" ]] && kill -0 "$SCAN_PID" 2>/dev/null; then kill "$SCAN_PID" 2>/dev/null || true @@ -58,13 +71,14 @@ scan_large_files() { fi # Scan files > 1GB - mdfind -onlyin "$target_path" "kMDItemFSSize > $MIN_LARGE_FILE_SIZE" 2>/dev/null | \ - while IFS= read -r file; do - if [[ -f "$file" ]]; then - local size=$(stat -f%z "$file" 2>/dev/null || echo "0") - echo "$size|$file" - fi - done | sort -t'|' -k1 -rn > "$output_file" + local file="" + while IFS= read -r file; do + if [[ -f "$file" ]]; then + local size=$(stat -f%z "$file" 2>/dev/null || echo "0") + echo "$size|$file" + fi + done < <(mdfind -onlyin "$target_path" "kMDItemFSSize > $MIN_LARGE_FILE_SIZE" 2>/dev/null) | \ + sort -t'|' -k1 -rn > "$output_file" } # Scan medium files (100MB - 1GB) @@ -76,14 +90,15 @@ scan_medium_files() { return 1 fi - mdfind -onlyin "$target_path" \ - "kMDItemFSSize > $MIN_MEDIUM_FILE_SIZE && kMDItemFSSize < $MIN_LARGE_FILE_SIZE" 2>/dev/null | \ - while IFS= read -r file; do - if [[ -f "$file" ]]; then - local size=$(stat -f%z "$file" 2>/dev/null || echo "0") - echo "$size|$file" - fi - done | sort -t'|' -k1 -rn > "$output_file" + local file="" + while IFS= read -r file; do + if [[ -f "$file" ]]; then + local size=$(stat -f%z "$file" 2>/dev/null || echo "0") + echo "$size|$file" + fi + done < <(mdfind -onlyin "$target_path" \ + "kMDItemFSSize > $MIN_MEDIUM_FILE_SIZE && kMDItemFSSize < $MIN_LARGE_FILE_SIZE" 2>/dev/null) | \ + sort -t'|' -k1 -rn > "$output_file" } # Scan top-level directories with du (optimized with parallel) @@ -248,7 +263,8 @@ perform_scan() { SCAN_PID=$! # Show spinner with progress while scanning - local spinner_chars="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" + local spinner_chars + spinner_chars="$(mo_spinner_chars)" local i=0 local elapsed=0 hide_cursor @@ -351,7 +367,7 @@ display_large_files_compact() { return fi - log_header "📊 Top Large Files" + log_header "Top Large Files" echo "" local count=0 @@ -373,8 +389,10 @@ display_large_files_compact() { local filename=$(basename "$path") local dirname=$(basename "$(dirname "$path")") - printf " ${GREEN}%-8s${NC} 📄 %-40s ${GRAY}%s${NC}\n" \ - "$human_size" "${filename:0:40}" "$dirname" + local info=$(get_file_info "$path") + local badge="${info%|*}" + printf " ${GREEN}%-8s${NC} %s %-40s ${GRAY}%s${NC}\n" \ + "$human_size" "$badge" "${filename:0:40}" "$dirname" ((count++)) done < "$temp_large" @@ -390,14 +408,14 @@ display_large_files() { local temp_large="$TEMP_PREFIX.large" if [[ ! -f "$temp_large" ]] || [[ ! -s "$temp_large" ]]; then - log_header "📊 Large Files (>1GB)" + log_header "Large Files (>1GB)" echo "" echo " ${GRAY}No files larger than 1GB found${NC}" echo "" return fi - log_header "📊 Large Files (>1GB)" + log_header "Large Files (>1GB)" echo "" local count=0 @@ -418,8 +436,10 @@ display_large_files() { local filename=$(basename "$path") local dirname=$(dirname "$path" | sed "s|^$HOME|~|") + local info=$(get_file_info "$path") + local badge="${info%|*}" printf " %s [${GREEN}%s${NC}] %7s\n" "$bar" "$human_size" "" - printf " 📄 %s\n" "$filename" + printf " %s %s\n" "$badge" "$filename" printf " ${GRAY}%s${NC}\n\n" "$dirname" ((count++)) @@ -441,7 +461,7 @@ display_directories_compact() { return fi - log_header "📁 Top Directories" + log_header "Top Directories" echo "" local count=0 @@ -479,8 +499,8 @@ display_directories_compact() { bar="${bar}$(printf "%${empty}s" "" | tr ' ' '░')" fi - printf " ${BLUE}%-8s${NC} %s ${GRAY}%3s%%${NC} 📁 %s\n" \ - "$human_size" "$bar" "$percentage" "$dirname" + printf " ${BLUE}%-8s${NC} %s ${GRAY}%3s%%${NC} %s %s\n" \ + "$human_size" "$bar" "$percentage" "$BADGE_DIR" "$dirname" ((count++)) done < "$temp_dirs" @@ -495,7 +515,7 @@ display_directories() { return fi - log_header "📁 Top Directories" + log_header "Top Directories" echo "" local count=0 @@ -525,7 +545,7 @@ display_directories() { local dirname=$(basename "$path") printf " %s [${BLUE}%s${NC}] %5s%%\n" "$bar" "$human_size" "$percentage" - printf " 📁 %s\n\n" "$display_path" + printf " %s %s\n\n" "$BADGE_DIR" "$display_path" ((count++)) done < "$temp_dirs" @@ -539,7 +559,7 @@ display_hotspots() { return fi - log_header "🔥 Hotspot Directories (High File Concentration)" + log_header "High-concentration Hotspot Directories" echo "" local count=0 @@ -551,7 +571,7 @@ display_hotspots() { local human_size=$(bytes_to_human "$size") local display_path=$(echo "$path" | sed "s|^$HOME|~|") - printf " 📍 %s\n" "$display_path" + printf " %s\n" "$display_path" printf " ${GREEN}%s${NC} in ${YELLOW}%d${NC} large files\n\n" \ "$human_size" "$file_count" @@ -629,9 +649,9 @@ display_cleanup_suggestions_compact() { fi if [[ $suggestions_count -gt 0 ]]; then - log_header "💡 Quick Insights" + log_header "Quick Insights" echo "" - echo " ${YELLOW}✨ $top_suggestion${NC}" + echo " ${YELLOW}$top_suggestion${NC}" if [[ $suggestions_count -gt 1 ]]; then echo " ${GRAY}... and $((suggestions_count - 1)) more insights${NC}" fi @@ -653,7 +673,7 @@ display_cleanup_suggestions_compact() { # Display smart cleanup suggestions (full version) display_cleanup_suggestions() { - log_header "💡 Smart Cleanup Suggestions" + log_header "Smart Cleanup Suggestions" echo "" local suggestions=() @@ -663,7 +683,7 @@ display_cleanup_suggestions() { local cache_size=$(du -sk "$HOME/Library/Caches" 2>/dev/null | cut -f1) if [[ $cache_size -gt 1048576 ]]; then # > 1GB local human=$(bytes_to_human $((cache_size * 1024))) - suggestions+=(" 🗑️ Clear application caches: $human") + suggestions+=(" Clear application caches: $human") fi fi @@ -671,7 +691,7 @@ display_cleanup_suggestions() { if [[ -d "$HOME/Downloads" ]]; then local old_files=$(find "$HOME/Downloads" -type f -mtime +90 2>/dev/null | wc -l | tr -d ' ') if [[ $old_files -gt 0 ]]; then - suggestions+=(" 📥 Clean old downloads: $old_files files older than 90 days") + suggestions+=(" Clean old downloads: $old_files files older than 90 days") fi fi @@ -680,7 +700,7 @@ display_cleanup_suggestions() { local dmg_count=$(mdfind -onlyin "$HOME" \ "kMDItemFSSize > 500000000 && kMDItemDisplayName == '*.dmg'" 2>/dev/null | wc -l | tr -d ' ') if [[ $dmg_count -gt 0 ]]; then - suggestions+=(" 💿 Remove disk images: $dmg_count DMG files >500MB") + suggestions+=(" Remove disk images: $dmg_count DMG files >500MB") fi fi @@ -689,7 +709,7 @@ display_cleanup_suggestions() { local xcode_size=$(du -sk "$HOME/Library/Developer/Xcode/DerivedData" 2>/dev/null | cut -f1) if [[ $xcode_size -gt 10485760 ]]; then # > 10GB local human=$(bytes_to_human $((xcode_size * 1024))) - suggestions+=(" 🔨 Clear Xcode cache: $human") + suggestions+=(" Clear Xcode cache: $human") fi fi @@ -710,7 +730,7 @@ display_cleanup_suggestions() { sort | uniq -d | wc -l | tr -d ' ' > "$temp_dup" 2>/dev/null || echo "0" > "$temp_dup" local dup_count=$(cat "$temp_dup" 2>/dev/null || echo "0") if [[ $dup_count -gt 5 ]]; then - suggestions+=(" 📋 Possible duplicates: $dup_count size matches in large files (>10MB)") + suggestions+=(" ♻️ Possible duplicates: $dup_count size matches in large files (>10MB)") fi fi @@ -718,7 +738,7 @@ display_cleanup_suggestions() { if [[ ${#suggestions[@]} -gt 0 ]]; then printf '%s\n' "${suggestions[@]}" echo "" - echo " ${YELLOW}Tip:${NC} Run 'mole clean' to perform cleanup operations" + echo " Tip: Run 'mole clean' to perform cleanup operations" else echo " ${GREEN}✓${NC} No obvious cleanup opportunities found" fi @@ -750,7 +770,7 @@ display_disk_summary() { done < "$temp_dirs" fi - log_header "💾 Disk Situation" + log_header "Disk Situation" local target_display=$(echo "$CURRENT_PATH" | sed "s|^$HOME|~|") echo " ${BLUE}Scanning:${NC} $target_display | ${BLUE}Free:${NC} $(get_free_space)" @@ -768,22 +788,28 @@ display_disk_summary() { get_file_info() { local path="$1" local ext="${path##*.}" - local icon="" - local type="" + local badge="$BADGE_FILE" + local type="File" case "$ext" in - dmg|iso|pkg) icon="📦" ; type="Installer" ;; - mov|mp4|avi|mkv|webm) icon="🎬" ; type="Video" ;; - zip|tar|gz|rar|7z) icon="🗜️" ; type="Archive" ;; - pdf) icon="📄" ; type="Document" ;; - jpg|jpeg|png|gif|heic) icon="🖼️" ; type="Image" ;; - key|ppt|pptx) icon="📊" ; type="Slides" ;; - log) icon="📝" ; type="Log" ;; - app) icon="⚙️" ; type="App" ;; - *) icon="📄" ; type="File" ;; + dmg|iso|pkg|zip|tar|gz|rar|7z) + badge="$BADGE_BUNDLE" ; type="Bundle" + ;; + mov|mp4|avi|mkv|webm|jpg|jpeg|png|gif|heic) + badge="$BADGE_MEDIA" ; type="Media" + ;; + pdf|key|ppt|pptx) + type="Document" + ;; + log) + badge="$BADGE_LOG" ; type="Log" + ;; + app) + badge="$BADGE_APP" ; type="App" + ;; esac - echo "$icon|$type" + echo "$badge|$type" } # Get file age in human readable format @@ -822,7 +848,7 @@ display_large_files_table() { return fi - log_header "🎯 What's Taking Up Space" + log_header "What's Taking Up Space" # Table header printf " %-4s %-10s %-8s %s\n" "TYPE" "SIZE" "AGE" "FILE" @@ -839,9 +865,9 @@ display_large_files_table() { local ext="${filename##*.}" local age=$(get_file_age "$path") - # Get file info + # Get file info and badge local info=$(get_file_info "$path") - local icon="${info%|*}" + local badge="${info%|*}" # Truncate filename if too long if [[ ${#filename} -gt 50 ]]; then @@ -858,7 +884,7 @@ display_large_files_table() { esac printf " %b%-4s %-10s %-8s %s${NC}\n" \ - "$color" "$icon" "$human_size" "$age" "$filename" + "$color" "$badge" "$human_size" "$age" "$filename" ((count++)) done < "$temp_large" @@ -942,7 +968,7 @@ display_unified_directories() { # Display context-aware recommendations display_recommendations() { - echo " ${YELLOW}💡 Quick Actions:${NC}" + echo " ${YELLOW}Quick Actions:${NC}" if [[ "$CURRENT_PATH" == "$HOME/Downloads"* ]]; then echo " → Delete ${RED}[Can Delete]${NC} items (installers/DMG)" @@ -965,7 +991,7 @@ display_space_chart() { return fi - log_header "📊 Space Distribution" + log_header "Space Distribution" echo "" # Calculate total @@ -1006,7 +1032,7 @@ display_space_chart() { # Display recent large files (added in last 30 days) display_recent_large_files() { - log_header "🆕 Recent Large Files (Last 30 Days)" + log_header "Recent Large Files (Last 30 Days)" echo "" if ! command -v mdfind &>/dev/null; then @@ -1041,7 +1067,10 @@ display_recent_large_files() { local dirname=$(dirname "$path" | sed "s|^$HOME|~|") local days_ago=$(( ($(date +%s) - mtime) / 86400 )) - printf " 📄 %s ${GRAY}(%s)${NC}\n" "$filename" "$human_size" + local info=$(get_file_info "$path") + local badge="${info%|*}" + + printf " %s %s ${GRAY}(%s)${NC}\n" "$badge" "$filename" "$human_size" printf " ${GRAY}%s - %d days ago${NC}\n\n" "$dirname" "$days_ago" ((count++)) @@ -1148,9 +1177,9 @@ count_directories() { display_interactive_menu() { clear_screen - log_header "🔍 Disk Space Analyzer" + log_header "Disk Space Analyzer" echo "" - echo "📂 Current: ${BLUE}$(echo "$CURRENT_PATH" | sed "s|^$HOME|~|")${NC}" + echo "Current: ${BLUE}$(echo "$CURRENT_PATH" | sed "s|^$HOME|~|")${NC}" echo "" # Show navigation hints @@ -1160,7 +1189,7 @@ display_interactive_menu() { # Display results based on view mode case "$VIEW_MODE" in "navigate") - log_header "📁 Select Directory" + log_header "Select Directory" echo "" display_directory_list "$CURSOR_POS" ;; @@ -1183,7 +1212,7 @@ display_interactive_menu() { display_file_types() { local temp_types="$TEMP_PREFIX.types" - log_header "📊 File Types Analysis" + log_header "File Types Analysis" echo "" if ! command -v mdfind &>/dev/null; then @@ -1216,7 +1245,14 @@ display_file_types() { if [[ $total_size -gt 0 ]]; then local human_size=$(bytes_to_human "$total_size") - printf " 📦 %-12s %8s (%d files)\n" "$type_name:" "$human_size" "$count" + local badge="$BADGE_FILE" + case "$type_name" in + "Videos"|"Images") badge="$BADGE_MEDIA" ;; + "Archives") badge="$BADGE_BUNDLE" ;; + "Documents") badge="$BADGE_FILE" ;; + "Audio") badge="🎵" ;; + esac + printf " %s %-12s %8s (%d files)\n" "$badge" "$type_name:" "$human_size" "$count" fi fi done @@ -1241,15 +1277,11 @@ scan_directory_contents_fast() { local max_items="${3:-16}" local show_progress="${4:-true}" - # Auto-detect optimal parallel jobs - more aggressive - local num_jobs=12 - if command -v sysctl &>/dev/null; then - local cpu_cores=$(sysctl -n hw.ncpu 2>/dev/null || echo 12) - # Use more parallel jobs for better I/O utilization - num_jobs=$((cpu_cores * 2)) - [[ $num_jobs -gt 24 ]] && num_jobs=24 - [[ $num_jobs -lt 12 ]] && num_jobs=12 - fi + # Auto-detect optimal parallel jobs using common function + local num_jobs=$(get_optimal_parallel_jobs "io") + # Cap at reasonable limits for I/O operations + [[ $num_jobs -gt 24 ]] && num_jobs=24 + [[ $num_jobs -lt 12 ]] && num_jobs=12 local temp_dirs="$output_file.dirs" local temp_files="$output_file.files" @@ -1258,7 +1290,7 @@ scan_directory_contents_fast() { if [[ "$show_progress" == "true" ]]; then printf "\033[?25l\033[H\033[J" >&2 echo "" >&2 - printf " ${BLUE}📊 ⠋ Scanning...${NC}\r" >&2 + printf " ${BLUE} | Scanning...${NC}\r" >&2 fi # Ultra-fast file scanning - batch stat for maximum speed @@ -1307,14 +1339,27 @@ scan_directory_contents_fast() { # Show progress while waiting if [[ "$show_progress" == "true" ]]; then - local spinner=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏') + local -a spinner=() + if [[ -n "${MO_SPINNER_CHARS_ARRAY:-}" ]]; then + read -r -a spinner <<< "${MO_SPINNER_CHARS_ARRAY}" + else + local spinner_chars + spinner_chars="$(mo_spinner_chars)" + local chars_len=${#spinner_chars} + for ((idx=0; idx/dev/null || kill -0 "$file_pid" 2>/dev/null ); do - printf "\r ${BLUE}📊 ${spinner[$((i % 10))]} Scanning... (%ds)${NC}" "$elapsed" >&2 + printf "\r ${BLUE}Scanning${NC} ${spinner[$((i % spin_len))]} (%ds)" "$elapsed" >&2 ((i++)) sleep 0.1 # Faster animation (100ms per frame) ((tick++)) @@ -1330,7 +1375,7 @@ scan_directory_contents_fast() { kill -9 "$file_pid" 2>/dev/null || true wait "$dir_pid" 2>/dev/null || true wait "$file_pid" 2>/dev/null || true - printf "\r ${YELLOW}⚠️ Large directory - showing estimated sizes${NC}\n" >&2 + printf "\r ${YELLOW}Large directory - showing estimated sizes${NC}\n" >&2 sleep 0.3 break fi @@ -1415,18 +1460,18 @@ show_volumes_overview() { # Collect most useful locations (quick display, no size calculation) { # Priority order for display (prioritized by typical usefulness) - [[ -d "$HOME" ]] && echo "1000|$HOME|🏠 Home Directory" - [[ -d "$HOME/Downloads" ]] && echo "900|$HOME/Downloads|📥 Downloads" - [[ -d "/Applications" ]] && echo "800|/Applications|📦 Applications" - [[ -d "$HOME/Library" ]] && echo "700|$HOME/Library|📚 User Library" - [[ -d "/Library" ]] && echo "600|/Library|📚 System Library" + [[ -d "$HOME" ]] && echo "1000|$HOME|Home Directory" + [[ -d "$HOME/Downloads" ]] && echo "900|$HOME/Downloads|Downloads" + [[ -d "/Applications" ]] && echo "800|/Applications|Applications" + [[ -d "$HOME/Library" ]] && echo "700|$HOME/Library|User Library" + [[ -d "/Library" ]] && echo "600|/Library|System Library" # External volumes (if any) if [[ -d "/Volumes" ]]; then local vol_priority=500 find /Volumes -mindepth 1 -maxdepth 1 -type d 2>/dev/null | while IFS= read -r vol; do local vol_name=$(basename "$vol") - echo "$((vol_priority))|$vol|🔌 $vol_name" + echo "$((vol_priority))|$vol|Volume: $vol_name" ((vol_priority--)) done fi @@ -1459,7 +1504,7 @@ show_volumes_overview() { output+="\033[?25l" # Hide cursor output+="\033[H\033[J" output+=$'\n' - output+="\033[0;35m💾 Select a location to explore\033[0m"$'\n' + output+="\033[0;35mSelect a location to explore\033[0m"$'\n' output+=$'\n' local idx=0 @@ -1692,7 +1737,7 @@ interactive_drill_down() { output+="\033[?25l" # Hide cursor output+="\033[H\033[J" # Clear screen output+=$'\n' - output+="\033[0;35m📊 Disk space explorer > $(echo "$current_path" | sed "s|^$HOME|~|")\033[0m"$'\n' + output+="\033[0;35mDisk space explorer > $(echo "$current_path" | sed "s|^$HOME|~|")\033[0m"$'\n' output+=$'\n' local max_show=15 # Show 15 items per page @@ -1727,37 +1772,39 @@ interactive_drill_down() { human_size=$(bytes_to_human "$size") fi - # Get icon and color - local icon="" color="${NC}" + # Determine label and color hints + local badge="$BADGE_FILE" color="${NC}" if [[ "$type" == "dir" ]]; then - icon="📁" color="${BLUE}" + badge="$BADGE_DIR" color="${BLUE}" if [[ $size -gt 10737418240 ]]; then color="${RED}" elif [[ $size -gt 1073741824 ]]; then color="${YELLOW}" fi else local ext="${name##*.}" + local info=$(get_file_info "$path") + badge="${info%|*}" case "$ext" in - dmg|iso|pkg) icon="📦" ; color="${RED}" ;; - mov|mp4|avi|mkv|webm) icon="🎬" ; color="${YELLOW}" ;; - zip|tar|gz|rar|7z) icon="🗜️" ; color="${YELLOW}" ;; - pdf) icon="📄" ;; - jpg|jpeg|png|gif|heic) icon="🖼️" ;; - key|ppt|pptx) icon="📊" ;; - log) icon="📝" ; color="${GRAY}" ;; - *) icon="📄" ;; + dmg|iso|pkg|zip|tar|gz|rar|7z) + color="${YELLOW}" + ;; + mov|mp4|avi|mkv|webm|jpg|jpeg|png|gif|heic) + color="${YELLOW}" + ;; + log) + color="${GRAY}" + ;; esac fi # Truncate name if [[ ${#name} -gt 50 ]]; then name="${name:0:47}..."; fi - # Build line with better spacing - # Icon (emoji) + 2 spaces + Right-aligned size + 4 spaces + filename + # Build line with emoji badge, size, and name local line if [[ $idx -eq $cursor ]]; then - line=$(printf " ${GREEN}▶${NC} ${color}%s %10s %s${NC}" "$icon" "$human_size" "$name") + line=$(printf " ${GREEN}▶${NC} %s%s${NC} %10s %s${NC}" "$color" "$badge" "$human_size" "$name") else - line=$(printf " ${color}%s %10s %s${NC}" "$icon" "$human_size" "$name") + line=$(printf " %s%s${NC} %10s %s${NC}" "$color" "$badge" "$human_size" "$name") fi output+="$line"$'\n' @@ -1837,7 +1884,7 @@ interactive_drill_down() { # Clear screen and show loading message printf "\033[H\033[J" echo "" - echo " ${BLUE}📄 Opening: $filename${NC}" + echo " ${BLUE}Opening file:${NC} $filename" echo "" # Try less first (best for text viewing) @@ -1869,7 +1916,7 @@ interactive_drill_down() { # Show message without flashing printf "\033[H\033[J" echo "" - echo " ${BLUE}📦 Opening: $filename${NC}" + echo " ${BLUE}Opening file:${NC} $filename" echo "" echo " ${GRAY}Launching default application...${NC}" @@ -1890,7 +1937,7 @@ interactive_drill_down() { if [[ "$open_success" != "true" ]]; then printf "\033[H\033[J" echo "" - echo " ${YELLOW}⚠️ Could not open file${NC}" + echo " ${YELLOW}Warning:${NC} Could not open file" echo "" echo " ${GRAY}File: $selected_path${NC}" echo " ${GRAY}Press any key to return...${NC}" @@ -1945,26 +1992,20 @@ interactive_drill_down() { echo "" if [[ "$type" == "dir" ]]; then - echo " ${RED}Delete folder? ${YELLOW}⚠️ This action cannot be undone!${NC}" + echo " ${RED}Delete folder? ${YELLOW}Warning:${NC} This action cannot be undone!" else - echo " ${RED}Delete file? ${YELLOW}⚠️ This action cannot be undone!${NC}" + echo " ${RED}Delete file? ${YELLOW}Warning:${NC} This action cannot be undone!" fi echo "" # Show icon based on type if [[ "$type" == "dir" ]]; then - echo " 📁 ${YELLOW}$selected_name${NC}" + echo " ${BADGE_DIR} ${YELLOW}$selected_name${NC}" else - local ext="${selected_name##*.}" - local icon="📄" - case "$ext" in - dmg|iso|pkg) icon="📦" ;; - mov|mp4|avi|mkv|webm) icon="🎬" ;; - zip|tar|gz|rar|7z) icon="🗜️" ;; - jpg|jpeg|png|gif|heic) icon="🖼️" ;; - esac - echo " $icon ${YELLOW}$selected_name${NC}" + local info=$(get_file_info "$selected_path") + local badge="${info%|*}" + echo " $badge ${YELLOW}$selected_name${NC}" fi echo " ${GRAY}Size: $human_size${NC}" @@ -1972,7 +2013,7 @@ interactive_drill_down() { if [[ "$needs_sudo" == "true" ]]; then echo "" - echo " ${YELLOW}🔐 Requires admin privileges${NC}" + echo " ${YELLOW}Warning:${NC} Requires admin privileges" fi echo "" @@ -1983,10 +2024,23 @@ interactive_drill_down() { confirm=$(read_key 2>/dev/null || echo "QUIT") if [[ "$confirm" == "ENTER" ]]; then + # Request sudo if needed before deletion + if [[ "$needs_sudo" == "true" ]]; then + printf "\033[H\033[J" + echo "" + echo "" + if ! request_sudo_access "Admin access required to delete this item"; then + echo "" + echo " ${RED}✗ Admin access denied${NC}" + sleep 1.5 + continue + fi + fi + # Show deleting message printf "\033[H\033[J" echo "" - echo " ${BLUE}🗑️ Deleting...${NC}" + echo " ${BLUE}Deleting...${NC}" echo "" # Try to delete with sudo if needed diff --git a/bin/clean.sh b/bin/clean.sh index d743b56..74cfd6f 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -4,6 +4,10 @@ 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/common.sh" @@ -12,18 +16,47 @@ source "$SCRIPT_DIR/../lib/common.sh" SYSTEM_CLEAN=false DRY_RUN=false IS_M_SERIES=$([ "$(uname -m)" = "arm64" ] && echo "true" || echo "false") + +# Constants +readonly MAX_PARALLEL_JOBS=15 # Maximum parallel background jobs +readonly TEMP_FILE_AGE_DAYS=7 # Age threshold for temp file cleanup +readonly ORPHAN_AGE_DAYS=60 # Age threshold for orphaned data +readonly SIZE_1GB_KB=1048576 # 1GB in kilobytes +readonly SIZE_1MB_KB=1024 # 1MB in kilobytes # Default whitelist patterns to avoid removing critical caches (can be extended by user) WHITELIST_PATTERNS=( "$HOME/Library/Caches/ms-playwright*" "$HOME/.cache/huggingface*" ) +WHITELIST_WARNINGS=() + # Load user-defined whitelist if [[ -f "$HOME/.config/mole/whitelist" ]]; then while IFS= read -r line; do + # Trim whitespace line="${line#${line%%[![:space:]]*}}" line="${line%${line##*[![:space:]]}}" + + # Skip empty lines and comments [[ -z "$line" || "$line" =~ ^# ]] && continue + + # Expand tilde to home directory [[ "$line" == ~* ]] && line="${line/#~/$HOME}" + + # Validate path format (allow safe characters only) + if [[ ! "$line" =~ ^[a-zA-Z0-9/_.\*~\ @-]+$ ]]; then + WHITELIST_WARNINGS+=("Invalid chars: $line") + continue + fi + + # Prevent absolute path to critical system directories + case "$line" in + /System/*|/bin/*|/sbin/*|/usr/bin/*|/usr/sbin/*) + WHITELIST_WARNINGS+=("System path: $line") + continue + ;; + esac + WHITELIST_PATTERNS+=("$line") done < "$HOME/.config/mole/whitelist" fi @@ -44,18 +77,54 @@ note_activity() { } # Cleanup background processes +CLEANUP_DONE=false cleanup() { - if [[ -n "$SUDO_KEEPALIVE_PID" ]]; then - kill "$SUDO_KEEPALIVE_PID" 2>/dev/null || true - SUDO_KEEPALIVE_PID="" + local signal="${1:-EXIT}" + local exit_code="${2:-$?}" + + # Prevent multiple executions + if [[ "$CLEANUP_DONE" == "true" ]]; then + return 0 fi + CLEANUP_DONE=true + + # Stop all spinners and clear the line if [[ -n "$SPINNER_PID" ]]; then kill "$SPINNER_PID" 2>/dev/null || true + wait "$SPINNER_PID" 2>/dev/null || true SPINNER_PID="" fi + + if [[ -n "$INLINE_SPINNER_PID" ]]; then + kill "$INLINE_SPINNER_PID" 2>/dev/null || true + wait "$INLINE_SPINNER_PID" 2>/dev/null || true + INLINE_SPINNER_PID="" + fi + + # Clear any spinner output + if [[ -t 1 ]]; then + printf "\r\033[K" + fi + + # Stop sudo keepalive + 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 + + # If interrupted, show message + if [[ "$signal" == "INT" ]] || [[ $exit_code -eq 130 ]]; then + printf "\r\033[K" + echo -e "${YELLOW}Interrupted by user${NC}" + fi } -trap cleanup EXIT INT TERM +trap 'cleanup EXIT $?' EXIT +trap 'cleanup INT 130; exit 130' INT +trap 'cleanup TERM 143; exit 143' TERM # Loading animation functions SPINNER_PID="" @@ -156,20 +225,25 @@ safe_clean() { # Show progress indicator for potentially slow operations if [[ ${#existing_paths[@]} -gt 3 ]]; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Checking $description with whitelist safety...\r" - local temp_dir=$(mktemp -d) + if [[ -t 1 ]]; then MOLE_SPINNER_PREFIX=" " start_inline_spinner "Checking items with whitelist safety..."; fi + local temp_dir=$(create_temp_dir) # Parallel processing (bash 3.2 compatible) local -a pids=() + local idx=0 for path in "${existing_paths[@]}"; do ( local size=$(du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0") local count=$(find "$path" -type f 2>/dev/null | wc -l | tr -d ' ') - echo "$size $count" > "$temp_dir/$(echo -n "$path" | shasum -a 256 | cut -d' ' -f1)" + # Use index + PID for unique filename + local tmp_file="$temp_dir/result_${idx}.$$" + echo "$size $count" > "$tmp_file" + mv "$tmp_file" "$temp_dir/result_${idx}" 2>/dev/null || true ) & pids+=($!) + ((idx++)) - if (( ${#pids[@]} >= 15 )); then + if (( ${#pids[@]} >= MAX_PARALLEL_JOBS )); then wait "${pids[0]}" 2>/dev/null || true pids=("${pids[@]:1}") fi @@ -179,10 +253,12 @@ safe_clean() { wait "$pid" 2>/dev/null || true done + # Read results using same index + idx=0 for path in "${existing_paths[@]}"; do - local hash=$(echo -n "$path" | shasum -a 256 | cut -d' ' -f1) - if [[ -f "$temp_dir/$hash" ]]; then - read -r size count < "$temp_dir/$hash" + local result_file="$temp_dir/result_${idx}" + if [[ -f "$result_file" ]]; then + read -r size count < "$result_file" 2>/dev/null || true if [[ "$count" -gt 0 && "$size" -gt 0 ]]; then if [[ "$DRY_RUN" != "true" ]]; then rm -rf "$path" 2>/dev/null || true @@ -192,12 +268,13 @@ safe_clean() { removed_any=1 fi fi + ((idx++)) done - rm -rf "$temp_dir" + # Temp dir will be auto-cleaned by cleanup_temp_files else # Show progress for small batches too (simpler jobs) - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Checking $description with whitelist safety...\r" + if [[ -t 1 ]]; then MOLE_SPINNER_PREFIX=" " start_inline_spinner "Checking items with whitelist safety..."; fi for path in "${existing_paths[@]}"; do local size_bytes=$(du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0") @@ -214,18 +291,12 @@ safe_clean() { done fi - # Clear progress indicator before showing result - [[ -t 1 ]] && echo -ne "\r\033[K" + # Clear progress / stop spinner before showing result + if [[ -t 1 ]]; then stop_inline_spinner; echo -ne "\r\033[K"; fi if [[ $removed_any -eq 1 ]]; then - local size_human - if [[ $total_size_bytes -gt 1048576 ]]; then # > 1GB - size_human=$(echo "$total_size_bytes" | awk '{printf "%.1fGB", $1/1024/1024}') - elif [[ $total_size_bytes -gt 1024 ]]; then # > 1MB - size_human=$(echo "$total_size_bytes" | awk '{printf "%.1fMB", $1/1024}') - else - size_human="${total_size_bytes}KB" - fi + # Convert KB to bytes for bytes_to_human() + local size_human=$(bytes_to_human "$((total_size_bytes * 1024))") local label="$description" if [[ ${#targets[@]} -gt 1 ]]; then @@ -250,7 +321,7 @@ safe_clean() { start_cleanup() { clear printf '\n' - echo -e "${PURPLE}🧹 Clean Your Mac${NC}" + echo -e "${PURPLE}Clean Your Mac${NC}" if [[ "$DRY_RUN" != "true" && -t 0 ]]; then printf '\n' echo -e "${YELLOW}Tip:${NC} Safety first—run 'mo clean --dry-run'. Important Macs should stop." @@ -258,50 +329,65 @@ start_cleanup() { if [[ "$DRY_RUN" == "true" ]]; then echo "" - echo -e "${YELLOW}🧪 Dry Run mode:${NC} showing what would be removed (no deletions)." + echo -e "${YELLOW}Dry Run mode:${NC} showing what would be removed (no deletions)." echo "" SYSTEM_CLEAN=false return fi if [[ -t 0 ]]; then - printf '\n' - echo -e "${BLUE}System cleanup? Password to include (Enter skips)${NC}" - printf "${BLUE}> ${NC}" - read -s password + echo "" + echo -ne "${BLUE}System cleanup? ${GRAY}Enter to continue, any key to skip${NC} " + + # Use IFS= and read without -n to allow Ctrl+C to work properly + IFS= read -r -s -n 1 choice + local read_status=$? echo "" - if [[ -n "$password" ]] && echo "$password" | sudo -S true 2>/dev/null; then - SYSTEM_CLEAN=true - # Start sudo keepalive with error handling - ( - local retry_count=0 - while true; do - if ! sudo -n true 2>/dev/null; then - ((retry_count++)) - if [[ $retry_count -ge 3 ]]; then - exit 1 + # If read was interrupted (Ctrl+C), exit cleanly + if [[ $read_status -ne 0 ]]; then + exit 130 + fi + + # Enter or y = yes, do system cleanup + if [[ -z "$choice" ]] || [[ "$choice" == $'\n' ]] || [[ "$choice" =~ ^[Yy]$ ]]; then + echo "" + if request_sudo_access "System cleanup requires admin access"; then + SYSTEM_CLEAN=true + echo -e "${GREEN}✓ Admin access granted${NC}" + # Start sudo keepalive with error handling + ( + local retry_count=0 + while true; do + if ! sudo -n true 2>/dev/null; then + ((retry_count++)) + if [[ $retry_count -ge 3 ]]; then + exit 1 + fi + sleep 5 + continue fi - sleep 5 - continue - fi - retry_count=0 - sleep 30 - kill -0 "$$" 2>/dev/null || exit - done - ) 2>/dev/null & - SUDO_KEEPALIVE_PID=$! - else - SYSTEM_CLEAN=false - if [[ -n "$password" ]]; then + retry_count=0 + sleep 30 + kill -0 "$$" 2>/dev/null || exit + done + ) 2>/dev/null & + SUDO_KEEPALIVE_PID=$! + else + SYSTEM_CLEAN=false echo "" - echo -e "${YELLOW}⚠️ Invalid password, continuing with user-level cleanup${NC}" + echo -e "${YELLOW}Authentication failed, continuing with user-level cleanup${NC}" fi + else + # Any other key = no system cleanup + SYSTEM_CLEAN=false + echo "" + echo -e "Skipped system cleanup, user-level only" fi else SYSTEM_CLEAN=false echo "" - echo -e "${BLUE}ℹ${NC} Running in non-interactive mode" + echo -e " Running in non-interactive mode" echo " • System-level cleanup skipped (requires interaction)" echo " • User-level cleanup will proceed automatically" echo "" @@ -310,7 +396,7 @@ start_cleanup() { perform_cleanup() { echo "" - echo "🍎 $(detect_architecture) | 💾 Free space: $(get_free_space)" + echo "$(detect_architecture) | Free space: $(get_free_space)" # Get initial space space_before=$(df / | tail -1 | awk '{print $4}') @@ -328,14 +414,34 @@ perform_cleanup() { sudo find /Library/Caches -name "*.cache" -delete 2>/dev/null || true sudo find /Library/Caches -name "*.tmp" -delete 2>/dev/null || true sudo find /Library/Caches -type f -name "*.log" -delete 2>/dev/null || true - sudo rm -rf /tmp/* 2>/dev/null && log_success "System temp files" || true - sudo rm -rf /var/tmp/* 2>/dev/null && log_success "System var temp" || true + + # Clean old temp files only (avoid breaking running processes) + local tmp_cleaned=0 + local tmp_count=$(sudo find /tmp -type f -mtime +${TEMP_FILE_AGE_DAYS} 2>/dev/null | wc -l | tr -d ' ') + if [[ "$tmp_count" -gt 0 ]]; then + sudo find /tmp -type f -mtime +${TEMP_FILE_AGE_DAYS} -delete 2>/dev/null || true + tmp_cleaned=1 + fi + local var_tmp_count=$(sudo find /var/tmp -type f -mtime +${TEMP_FILE_AGE_DAYS} 2>/dev/null | wc -l | tr -d ' ') + if [[ "$var_tmp_count" -gt 0 ]]; then + sudo find /var/tmp -type f -mtime +${TEMP_FILE_AGE_DAYS} -delete 2>/dev/null || true + tmp_cleaned=1 + fi + [[ $tmp_cleaned -eq 1 ]] && log_success "Old system temp files (${TEMP_FILE_AGE_DAYS}+ days)" + sudo rm -rf /Library/Updates/* 2>/dev/null || true log_success "System library caches and updates" end_section fi + # Show whitelist warnings if any + if [[ ${#WHITELIST_WARNINGS[@]} -gt 0 ]]; then + echo "" + for warning in "${WHITELIST_WARNINGS[@]}"; do + echo -e " ${YELLOW}☼${NC} Whitelist: $warning" + done + fi # ===== 2. User essentials ===== start_section "System essentials" @@ -343,11 +449,22 @@ perform_cleanup() { safe_clean ~/Library/Logs/* "User app logs" safe_clean ~/.Trash/* "Trash" - # Empty the trash on all mounted volumes + # Empty trash on mounted volumes (skip network/readonly volumes) if [[ -d "/Volumes" ]]; then for volume in /Volumes/*; do - if [[ -d "$volume" && -d "$volume/.Trashes" ]]; then - find "$volume/.Trashes" -mindepth 1 -maxdepth 1 -exec rm -rf {} \; 2>/dev/null || true + [[ -d "$volume" && -d "$volume/.Trashes" && -w "$volume" ]] || continue + + # Skip network volumes + local fs_type=$(df -T "$volume" 2>/dev/null | tail -1 | awk '{print $2}') + case "$fs_type" in + nfs|smbfs|afpfs|cifs|webdav) continue ;; + esac + + # Verify volume is mounted + if mount | grep -q "on $volume "; then + if [[ "$DRY_RUN" != "true" ]]; then + find "$volume/.Trashes" -mindepth 1 -maxdepth 1 -exec rm -rf {} \; 2>/dev/null || true + fi fi done fi @@ -449,10 +566,11 @@ perform_cleanup() { start_section "Developer tools" # Node.js ecosystem if command -v npm >/dev/null 2>&1; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning npm cache...\r" - npm cache clean --force >/dev/null 2>&1 || true - [[ -t 1 ]] && echo -ne "\r\033[K" - echo -e " ${GREEN}✓${NC} npm cache cleaned" + if [[ "$DRY_RUN" != "true" ]]; then + clean_tool_cache "npm cache" npm cache clean --force + else + echo -e " ${YELLOW}→${NC} npm cache (would clean)" + fi note_activity fi @@ -463,10 +581,11 @@ perform_cleanup() { # Python ecosystem if command -v pip3 >/dev/null 2>&1; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning pip cache...\r" - pip3 cache purge >/dev/null 2>&1 || true - [[ -t 1 ]] && echo -ne "\r\033[K" - echo -e " ${GREEN}✓${NC} pip cache cleaned" + if [[ "$DRY_RUN" != "true" ]]; then + clean_tool_cache "pip cache" pip3 cache purge + else + echo -e " ${YELLOW}→${NC} pip cache (would clean)" + fi note_activity fi @@ -476,11 +595,11 @@ perform_cleanup() { # Go ecosystem if command -v go >/dev/null 2>&1; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Go cache...\r" - go clean -modcache >/dev/null 2>&1 || true - go clean -cache >/dev/null 2>&1 || true - [[ -t 1 ]] && echo -ne "\r\033[K" - echo -e " ${GREEN}✓${NC} Go cache cleaned" + if [[ "$DRY_RUN" != "true" ]]; then + clean_tool_cache "Go cache" bash -c 'go clean -modcache >/dev/null 2>&1 || true; go clean -cache >/dev/null 2>&1 || true' + else + echo -e " ${YELLOW}→${NC} Go cache (would clean)" + fi note_activity fi @@ -492,10 +611,11 @@ perform_cleanup() { # Docker (only clean build cache, preserve images and volumes) if command -v docker >/dev/null 2>&1; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Docker build cache...\r" - docker builder prune -af >/dev/null 2>&1 || true - [[ -t 1 ]] && echo -ne "\r\033[K" - echo -e " ${GREEN}✓${NC} Docker build cache cleaned" + if [[ "$DRY_RUN" != "true" ]]; then + clean_tool_cache "Docker build cache" docker builder prune -af + else + echo -e " ${YELLOW}→${NC} Docker build cache (would clean)" + fi note_activity fi @@ -513,10 +633,11 @@ perform_cleanup() { safe_clean /opt/homebrew/var/homebrew/locks/* "Homebrew lock files (M series)" safe_clean /usr/local/var/homebrew/locks/* "Homebrew lock files (Intel)" if command -v brew >/dev/null 2>&1; then - [[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Homebrew...\r" - brew cleanup >/dev/null 2>&1 || true - [[ -t 1 ]] && echo -ne "\r\033[K" - echo -e " ${GREEN}✓${NC} Homebrew cache cleaned" + if [[ "$DRY_RUN" != "true" ]]; then + clean_tool_cache "Homebrew cleanup" brew cleanup + else + echo -e " ${YELLOW}→${NC} Homebrew (would cleanup)" + fi note_activity fi @@ -870,13 +991,13 @@ perform_cleanup() { # Safeguards: retains unusual locations in use, recent apps, and critical/licensed data start_section "Orphaned app data cleanup" - local -r ORPHAN_AGE_THRESHOLD=60 + local -r ORPHAN_AGE_THRESHOLD=$ORPHAN_AGE_DAYS # Build a comprehensive list of installed application bundle identifiers - echo -n " ${BLUE}◎${NC} Scanning installed applications..." - local installed_bundles=$(mktemp) - local running_bundles=$(mktemp) - local launch_agents=$(mktemp) + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning installed applications..." # ensure spinner function exists above + local installed_bundles=$(create_temp_file) + local running_bundles=$(create_temp_file) + local launch_agents=$(create_temp_file) # Scan multiple possible application locations to avoid false positives local -a search_paths=( @@ -886,18 +1007,26 @@ perform_cleanup() { "/System/Library/CoreServices/Applications" "/Library/Application Support" "$HOME/Library/Application Support" + "/Users/Shared/Applications" + "/Applications/Utilities" ) # Add Homebrew paths if they exist [[ -d "/opt/homebrew/Caskroom" ]] && search_paths+=("/opt/homebrew/Caskroom") [[ -d "/usr/local/Caskroom" ]] && search_paths+=("/usr/local/Caskroom") [[ -d "/opt/homebrew/Cellar" ]] && search_paths+=("/opt/homebrew/Cellar") + [[ -d "/usr/local/Cellar" ]] && search_paths+=("/usr/local/Cellar") # Add common developer paths [[ -d "$HOME/Developer" ]] && search_paths+=("$HOME/Developer") [[ -d "$HOME/Projects" ]] && search_paths+=("$HOME/Projects") [[ -d "$HOME/Downloads" ]] && search_paths+=("$HOME/Downloads") + # Add other common third-party install locations + [[ -d "/opt/apps" ]] && search_paths+=("/opt/apps") + [[ -d "/opt/local/Applications" ]] && search_paths+=("/opt/local/Applications") + [[ -d "/usr/local/apps" ]] && search_paths+=("/usr/local/apps") + # Scan for .app bundles in all search paths (with depth limit for performance) for search_path in "${search_paths[@]}"; do if [[ -d "$search_path" ]]; then @@ -905,10 +1034,20 @@ perform_cleanup() { [[ -f "$app/Contents/Info.plist" ]] || continue bundle_id=$(defaults read "$app/Contents/Info.plist" CFBundleIdentifier 2>/dev/null || echo "") [[ -n "$bundle_id" ]] && echo "$bundle_id" >> "$installed_bundles" - done < <(find "$search_path" -type d -name "*.app" 2>/dev/null || true) + done < <(find "$search_path" -maxdepth 3 -type d -name "*.app" 2>/dev/null || true) fi done + # Use Spotlight as fallback to catch apps in unusual locations + # This significantly reduces false positives + if command -v mdfind >/dev/null 2>&1; then + while IFS= read -r app; do + [[ -f "$app/Contents/Info.plist" ]] || continue + bundle_id=$(defaults read "$app/Contents/Info.plist" CFBundleIdentifier 2>/dev/null || echo "") + [[ -n "$bundle_id" ]] && echo "$bundle_id" >> "$installed_bundles" + done < <(mdfind "kMDItemKind == 'Application'" 2>/dev/null | grep "\.app$" || true) + fi + # Get running applications (if an app is running, it's definitely not orphaned) local running_apps=$(osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2>/dev/null || echo "") echo "$running_apps" | tr ',' '\n' | sed 's/^ *//;s/ *$//' | grep -v '^$' > "$running_bundles" @@ -925,7 +1064,8 @@ perform_cleanup() { mv "${installed_bundles}.final" "$installed_bundles" local app_count=$(wc -l < "$installed_bundles" | tr -d ' ') - echo " ${GREEN}✓${NC} Found $app_count active/installed apps" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $app_count active/installed apps" # Track statistics local orphaned_count=0 @@ -983,7 +1123,7 @@ perform_cleanup() { } # Clean orphaned caches - echo -n " ${BLUE}◎${NC} Scanning orphaned caches..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned caches..." local cache_found=0 if ls ~/Library/Caches/com.* >/dev/null 2>&1; then for cache_dir in ~/Library/Caches/com.* ~/Library/Caches/org.* ~/Library/Caches/net.* ~/Library/Caches/io.*; do @@ -999,10 +1139,11 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $cache_found orphaned caches" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $cache_found orphaned caches" # Clean orphaned logs - echo -n " ${BLUE}◎${NC} Scanning orphaned logs..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned logs..." local logs_found=0 if ls ~/Library/Logs/com.* >/dev/null 2>&1; then for log_dir in ~/Library/Logs/com.* ~/Library/Logs/org.* ~/Library/Logs/net.* ~/Library/Logs/io.*; do @@ -1018,10 +1159,11 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $logs_found orphaned log directories" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $logs_found orphaned log directories" # Clean orphaned saved states - echo -n " ${BLUE}◎${NC} Scanning orphaned saved states..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned saved states..." local states_found=0 if ls ~/Library/Saved\ Application\ State/*.savedState >/dev/null 2>&1; then for state_dir in ~/Library/Saved\ Application\ State/*.savedState; do @@ -1037,14 +1179,15 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $states_found orphaned saved states" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $states_found orphaned saved states" # Clean orphaned containers # NOTE: Container cleanup is DISABLED by default due to naming mismatch issues # Some apps create containers with names that don't strictly match their Bundle ID, # especially when system extensions are registered. This can cause false positives. # To avoid deleting data from installed apps, we skip container cleanup. - echo -n " ${BLUE}◎${NC} Scanning orphaned containers..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned containers..." local containers_found=0 if ls ~/Library/Containers/com.* >/dev/null 2>&1; then # Count potential orphaned containers but don't delete them @@ -1061,10 +1204,11 @@ perform_cleanup() { fi done fi - echo " ${BLUE}○${NC} Skipped $containers_found potential orphaned containers" + stop_inline_spinner + echo -e " ${BLUE}○${NC} Skipped $containers_found potential orphaned containers" # Clean orphaned WebKit data - echo -n " ${BLUE}◎${NC} Scanning orphaned WebKit data..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned WebKit data..." local webkit_found=0 if ls ~/Library/WebKit/com.* >/dev/null 2>&1; then for webkit_dir in ~/Library/WebKit/com.* ~/Library/WebKit/org.* ~/Library/WebKit/net.* ~/Library/WebKit/io.*; do @@ -1080,10 +1224,11 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $webkit_found orphaned WebKit data" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $webkit_found orphaned WebKit data" # Clean orphaned HTTP storages - echo -n " ${BLUE}◎${NC} Scanning orphaned HTTP storages..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned HTTP storages..." local http_found=0 if ls ~/Library/HTTPStorages/com.* >/dev/null 2>&1; then for http_dir in ~/Library/HTTPStorages/com.* ~/Library/HTTPStorages/org.* ~/Library/HTTPStorages/net.* ~/Library/HTTPStorages/io.*; do @@ -1099,10 +1244,11 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $http_found orphaned HTTP storages" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $http_found orphaned HTTP storages" # Clean orphaned cookies - echo -n " ${BLUE}◎${NC} Scanning orphaned cookies..." + MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned cookies..." local cookies_found=0 if ls ~/Library/Cookies/*.binarycookies >/dev/null 2>&1; then for cookie_file in ~/Library/Cookies/*.binarycookies; do @@ -1118,7 +1264,8 @@ perform_cleanup() { fi done fi - echo " ${GREEN}✓${NC} Found $cookies_found orphaned cookie files" + stop_inline_spinner + echo -e " ${GREEN}✓${NC} Found $cookies_found orphaned cookie files" # Calculate total orphaned_count=$((cache_found + logs_found + states_found + containers_found + webkit_found + http_found + cookies_found)) @@ -1156,8 +1303,8 @@ perform_cleanup() { if [[ -n "${backup_kb:-}" && "$backup_kb" -gt 102400 ]]; then backup_human=$(du -sh "$backup_dir" 2>/dev/null | awk '{print $1}') note_activity - echo -e " ${BLUE}💾${NC} Found ${GREEN}${backup_human}${NC} iOS backups" - echo -e " ${YELLOW}💡${NC} You can delete them manually: ${backup_dir}" + echo -e " Found ${GREEN}${backup_human}${NC} iOS backups" + echo -e " You can delete them manually: ${backup_dir}" fi fi end_section @@ -1169,67 +1316,45 @@ perform_cleanup() { echo "" echo "====================================================================" if [[ "$DRY_RUN" == "true" ]]; then - echo "🧪 DRY RUN COMPLETE!" + echo "DRY RUN COMPLETE!" else - echo "🎉 CLEANUP COMPLETE!" + echo "CLEANUP COMPLETE!" fi if [[ $total_size_cleaned -gt 0 ]]; then local freed_gb=$(echo "$total_size_cleaned" | awk '{printf "%.2f", $1/1024/1024}') if [[ "$DRY_RUN" == "true" ]]; then - echo "💾 Potential reclaimable space: ${GREEN}${freed_gb}GB${NC} (no changes made) | Free space now: $(get_free_space)" + echo "Potential reclaimable space: ${GREEN}${freed_gb}GB${NC} (no changes made) | Free space now: $(get_free_space)" else - echo "💾 Space freed: ${GREEN}${freed_gb}GB${NC} | Free space now: $(get_free_space)" + echo "Space freed: ${GREEN}${freed_gb}GB${NC} | Free space now: $(get_free_space)" fi if [[ "$DRY_RUN" != "true" ]]; then if [[ $(echo "$freed_gb" | awk '{print ($1 >= 1) ? 1 : 0}') -eq 1 ]]; then local movies=$(echo "$freed_gb" | awk '{printf "%.0f", $1/4.5}') if [[ $movies -gt 0 ]]; then - echo "🎬 That's like ~$movies 4K movies worth of space!" + echo "That's like ~$movies 4K movies worth of space!" fi fi fi else if [[ "$DRY_RUN" == "true" ]]; then - echo "💾 No significant reclaimable space detected (already clean) | Free space: $(get_free_space)" + echo "No significant reclaimable space detected (already clean) | Free space: $(get_free_space)" else - echo "💾 No significant space was freed (system was already clean) | Free space: $(get_free_space)" + echo "No significant space was freed (system was already clean) | Free space: $(get_free_space)" fi fi if [[ $files_cleaned -gt 0 && $total_items -gt 0 ]]; then - echo "📊 Files cleaned: $files_cleaned | Categories processed: $total_items" + printf "Files cleaned: %s | Categories processed: %s\n" "$files_cleaned" "$total_items" elif [[ $files_cleaned -gt 0 ]]; then - echo "📊 Files cleaned: $files_cleaned" + printf "Files cleaned: %s\n" "$files_cleaned" elif [[ $total_items -gt 0 ]]; then - echo "🗂️ Categories processed: $total_items" + printf "Categories processed: %s\n" "$total_items" fi - - if [[ "$SYSTEM_CLEAN" != "true" ]]; then - echo "" - echo -e "${BLUE}💡 For deeper cleanup, run with admin password next time${NC}" - fi - - echo "====================================================================" + printf "====================================================================\n" } -# Cleanup function - restore cursor on exit -cleanup() { - # Restore cursor - show_cursor - # Kill any background processes - if [[ -n "${SUDO_KEEPALIVE_PID:-}" ]]; then - kill "$SUDO_KEEPALIVE_PID" 2>/dev/null || true - fi - if [[ -n "${SPINNER_PID:-}" ]]; then - kill "$SPINNER_PID" 2>/dev/null || true - fi - exit "${1:-0}" -} - -# Set trap for cleanup on exit -trap cleanup EXIT INT TERM main() { # Parse args (only dry-run and help for minimal impact) @@ -1259,8 +1384,8 @@ main() { esac done - hide_cursor start_cleanup + hide_cursor perform_cleanup show_cursor } diff --git a/bin/uninstall.sh b/bin/uninstall.sh index 44be462..d3691f7 100755 --- a/bin/uninstall.sh +++ b/bin/uninstall.sh @@ -8,6 +8,10 @@ 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/common.sh" @@ -111,19 +115,21 @@ scan_applications() { # Cache is valid if: age < TTL AND app count matches if [[ $cache_age -lt $cache_ttl && "$cached_app_count" == "$current_app_count" ]]; then - echo "Using cached app list (${cache_age}s old, $current_app_count apps) ✓" >&2 + # Only show cache info in debug mode + [[ -n "${MOLE_DEBUG:-}" ]] && echo "Using cached app list (${cache_age}s old, $current_app_count apps) ✓" >&2 echo "$cache_file" return 0 fi fi - local temp_file=$(mktemp) + local temp_file=$(mktemp_file) + echo "" >&2 # Add space before scanning output without breaking stdout return # Pre-cache current epoch to avoid repeated calls local current_epoch=$(date "+%s") - # Spinner for scanning feedback - local spinner_chars="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" + # Spinner for scanning feedback (simple ASCII for compatibility) + local spinner_chars="|/-\\" local spinner_idx=0 # First pass: quickly collect all valid app paths and bundle IDs @@ -289,8 +295,8 @@ scan_applications() { pids+=($!) # Update progress with spinner - local spinner_char="${spinner_chars:$((spinner_idx % 10)):1}" - echo -ne "\r🗑️ ${spinner_char} Scanning... $app_count/$total_apps" >&2 + local spinner_char="${spinner_chars:$((spinner_idx % 4)):1}" + echo -ne "\r\033[K ${spinner_char} Scanning applications... $app_count/$total_apps" >&2 ((spinner_idx++)) # Wait if we've hit max parallel limit @@ -305,7 +311,7 @@ scan_applications() { wait "$pid" 2>/dev/null done - echo -e "\r🗑️ ✓ Found $app_count applications " >&2 + echo -e "\r\033[K ✓ Found $app_count applications" >&2 echo "" >&2 # Check if we found any applications @@ -315,13 +321,19 @@ scan_applications() { fi # Sort by last used (oldest first) and cache the result - sort -t'|' -k1,1n "$temp_file" > "${temp_file}.sorted" + sort -t'|' -k1,1n "$temp_file" > "${temp_file}.sorted" || { rm -f "$temp_file"; return 1; } rm -f "$temp_file" # Update cache with app count metadata cp "${temp_file}.sorted" "$cache_file" 2>/dev/null || true echo "$current_app_count" > "$cache_meta" 2>/dev/null || true - echo "${temp_file}.sorted" + + # Verify sorted file exists before returning + if [[ -f "${temp_file}.sorted" ]]; then + echo "${temp_file}.sorted" + else + return 1 + fi } # Load applications into arrays @@ -374,7 +386,6 @@ uninstall_applications() { IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app" echo "" - log_info "Processing: $app_name" # Check if app is running if pgrep -f "$app_name" >/dev/null 2>&1; then @@ -470,7 +481,7 @@ uninstall_applications() { log_success "$app_name uninstalled successfully" else - log_info "Skipped $app_name" + echo -e " ${BLUE}❂${NC} Skipped $app_name" fi done @@ -490,7 +501,7 @@ uninstall_applications() { log_success "Freed $freed_display of disk space" fi - echo "📊 Applications uninstalled: $files_cleaned" + echo "Applications uninstalled: $files_cleaned" ((total_size_cleaned += total_size_freed)) } @@ -513,6 +524,7 @@ main() { local apps_file=$(scan_applications) if [[ ! -f "$apps_file" ]]; then + echo "" log_error "Failed to scan applications" return 1 fi @@ -532,20 +544,26 @@ main() { # Restore cursor and show a concise summary before confirmation show_cursor clear - printf '\n' local selection_count=${#selected_apps[@]} - echo -e "${PURPLE}🗑️ Selected ${selection_count} app(s)${NC}" - - if [[ $selection_count -gt 0 ]]; then - for selected_app in "${selected_apps[@]}"; do - IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app" - echo " • $app_name ($size)" - done - else - echo -e "${GRAY}No apps chosen.${NC}" + if [[ $selection_count -eq 0 ]]; then + echo "No apps selected"; rm -f "$apps_file"; return 0 fi + # Compact one-line summary (list up to 3 names, aggregate rest) + local names=() + local idx=0 + for selected_app in "${selected_apps[@]}"; do + IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app" + if (( idx < 3 )); then + names+=("${app_name}(${size})") + fi + ((idx++)) + done + local extra=$((selection_count-3)) + local list="${names[*]}" + [[ $extra -gt 0 ]] && list+=" +${extra}" + echo "◎ ${selection_count} apps: ${list}" - # Execute batch uninstallation, confirmation handled in batch_uninstall_applications + # Execute batch uninstallation (handles confirmation) batch_uninstall_applications # Cleanup diff --git a/install.sh b/install.sh index 986eaad..8b8f99c 100755 --- a/install.sh +++ b/install.sh @@ -10,6 +10,18 @@ BLUE='\033[0;34m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' + +# Simple spinner +_SPINNER_PID="" +start_line_spinner() { + local msg="$1"; [[ ! -t 1 ]] && { echo -e "${BLUE}|${NC} $msg"; return; } + local chars="${MO_SPINNER_CHARS:-|/-\\}"; [[ -z "$chars" ]] && chars='|/-\\' + local i=0 + ( while true; do c="${chars:$((i % ${#chars})):1}"; printf "\r ${BLUE}%s${NC} %s" "$c" "$msg"; ((i++)); sleep 0.12; done ) & + _SPINNER_PID=$! +} +stop_line_spinner() { if [[ -n "$_SPINNER_PID" ]]; then kill "$_SPINNER_PID" 2>/dev/null || true; wait "$_SPINNER_PID" 2>/dev/null || true; _SPINNER_PID=""; printf "\r"; fi; } + # Verbosity (0 = quiet, 1 = verbose) VERBOSE=1 @@ -81,14 +93,14 @@ resolve_source_dir() { # 3) Fallback: fetch repository to a temp directory (works for curl | bash) local tmp - tmp="$(mktemp -d)" + tmp="$(mktemp_dir)" # Expand tmp now so trap doesn't depend on local scope trap "rm -rf '$tmp'" EXIT - echo -e "${BLUE}◎${NC} Fetching Mole source..." + start_line_spinner "Fetching Mole source..." if command -v curl >/dev/null 2>&1; then - # Download main branch tarball if curl -fsSL -o "$tmp/mole.tar.gz" "https://github.com/tw93/mole/archive/refs/heads/main.tar.gz"; then + stop_line_spinner tar -xzf "$tmp/mole.tar.gz" -C "$tmp" # Extracted folder name: mole-main if [[ -d "$tmp/mole-main" ]]; then @@ -97,14 +109,17 @@ resolve_source_dir() { fi fi fi + stop_line_spinner - # 4) Fallback to git if available + start_line_spinner "Cloning Mole source..." if command -v git >/dev/null 2>&1; then if git clone --depth=1 https://github.com/tw93/mole.git "$tmp/mole" >/dev/null 2>&1; then + stop_line_spinner SOURCE_DIR="$tmp/mole" return 0 fi fi + stop_line_spinner log_error "Failed to fetch source files. Ensure curl or git is available." exit 1 @@ -223,9 +238,7 @@ install_files() { # Copy main executable when destination differs if [[ -f "$SOURCE_DIR/mole" ]]; then - if [[ "$source_dir_abs" == "$install_dir_abs" ]]; then - log_info "Mole binary already present in $INSTALL_DIR" - else + if [[ "$source_dir_abs" != "$install_dir_abs" ]]; then if [[ "$INSTALL_DIR" == "/usr/local/bin" ]] && [[ ! -w "$INSTALL_DIR" ]]; then sudo cp "$SOURCE_DIR/mole" "$INSTALL_DIR/mole" sudo chmod +x "$INSTALL_DIR/mole" @@ -428,9 +441,7 @@ uninstall_mole() { echo " $CONFIG_DIR" else echo "" - read -p "Remove configuration directory $CONFIG_DIR? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then + read -p "Remove configuration directory $CONFIG_DIR? (y/N): " -n 1 -r; echo ""; if [[ $REPLY =~ ^[Yy]$ ]]; then rm -rf "$CONFIG_DIR" log_success "Removed configuration directory" else @@ -476,10 +487,10 @@ perform_update() { update_via_homebrew "$VERSION" else # Fallback: inline implementation - echo -e "${BLUE}◎${NC} Updating Homebrew..." + echo -e "${BLUE}|${NC} Updating Homebrew..." brew update 2>&1 | grep -Ev "^(==>|Already up-to-date)" || true - echo -e "${BLUE}◎${NC} Upgrading Mole..." + echo -e "${BLUE}|${NC} Upgrading Mole..." local upgrade_output upgrade_output=$(brew upgrade mole 2>&1) || true diff --git a/lib/app_selector.sh b/lib/app_selector.sh index 4ed1264..7f12058 100755 --- a/lib/app_selector.sh +++ b/lib/app_selector.sh @@ -37,11 +37,12 @@ select_apps_for_uninstall() { menu_options+=("$(format_app_display "$display_name" "$size" "$last_used")") done - echo "" + # Clear screen before menu (alternate screen preserves main screen) + clear_screen # Use paginated menu - result will be stored in MOLE_SELECTION_RESULT MOLE_SELECTION_RESULT="" - paginated_multi_select "🗑️ Select Apps to Remove" "${menu_options[@]}" + paginated_multi_select "Select Apps to Remove" "${menu_options[@]}" local exit_code=$? if [[ $exit_code -ne 0 ]]; then @@ -75,4 +76,4 @@ select_apps_for_uninstall() { if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then echo "This is a library file. Source it from other scripts." >&2 exit 1 -fi \ No newline at end of file +fi diff --git a/lib/batch_uninstall.sh b/lib/batch_uninstall.sh index 5550401..5e63898 100755 --- a/lib/batch_uninstall.sh +++ b/lib/batch_uninstall.sh @@ -1,5 +1,11 @@ #!/bin/bash +set -euo pipefail + +# Ensure common.sh is loaded +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +[[ -z "${MOLE_COMMON_LOADED:-}" ]] && source "$SCRIPT_DIR/lib/common.sh" + # Batch uninstall functionality with minimal confirmations # Replaces the overly verbose individual confirmation approach # Note: find_app_files() and calculate_total_size() functions now in lib/common.sh @@ -20,18 +26,9 @@ batch_uninstall_applications() { local -a app_details=() echo "" - - # Show analyzing message with spinner - local spinner_chars="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" - local spinner_idx=0 - local analyzed=0 - + # Silent analysis without spinner output (avoid visual flicker) for selected_app in "${selected_apps[@]}"; do - # Update spinner - local spinner_char="${spinner_chars:$((spinner_idx % 10)):1}" - ((analyzed++)) - echo -ne "\r🗑️ ${spinner_char} Analyzing... $analyzed/${#selected_apps[@]}" >&2 - ((spinner_idx++)) + [[ -z "$selected_app" ]] && continue IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app" # Check if app is running @@ -57,36 +54,31 @@ batch_uninstall_applications() { app_details+=("$app_name|$app_path|$bundle_id|$total_kb|$encoded_files") done - # Clear spinner line - echo -ne "\r\033[K" >&2 - - # Format size display - if [[ $total_estimated_size -gt 1048576 ]]; then - local size_display=$(echo "$total_estimated_size" | awk '{printf "%.2fGB", $1/1024/1024}') - elif [[ $total_estimated_size -gt 1024 ]]; then - local size_display=$(echo "$total_estimated_size" | awk '{printf "%.1fMB", $1/1024}') - else - local size_display="${total_estimated_size}KB" - fi + # Format size display (convert KB to bytes for bytes_to_human()) + local size_display=$(bytes_to_human "$((total_estimated_size * 1024))") # Request sudo access if needed (do this before confirmation) if [[ ${#sudo_apps[@]} -gt 0 ]]; then - echo "" - echo -e "${YELLOW}🔐 Admin privileges required for: ${BLUE}${sudo_apps[*]}${NC}" - echo -e "${BLUE}You will be prompted for your password before proceeding...${NC}" - if ! sudo -v; then - log_error "Administrator privileges required but not granted" - return 1 + # Check if sudo is already cached + if sudo -n true 2>/dev/null; then + echo "◎ Admin access confirmed for: ${sudo_apps[*]}" + else + echo "◎ Admin required for: ${sudo_apps[*]}" + echo "" + if ! request_sudo_access "Uninstalling system apps requires admin access"; then + echo "" + log_error "Admin access denied" + return 1 + fi + echo "" + echo "✓ Admin access granted" fi - # Keep sudo alive during the process + echo "◎ Gathering targets..." (while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null) & local sudo_keepalive_pid=$! - - # Append keepalive cleanup to existing traps without overriding them local _trap_cleanup_cmd="kill $sudo_keepalive_pid 2>/dev/null || true; wait $sudo_keepalive_pid 2>/dev/null || true" for signal in EXIT INT TERM; do - local existing_trap - existing_trap=$(trap -p "$signal" | awk -F"'" '{print $2}') + local existing_trap; existing_trap=$(trap -p "$signal" | awk -F"'" '{print $2}') if [[ -n "$existing_trap" ]]; then trap "$existing_trap; $_trap_cleanup_cmd" "$signal" else @@ -96,164 +88,98 @@ batch_uninstall_applications() { fi # Show summary and get batch confirmation - printf '\n' local app_total=${#selected_apps[@]} - echo -e "${YELLOW}📦 Remove ${BLUE}${app_total}${YELLOW} app(s), free about ${GREEN}$size_display${NC}" if [[ ${#running_apps[@]} -gt 0 ]]; then - echo -e "${YELLOW}⚠️ Will force-quit: ${RED}${running_apps[*]}${NC}" + echo -n "${BLUE}◎ Remove ${app_total} app(s) (${size_display}) | Quit: ${running_apps[*]} | Enter=go / ESC=q:${NC} " + else + echo -n "${BLUE}◎ Remove ${app_total} app(s) (${size_display}) | Enter=go / ESC=q:${NC} " fi - printf "%b" "${BLUE}Continue? Press Enter to proceed, or q/ESC to cancel:${NC} " - local confirm_key="" - IFS= read -r -s -n1 confirm_key || confirm_key="" - if [[ "$confirm_key" == $'\e' ]]; then - while IFS= read -r -s -n1 -t 0 rest; do - [[ -z "$rest" || "$rest" == $'\n' ]] && break - done - fi - echo "" - - local cancel=false - case "$confirm_key" in - ""|$'\n'|$'\r') ;; - $'\e'|"q"|"Q") cancel=true ;; - *) cancel=true ;; + IFS= read -r -s -n1 key || key="" + case "$key" in + $'\e'|q|Q) echo ""; return 0 ;; + ""|$'\n'|$'\r'|y|Y) echo "" ;; + *) echo ""; return 0 ;; esac - if [[ "$cancel" == true ]]; then - log_info "Uninstallation cancelled" - # Clean up sudo keepalive if it was started - if [[ -n "${sudo_keepalive_pid:-}" ]]; then - kill "$sudo_keepalive_pid" 2>/dev/null || true - fi - return 0 - fi - - echo -e "${PURPLE}⚡ Starting uninstallation in 3 seconds...${NC} ${YELLOW}(Press Ctrl+C to abort)${NC}" - sleep 1 && echo -e "${PURPLE}⚡ ${BLUE}2${PURPLE}...${NC}" - sleep 1 && echo -e "${PURPLE}⚡ ${BLUE}1${PURPLE}...${NC}" - sleep 1 - echo -e "${GREEN}✨ Let's go!${NC}" + echo -n "◎ Starting in 3s... 3"; sleep 1; echo -ne "\r◎ Starting in 3s... 2"; sleep 1; echo -ne "\r◎ Starting in 3s... 1"; sleep 1 + echo -ne "\r\033[K" + if [[ -t 1 ]]; then start_inline_spinner "Uninstalling apps..."; fi # Force quit running apps first (batch) if [[ ${#running_apps[@]} -gt 0 ]]; then - echo "" - log_info "Force quitting running applications..." - for app_name in "${running_apps[@]}"; do - echo " • Quitting $app_name..." - pkill -f "$app_name" 2>/dev/null || true - done - echo " • Waiting 3 seconds for apps to close..." - sleep 3 + pkill -f "${running_apps[0]}" 2>/dev/null || true + for app_name in "${running_apps[@]:1}"; do pkill -f "$app_name" 2>/dev/null || true; done + sleep 2 + if pgrep -f "${running_apps[0]}" >/dev/null 2>&1; then sleep 1; fi fi - # Perform uninstallations without individual confirmations + # Perform uninstallations (compact output) + if [[ -t 1 ]]; then stop_inline_spinner; fi echo "" - log_info "Starting batch uninstallation..." - local success_count=0 - local failed_count=0 - + local success_count=0 failed_count=0 + local -a failed_items=() for detail in "${app_details[@]}"; do IFS='|' read -r app_name app_path bundle_id total_kb encoded_files <<< "$detail" - - # Decode the related files list local related_files=$(echo "$encoded_files" | base64 -d) - - echo -e "${YELLOW}🗑️ Uninstalling: ${BLUE}$app_name${NC}" - - # Check if app is still running (even after force quit) - if pgrep -f "$app_name" >/dev/null 2>&1; then - echo -e " ${YELLOW}⚠️${NC} App is still running, attempting force kill..." - pkill -9 -f "$app_name" 2>/dev/null || true - sleep 2 - if pgrep -f "$app_name" >/dev/null 2>&1; then - echo -e " ${RED}✗${NC} Failed to remove $app_name" - echo -e " ${YELLOW}Reason: Application is still running and cannot be terminated${NC}" - ((failed_count++)) - continue - fi - fi - - # Check if app requires admin privileges to delete + local reason="" local needs_sudo=false - if [[ ! -w "$(dirname "$app_path")" ]] || [[ "$(stat -f%Su "$app_path" 2>/dev/null)" == "root" ]]; then - needs_sudo=true + [[ ! -w "$(dirname "$app_path")" || "$(stat -f%Su "$app_path" 2>/dev/null)" == "root" ]] && needs_sudo=true + if ! force_kill_app "$app_name"; then + reason="still running" fi - - # Remove the application with appropriate permissions - local removal_success=false - local error_msg="" - if [[ "$needs_sudo" == "true" ]]; then - if sudo rm -rf "$app_path" 2>/dev/null; then - removal_success=true - echo -e " ${GREEN}✓${NC} Removed application" + if [[ -z "$reason" ]]; then + if [[ "$needs_sudo" == true ]]; then + sudo rm -rf "$app_path" 2>/dev/null || reason="remove failed" else - error_msg="Failed to remove with sudo (check permissions or SIP protection)" - fi - else - if rm -rf "$app_path" 2>/dev/null; then - removal_success=true - echo -e " ${GREEN}✓${NC} Removed application" - else - error_msg="Failed to remove (check if app is running or protected)" + rm -rf "$app_path" 2>/dev/null || reason="remove failed" fi fi - - if [[ "$removal_success" == "true" ]]; then - - # Remove related files + if [[ -z "$reason" ]]; then local files_removed=0 while IFS= read -r file; do - if [[ -n "$file" && -e "$file" ]]; then - if rm -rf "$file" 2>/dev/null; then - ((files_removed++)) - fi - fi + [[ -n "$file" && -e "$file" ]] || continue + rm -rf "$file" 2>/dev/null && ((files_removed++)) || true done <<< "$related_files" - - if [[ $files_removed -gt 0 ]]; then - echo -e " ${GREEN}✓${NC} Cleaned $files_removed related files" - fi - ((total_size_freed += total_kb)) ((success_count++)) ((files_cleaned++)) ((total_items++)) - + printf " ${GREEN}OK${NC} %-20s%s\n" "$app_name" $([[ $files_removed -gt 0 ]] && echo "+$files_removed" ) else - echo -e " ${RED}✗${NC} Failed to remove $app_name" - if [[ -n "$error_msg" ]]; then - echo -e " ${YELLOW}Reason: $error_msg${NC}" - fi ((failed_count++)) + failed_items+=("$app_name:$reason") fi done - # Show final summary - echo "" - echo "====================================================================" - echo "🎉 UNINSTALLATION COMPLETE!" - - if [[ $success_count -gt 0 ]]; then - if [[ $total_size_freed -gt 1048576 ]]; then - local freed_display=$(echo "$total_size_freed" | awk '{printf "%.2fGB", $1/1024/1024}') - elif [[ $total_size_freed -gt 1024 ]]; then - local freed_display=$(echo "$total_size_freed" | awk '{printf "%.1fMB", $1/1024}') + # Summary + local freed_display="0B" + if [[ $total_size_freed -gt 0 ]]; then + local freed_kb=$total_size_freed + if [[ $freed_kb -ge 1048576 ]]; then + freed_display=$(echo "$freed_kb" | awk '{printf "%.2fGB", $1/1024/1024}') + elif [[ $freed_kb -ge 1024 ]]; then + freed_display=$(echo "$freed_kb" | awk '{printf "%.1fMB", $1/1024}') else - local freed_display="${total_size_freed}KB" + freed_display="${freed_kb}KB" + fi + fi + local bar="================================================================================" + echo "" + echo "$bar" + if [[ $failed_count -gt 0 ]]; then + echo -e "Removed: ${GREEN}$success_count${NC} | Failed: ${RED}$failed_count${NC} | Freed: ${GREEN}$freed_display${NC}" + if [[ $failed_count -eq 1 ]]; then + local first="${failed_items[0]}" + local name=${first%%:*} + local reason=${first#*:} + echo "${name} $(map_uninstall_reason "$reason")" + else + local joined="${failed_items[*]}"; echo "Failures: $joined" fi - echo "🗑️ Apps uninstalled: $success_count | Space freed: ${GREEN}${freed_display}${NC}" else - echo "🗑️ No applications were uninstalled" - fi - - if [[ $failed_count -gt 0 ]]; then - echo -e "${RED}⚠️ Failed to uninstall: $failed_count${NC}" - fi - - echo "====================================================================" - if [[ $failed_count -gt 0 ]]; then - log_warning "$failed_count applications failed to uninstall" + echo -e "Removed: ${GREEN}$success_count${NC} | Failed: ${RED}$failed_count${NC} | Freed: ${GREEN}$freed_display${NC}" fi + echo "$bar" # Clean up sudo keepalive if it was started if [[ -n "${sudo_keepalive_pid:-}" ]]; then @@ -262,4 +188,5 @@ batch_uninstall_applications() { fi ((total_size_cleaned += total_size_freed)) + unset failed_items } diff --git a/lib/common.sh b/lib/common.sh index a3e49e5..412352d 100755 --- a/lib/common.sh +++ b/lib/common.sh @@ -20,6 +20,13 @@ readonly RED="${ESC}[0;31m" readonly GRAY="${ESC}[0;90m" readonly NC="${ESC}[0m" +# Spinner character helpers (ASCII by default, overridable via env) +mo_spinner_chars() { + local chars="${MO_SPINNER_CHARS:-|/-\\}" + [[ -z "$chars" ]] && chars='|/-\\' + printf "%s" "$chars" +} + # Logging configuration readonly LOG_FILE="${HOME}/.config/mole/mole.log" readonly LOG_MAX_SIZE_DEFAULT=1048576 # 1MB @@ -51,7 +58,7 @@ log_success() { log_warning() { rotate_log - echo -e "${YELLOW}⚠️ $1${NC}" + echo -e "${YELLOW}$1${NC}" echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $1" >> "$LOG_FILE" 2>/dev/null || true } @@ -247,6 +254,48 @@ check_sudo() { return 0 } +# Check if Touch ID is configured for sudo +check_touchid_support() { + if [[ -f /etc/pam.d/sudo ]]; then + grep -q "pam_tid.so" /etc/pam.d/sudo 2>/dev/null + return $? + fi + return 1 +} + +# Request sudo access with Touch ID support +# Usage: request_sudo_access "prompt message" [optional: force_password] +request_sudo_access() { + local prompt_msg="${1:-Admin access required}" + local force_password="${2:-false}" + + # Check if already has sudo access + if sudo -n true 2>/dev/null; then + return 0 + fi + + # If Touch ID is supported and not forced to use password + if [[ "$force_password" != "true" ]] && check_touchid_support; then + echo -e "${BLUE}${prompt_msg}${NC} ${GRAY}(Touch ID or password)${NC}" + if sudo -v 2>/dev/null; then + return 0 + else + return 1 + fi + else + # Traditional password method + echo -e "${BLUE}${prompt_msg}${NC}" + echo -ne "${BLUE} Password> ${NC}" + read -s password + echo "" + if [[ -n "$password" ]] && echo "$password" | sudo -S true 2>/dev/null; then + return 0 + else + return 1 + fi + fi +} + request_sudo() { echo "This operation requires administrator privileges." echo -n "Please enter your password: " @@ -264,11 +313,11 @@ request_sudo() { update_via_homebrew() { local version="${1:-unknown}" - echo -e "${BLUE}◎${NC} Updating Homebrew..." + echo -e "${BLUE}|${NC} Updating Homebrew..." # Filter out common noise but show important info brew update 2>&1 | grep -Ev "^(==>|Already up-to-date)" || true - echo -e "${BLUE}◎${NC} Upgrading Mole..." + echo -e "${BLUE}|${NC} Upgrading Mole..." local upgrade_output upgrade_output=$(brew upgrade mole 2>&1) || true @@ -307,6 +356,388 @@ load_config() { # Initialize configuration on sourcing load_config +# ============================================================================ +# Spinner and Progress Indicators +# ============================================================================ + +# Global spinner process IDs +SPINNER_PID="" +INLINE_SPINNER_PID="" + +# Start a full-line spinner with message +start_spinner() { + local message="$1" + + if [[ ! -t 1 ]]; then + echo -n " ${BLUE}|${NC} $message" + return + fi + + echo -n " ${BLUE}|${NC} $message" + ( + local delay=0.5 + while true; do + printf "\r${MOLE_SPINNER_PREFIX:-}${BLUE}|${NC} $message. " + sleep $delay + printf "\r${MOLE_SPINNER_PREFIX:-}${BLUE}|${NC} $message.. " + sleep $delay + printf "\r${MOLE_SPINNER_PREFIX:-}${BLUE}|${NC} $message..." + sleep $delay + printf "\r${MOLE_SPINNER_PREFIX:-}${BLUE}|${NC} $message " + sleep $delay + done + ) & + SPINNER_PID=$! +} + +# Start an inline spinner (rotating character) +start_inline_spinner() { + stop_inline_spinner 2>/dev/null || true + local message="$1" + + if [[ -t 1 ]]; then + ( + local chars + chars="$(mo_spinner_chars)" + local i=0 + while true; do + local c="${chars:$((i % ${#chars})):1}" + printf "\r${MOLE_SPINNER_PREFIX:-}${BLUE}%s${NC} %s" "$c" "$message" + ((i++)) + sleep 0.12 + done + ) & + INLINE_SPINNER_PID=$! + else + echo -n " ${BLUE}|${NC} $message" + fi +} + +# Stop inline spinner +stop_inline_spinner() { + if [[ -n "$INLINE_SPINNER_PID" ]]; then + kill "$INLINE_SPINNER_PID" 2>/dev/null || true + wait "$INLINE_SPINNER_PID" 2>/dev/null || true + INLINE_SPINNER_PID="" + [[ -t 1 ]] && printf "\r" + fi +} + +# Stop spinner with optional result message +stop_spinner() { + local result_message="${1:-Done}" + + stop_inline_spinner + + if [[ -n "$SPINNER_PID" ]]; then + kill "$SPINNER_PID" 2>/dev/null || true + wait "$SPINNER_PID" 2>/dev/null || true + SPINNER_PID="" + fi + + if [[ -n "$result_message" ]]; then + if [[ -t 1 ]]; then + printf "\r${MOLE_SPINNER_PREFIX:-}${GREEN}✓${NC} %s\n" "$result_message" + else + echo " ✓ $result_message" + fi + fi +} + +# ============================================================================ +# User Interaction - Confirmation Dialogs +# ============================================================================ + + +# ============================================================================ +# Temporary File Management +# ============================================================================ + +# Global temp file tracking +declare -a MOLE_TEMP_FILES=() +declare -a MOLE_TEMP_DIRS=() + +# Create tracked temporary file +# Returns: temp file path +create_temp_file() { + local temp + temp=$(mktemp) || return 1 + MOLE_TEMP_FILES+=("$temp") + echo "$temp" +} + +# Create tracked temporary directory +# Returns: temp directory path +create_temp_dir() { + local temp + temp=$(mktemp -d) || return 1 + MOLE_TEMP_DIRS+=("$temp") + echo "$temp" +} + +# Create temp file with prefix (for analyze.sh compatibility) +# Args: $1 - prefix/suffix string +# Returns: temp file path +create_temp_file_named() { + local suffix="${1:-}" + local temp + temp=$(mktemp "/tmp/mole_${suffix}_XXXXXX") || return 1 + MOLE_TEMP_FILES+=("$temp") + echo "$temp" +} + +# Cleanup all tracked temp files +cleanup_temp_files() { + local file + if [[ ${#MOLE_TEMP_FILES[@]} -gt 0 ]]; then + for file in "${MOLE_TEMP_FILES[@]}"; do + [[ -f "$file" ]] && rm -f "$file" 2>/dev/null || true + done + fi + + if [[ ${#MOLE_TEMP_DIRS[@]} -gt 0 ]]; then + for file in "${MOLE_TEMP_DIRS[@]}"; do + [[ -d "$file" ]] && rm -rf "$file" 2>/dev/null || true + done + fi + + MOLE_TEMP_FILES=() + MOLE_TEMP_DIRS=() +} + +# Auto-cleanup on script exit (call this in main scripts) +register_temp_cleanup() { + trap cleanup_temp_files EXIT INT TERM +} + +# ============================================================================ +# Parallel Processing Framework +# ============================================================================ + +# Execute commands in parallel with job control +# Args: $1 - max parallel jobs +# $2 - worker function name +# $3+ - items to process +parallel_execute() { + local max_jobs="${1:-12}" + local worker_func="$2" + shift 2 + local -a items=("$@") + + if [[ ${#items[@]} -eq 0 ]]; then + return 0 + fi + + local -a pids=() + for item in "${items[@]}"; do + # Execute worker function in background + "$worker_func" "$item" & + pids+=($!) + + # Wait for a slot if we've hit max parallel jobs + if (( ${#pids[@]} >= max_jobs )); then + wait "${pids[0]}" 2>/dev/null || true + pids=("${pids[@]:1}") + fi + done + +# Wait for remaining background jobs + if (( ${#pids[@]} > 0 )); then + for pid in "${pids[@]}"; do + wait "$pid" 2>/dev/null || true + done + fi +} + +# ============================================================================ +# Lightweight spinner helper wrappers +# ============================================================================ +# Usage: with_spinner "Message" cmd arg... +# Set MOLE_SPINNER_PREFIX=" " for indented spinner (e.g., in clean context) +with_spinner() { + local msg="$1"; shift || true + if [[ -t 1 ]]; then + start_inline_spinner "$msg" + fi + "$@" >/dev/null 2>&1 || return $? + if [[ -t 1 ]]; then + stop_inline_spinner + fi +} + +# ============================================================================ +# Cache/tool cleanup abstraction +# ============================================================================ +# clean_tool_cache "Label" command... +clean_tool_cache() { + local label="$1"; shift || true + if [[ "$DRY_RUN" == "true" ]]; then + echo -e " ${YELLOW}→${NC} $label (would clean)" + return 0 + fi + MOLE_SPINNER_PREFIX=" " with_spinner "$label" "$@" + echo -e " ${GREEN}✓${NC} $label" +} + +# ============================================================================ +# Confirmation prompt abstraction (Enter=confirm ESC/q=cancel) +# confirm_prompt "Message" -> 0 yes, 1 no +confirm_prompt() { + local message="$1" + echo -n "$message (Enter=OK / ESC q=Cancel): " + IFS= read -r -s -n1 _key || _key="" + case "$_key" in + $'\e'|q|Q) echo ""; return 1 ;; + ""|$'\n'|$'\r'|y|Y) echo ""; return 0 ;; + *) echo ""; return 1 ;; + esac +} + + +# Get optimal parallel job count based on CPU cores + +# ========================================================================= +# Size helpers +# ========================================================================= +bytes_to_human_kb() { bytes_to_human "$(( ${1:-0} * 1024 ))"; } +print_space_stat() { + local freed_kb="$1"; shift || true + local current_free + current_free=$(get_free_space) + local human + human=$(bytes_to_human_kb "$freed_kb") + echo "Space freed: ${GREEN}${human}${NC} | Free space now: $current_free" +} + +# ========================================================================= +# mktemp unification wrappers (register access) +# ========================================================================= +register_temp_file() { MOLE_TEMP_FILES+=("$1"); } +register_temp_dir() { MOLE_TEMP_DIRS+=("$1"); } + +mktemp_file() { local f; f=$(mktemp) || return 1; register_temp_file "$f"; echo "$f"; } +mktemp_dir() { local d; d=$(mktemp -d) || return 1; register_temp_dir "$d"; echo "$d"; } + +# ========================================================================= +# Uninstall helper abstractions +# ========================================================================= +force_kill_app() { + # Args: app_name; tries graceful then force kill; returns 0 if stopped, 1 otherwise + local app="$1" + if pgrep -f "$app" >/dev/null 2>&1; then + pkill -f "$app" 2>/dev/null || true + sleep 1 + fi + if pgrep -f "$app" >/dev/null 2>&1; then + pkill -9 -f "$app" 2>/dev/null || true + sleep 1 + fi + pgrep -f "$app" >/dev/null 2>&1 && return 1 || return 0 +} + +map_uninstall_reason() { + # Args: reason_token + case "$1" in + still*running*) echo "was not removed; it remains running and resisted termination." ;; + remove*failed*) echo "was not removed due to a removal failure (permissions or protection)." ;; + permission*) echo "was not removed due to insufficient permissions." ;; + *) echo "was not removed; $1." ;; + esac +} + +batch_safe_clean() { + # Usage: batch_safe_clean "Label" path1 path2 ... + local label="$1"; shift || true + local -a paths=("$@") + if [[ ${#paths[@]} -eq 0 ]]; then return 0; fi + safe_clean "${paths[@]}" "$label" +} + +# Get optimal parallel job count based on CPU cores +get_optimal_parallel_jobs() { + local operation_type="${1:-default}" + local cpu_cores + cpu_cores=$(sysctl -n hw.ncpu 2>/dev/null || echo 4) + case "$operation_type" in + scan|io) + echo $((cpu_cores * 2)) + ;; + compute) + echo "$cpu_cores" + ;; + *) + echo $((cpu_cores + 2)) + ;; + esac +} + +# ============================================================================ +# Sudo Keepalive Management +# ============================================================================ + +# Start sudo keepalive process +# Returns: PID of the keepalive process +start_sudo_keepalive() { + ( + local retry_count=0 + while true; do + if ! sudo -n true 2>/dev/null; then + ((retry_count++)) + if [[ $retry_count -ge 3 ]]; then + exit 1 + fi + sleep 5 + continue + fi + retry_count=0 + sleep 30 + kill -0 "$$" 2>/dev/null || exit + done + ) 2>/dev/null & + echo $! +} + +# Stop sudo keepalive process +# Args: $1 - PID of the keepalive process +stop_sudo_keepalive() { + local pid="${1:-}" + if [[ -n "$pid" ]]; then + kill "$pid" 2>/dev/null || true + wait "$pid" 2>/dev/null || true + fi +} + +# ============================================================================ +# Section Management +# ============================================================================ + +# Section tracking variables +TRACK_SECTION=0 +SECTION_ACTIVITY=0 + +# Start a new section +start_section() { + TRACK_SECTION=1 + SECTION_ACTIVITY=0 + echo "" + echo -e "${PURPLE}▶ $1${NC}" +} + +# End a section (show "Nothing to tidy" if no activity) +end_section() { + if [[ $TRACK_SECTION -eq 1 && $SECTION_ACTIVITY -eq 0 ]]; then + echo -e " ${BLUE}○${NC} Nothing to tidy" + fi + TRACK_SECTION=0 +} + +# Mark activity in current section +note_activity() { + if [[ $TRACK_SECTION -eq 1 ]]; then + SECTION_ACTIVITY=1 + fi +} + # ============================================================================ # App Management Functions # ============================================================================ @@ -649,9 +1080,10 @@ readonly PRESERVED_BUNDLE_PATTERNS=("${SYSTEM_CRITICAL_BUNDLES[@]}" "${DATA_PROT should_preserve_bundle() { local bundle_id="$1" for pattern in "${PRESERVED_BUNDLE_PATTERNS[@]}"; do - if [[ "$bundle_id" == $pattern ]]; then - return 0 - fi + # Use case for safer glob matching + case "$bundle_id" in + $pattern) return 0 ;; + esac done return 1 } @@ -660,9 +1092,10 @@ should_preserve_bundle() { should_protect_from_uninstall() { local bundle_id="$1" for pattern in "${SYSTEM_CRITICAL_BUNDLES[@]}"; do - if [[ "$bundle_id" == $pattern ]]; then - return 0 - fi + # Use case for safer glob matching + case "$bundle_id" in + $pattern) return 0 ;; + esac done return 1 } @@ -672,9 +1105,10 @@ should_protect_data() { local bundle_id="$1" # Protect both system critical and data protected bundles during cleanup for pattern in "${SYSTEM_CRITICAL_BUNDLES[@]}" "${DATA_PROTECTED_BUNDLES[@]}"; do - if [[ "$bundle_id" == $pattern ]]; then - return 0 - fi + # Use case for safer glob matching + case "$bundle_id" in + $pattern) return 0 ;; + esac done return 1 } diff --git a/lib/paginated_menu.sh b/lib/paginated_menu.sh index 1c6fb36..9a8fda4 100755 --- a/lib/paginated_menu.sh +++ b/lib/paginated_menu.sh @@ -42,11 +42,25 @@ paginated_multi_select() { done fi + # Preserve original TTY settings so we can restore them reliably + local original_stty="" + if [[ -t 0 ]] && command -v stty >/dev/null 2>&1; then + original_stty=$(stty -g 2>/dev/null || echo "") + fi + + restore_terminal() { + show_cursor + if [[ -n "${original_stty-}" ]]; then + stty "${original_stty}" 2>/dev/null || stty sane 2>/dev/null || stty echo icanon 2>/dev/null || true + else + stty sane 2>/dev/null || stty echo icanon 2>/dev/null || true + fi + leave_alt_screen + } + # Cleanup function cleanup() { - show_cursor - stty echo icanon 2>/dev/null || true - leave_alt_screen + restore_terminal } # Interrupt handler @@ -220,15 +234,13 @@ EOF # Remove the trap to avoid cleanup on normal exit trap - EXIT INT TERM - + # Store result in global variable MOLE_SELECTION_RESULT="$final_result" - + # Manually cleanup terminal before returning - show_cursor - stty echo icanon 2>/dev/null || true - leave_alt_screen - + restore_terminal + return 0 ;; esac diff --git a/lib/whitelist_manager.sh b/lib/whitelist_manager.sh index 3665c88..d020b71 100755 --- a/lib/whitelist_manager.sh +++ b/lib/whitelist_manager.sh @@ -42,15 +42,15 @@ collect_files_to_be_cleaned() { local clean_sh="$SCRIPT_DIR/../bin/clean.sh" local -a items=() - echo -e "${BLUE}◎${NC} Scanning cache files..." + echo -e "${BLUE}|${NC} Scanning cache files..." echo "" # Run clean.sh in dry-run mode - local temp_output=$(mktemp) + local temp_output=$(create_temp_file) echo "" | bash "$clean_sh" --dry-run 2>&1 > "$temp_output" || true # Strip ANSI color codes for parsing - local temp_plain=$(mktemp) + local temp_plain=$(create_temp_file) sed $'s/\033\[[0-9;]*m//g' "$temp_output" > "$temp_plain" # Parse output: " → Description (size, dry)" @@ -83,7 +83,7 @@ collect_files_to_be_cleaned() { fi done < "$temp_plain" - rm -f "$temp_output" "$temp_plain" + # Temp files will be auto-cleaned by cleanup_temp_files # Return early if no items found if [[ ${#items[@]} -eq 0 ]]; then @@ -255,7 +255,7 @@ get_description_for_pattern() { manage_whitelist() { clear echo "" - echo -e "${PURPLE}📋 Whitelist Manager${NC}" + echo -e "${PURPLE}Whitelist Manager${NC}" echo "" # Load user-defined whitelist diff --git a/mole b/mole index a68fdf1..5142d12 100755 --- a/mole +++ b/mole @@ -2,9 +2,9 @@ # Mole - Main Entry Point # A comprehensive macOS maintenance tool # -# 🧹 Clean - Remove junk files and optimize system -# 🗑️ Uninstall - Remove applications completely -# 📊 Analyze - Interactive disk space explorer +# Clean - Remove junk files and optimize system +# Uninstall - Remove applications completely +# Analyze - Interactive disk space explorer # # Usage: # ./mole # Interactive main menu @@ -44,7 +44,7 @@ check_for_updates() { grep '^VERSION=' | head -1 | sed 's/VERSION="\(.*\)"/\1/') if [[ -n "$latest" && "$VERSION" != "$latest" && "$(printf '%s\n' "$VERSION" "$latest" | sort -V | head -1)" == "$VERSION" ]]; then - echo -e "${YELLOW}📢 New version ${GREEN}${latest}${YELLOW} available (current: ${VERSION})\n Run ${GREEN}mole update${YELLOW} to upgrade${NC}" > "$msg_cache" + echo -e "${YELLOW}New version ${GREEN}${latest}${YELLOW} available (current: ${VERSION})\n Run ${GREEN}mole update${YELLOW} to upgrade${NC}" > "$msg_cache" else echo -n > "$msg_cache" fi @@ -148,11 +148,11 @@ update_mole() { fi # Download and run installer with progress - echo -e "${BLUE}◎${NC} Downloading latest version..." + echo -e "${BLUE}|${NC} Downloading latest version..." local installer_url="https://raw.githubusercontent.com/tw93/mole/main/install.sh" local tmp_installer - tmp_installer="$(mktemp)" || { log_error "Update failed"; exit 1; } + tmp_installer="$(mktemp_file)" || { log_error "Update failed"; exit 1; } # Download installer with progress if command -v curl >/dev/null 2>&1; then @@ -181,7 +181,7 @@ update_mole() { local install_dir install_dir="$(cd "$(dirname "$mole_path")" && pwd)" - echo -e "${BLUE}◎${NC} Installing update..." + echo -e "${BLUE}|${NC} Installing update..." # Run installer with visible output (but capture for error handling) local install_output @@ -213,7 +213,7 @@ update_mole() { remove_mole() { clear echo "" - echo -e "${YELLOW}⚠️ Remove Mole${NC}" + echo -e "${YELLOW}Remove Mole${NC}" echo "" # Detect all installations @@ -286,16 +286,12 @@ remove_mole() { echo "" # Confirm removal - read -p "Are you sure you want to remove Mole? (y/N): " -n 1 -r - echo "" - - if [[ ! $REPLY =~ ^[Yy]$ ]]; then + read -p "Are you sure you want to remove Mole? (y/N): " -n 1 -r; echo ""; if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Cancelled." exit 0 fi echo "" - log_info "Removing Mole..." # Remove Homebrew installation if [[ "$is_homebrew" == "true" ]]; then @@ -344,7 +340,7 @@ remove_mole() { fi echo "" - echo -e "${GREEN}✨ Mole has been removed successfully${NC}" + echo -e "${GREEN}Mole has been removed successfully${NC}" echo "" echo "Thank you for using Mole!" @@ -356,16 +352,12 @@ show_main_menu() { local selected="${1:-1}" local full_draw="${2:-true}" - if [[ "$full_draw" == "true" ]]; then - clear_screen - echo "" - show_brand_banner - show_update_notification - echo "" - printf '\033[s' - else - printf '\033[u\033[0J' - fi + # Full redraw each time (prevents ghost menu items) + clear_screen + echo "" + show_brand_banner + show_update_notification + echo "" show_menu_option 1 "Clean Mac - Remove junk files and optimize" "$([[ $selected -eq 1 ]] && echo true || echo false)" show_menu_option 2 "Uninstall Apps - Remove applications completely" "$([[ $selected -eq 2 ]] && echo true || echo false)" @@ -410,6 +402,9 @@ interactive_main_menu() { first_draw=false fi + # Drain any pending input to prevent touchpad scroll issues + drain_pending_input + local key=$(read_key) [[ $? -ne 0 ]] && continue