mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 13:16:47 +00:00
🎨 Loading optimization and better use of links
This commit is contained in:
@@ -40,6 +40,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
|
||||
@@ -252,7 +253,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
|
||||
@@ -1245,15 +1247,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"
|
||||
@@ -1262,7 +1260,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
|
||||
@@ -1311,14 +1309,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<chars_len; idx++)); do
|
||||
spinner+=("${spinner_chars:idx:1}")
|
||||
done
|
||||
fi
|
||||
[[ ${#spinner[@]} -eq 0 ]] && spinner=('|' '/' '-' '\\')
|
||||
local i=0
|
||||
local max_wait=30 # Reduced to 30 seconds (fast fail)
|
||||
local elapsed=0
|
||||
local tick=0
|
||||
local spin_len=${#spinner[@]}
|
||||
(( spin_len == 0 )) && spinner=('|' '/' '-' '\\') && spin_len=${#spinner[@]}
|
||||
|
||||
while ( kill -0 "$dir_pid" 2>/dev/null || kill -0 "$file_pid" 2>/dev/null ); do
|
||||
printf "\r ${BLUE}📊 ${spinner[$((i % 10))]} Scanning... (%ds)${NC}" "$elapsed" >&2
|
||||
printf "\r ${BLUE}📊 ${spinner[$((i % spin_len))]} Scanning... (%ds)${NC}" "$elapsed" >&2
|
||||
((i++))
|
||||
sleep 0.1 # Faster animation (100ms per frame)
|
||||
((tick++))
|
||||
|
||||
135
bin/clean.sh
135
bin/clean.sh
@@ -189,8 +189,8 @@ 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=()
|
||||
@@ -235,10 +235,10 @@ safe_clean() {
|
||||
((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")
|
||||
@@ -255,18 +255,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 $SIZE_1GB_KB ]]; then # > 1GB
|
||||
size_human=$(echo "$total_size_bytes" | awk '{printf "%.1fGB", $1/1024/1024}')
|
||||
elif [[ $total_size_bytes -gt $SIZE_1MB_KB ]]; 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
|
||||
@@ -306,11 +300,7 @@ start_cleanup() {
|
||||
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? Password to include (Enter skips)${NC}\n${BLUE}> ${NC}"; read -s password; echo ""
|
||||
|
||||
if [[ -n "$password" ]] && echo "$password" | sudo -S true 2>/dev/null; then
|
||||
SYSTEM_CLEAN=true
|
||||
@@ -522,10 +512,7 @@ perform_cleanup() {
|
||||
# Node.js ecosystem
|
||||
if command -v npm >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" != "true" ]]; 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"
|
||||
clean_tool_cache "npm cache" npm cache clean --force
|
||||
else
|
||||
echo -e " ${YELLOW}→${NC} npm cache (would clean)"
|
||||
fi
|
||||
@@ -540,10 +527,7 @@ perform_cleanup() {
|
||||
# Python ecosystem
|
||||
if command -v pip3 >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" != "true" ]]; 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"
|
||||
clean_tool_cache "pip cache" pip3 cache purge
|
||||
else
|
||||
echo -e " ${YELLOW}→${NC} pip cache (would clean)"
|
||||
fi
|
||||
@@ -557,11 +541,7 @@ perform_cleanup() {
|
||||
# Go ecosystem
|
||||
if command -v go >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" != "true" ]]; 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"
|
||||
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
|
||||
@@ -577,10 +557,7 @@ perform_cleanup() {
|
||||
# Docker (only clean build cache, preserve images and volumes)
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" != "true" ]]; 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"
|
||||
clean_tool_cache "Docker build cache" docker builder prune -af
|
||||
else
|
||||
echo -e " ${YELLOW}→${NC} Docker build cache (would clean)"
|
||||
fi
|
||||
@@ -602,10 +579,7 @@ perform_cleanup() {
|
||||
safe_clean /usr/local/var/homebrew/locks/* "Homebrew lock files (Intel)"
|
||||
if command -v brew >/dev/null 2>&1; then
|
||||
if [[ "$DRY_RUN" != "true" ]]; 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"
|
||||
clean_tool_cache "Homebrew cleanup" brew cleanup
|
||||
else
|
||||
echo -e " ${YELLOW}→${NC} Homebrew (would cleanup)"
|
||||
fi
|
||||
@@ -965,10 +939,10 @@ perform_cleanup() {
|
||||
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=(
|
||||
@@ -1035,7 +1009,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
|
||||
@@ -1093,7 +1068,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
|
||||
@@ -1109,10 +1084,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
|
||||
@@ -1128,10 +1104,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
|
||||
@@ -1147,14 +1124,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
|
||||
@@ -1171,10 +1149,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
|
||||
@@ -1190,10 +1169,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
|
||||
@@ -1209,10 +1189,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
|
||||
@@ -1228,7 +1209,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))
|
||||
@@ -1309,40 +1291,15 @@ perform_cleanup() {
|
||||
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
|
||||
|
||||
# Show context-specific tips
|
||||
echo ""
|
||||
if [[ "$DRY_RUN" == "true" ]]; then
|
||||
echo -e "${BLUE}💡 Tip: Use 'mo clean --whitelist' to protect important caches${NC}"
|
||||
elif [[ "$SYSTEM_CLEAN" != "true" ]]; then
|
||||
echo -e "${BLUE}💡 Tip: Run with admin password for deeper system cleanup${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)
|
||||
@@ -1372,8 +1329,8 @@ main() {
|
||||
esac
|
||||
done
|
||||
|
||||
hide_cursor
|
||||
start_cleanup
|
||||
hide_cursor
|
||||
perform_cleanup
|
||||
show_cursor
|
||||
}
|
||||
|
||||
@@ -122,8 +122,9 @@ scan_applications() {
|
||||
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")
|
||||
|
||||
@@ -320,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
|
||||
@@ -379,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
|
||||
@@ -475,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
|
||||
|
||||
@@ -518,6 +524,7 @@ main() {
|
||||
local apps_file=$(scan_applications)
|
||||
|
||||
if [[ ! -f "$apps_file" ]]; then
|
||||
echo ""
|
||||
log_error "Failed to scan applications"
|
||||
return 1
|
||||
fi
|
||||
@@ -537,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
|
||||
|
||||
35
install.sh
35
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
|
||||
|
||||
|
||||
@@ -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,29 @@ 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 -n "◎ Admin required for: ${sudo_apps[*]}. "
|
||||
if ! sudo -v; then
|
||||
echo ""
|
||||
log_error "Admin access denied"
|
||||
return 1
|
||||
fi
|
||||
echo "✓ 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 +86,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 +186,5 @@ batch_uninstall_applications() {
|
||||
fi
|
||||
|
||||
((total_size_cleaned += total_size_freed))
|
||||
unset failed_items
|
||||
}
|
||||
|
||||
393
lib/common.sh
393
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
|
||||
@@ -264,11 +271,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 +314,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
|
||||
# ============================================================================
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
11
mole
11
mole
@@ -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
|
||||
@@ -286,10 +286,7 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user