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 on exit
|
||||||
cleanup() {
|
cleanup() {
|
||||||
show_cursor
|
show_cursor
|
||||||
|
# Cleanup temp files using glob pattern (analyze uses many temp files)
|
||||||
rm -f "$TEMP_PREFIX"* 2>/dev/null || true
|
rm -f "$TEMP_PREFIX"* 2>/dev/null || true
|
||||||
if [[ -n "$SCAN_PID" ]] && kill -0 "$SCAN_PID" 2>/dev/null; then
|
if [[ -n "$SCAN_PID" ]] && kill -0 "$SCAN_PID" 2>/dev/null; then
|
||||||
kill "$SCAN_PID" 2>/dev/null || true
|
kill "$SCAN_PID" 2>/dev/null || true
|
||||||
@@ -252,7 +253,8 @@ perform_scan() {
|
|||||||
SCAN_PID=$!
|
SCAN_PID=$!
|
||||||
|
|
||||||
# Show spinner with progress while scanning
|
# Show spinner with progress while scanning
|
||||||
local spinner_chars="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
|
local spinner_chars
|
||||||
|
spinner_chars="$(mo_spinner_chars)"
|
||||||
local i=0
|
local i=0
|
||||||
local elapsed=0
|
local elapsed=0
|
||||||
hide_cursor
|
hide_cursor
|
||||||
@@ -1245,15 +1247,11 @@ scan_directory_contents_fast() {
|
|||||||
local max_items="${3:-16}"
|
local max_items="${3:-16}"
|
||||||
local show_progress="${4:-true}"
|
local show_progress="${4:-true}"
|
||||||
|
|
||||||
# Auto-detect optimal parallel jobs - more aggressive
|
# Auto-detect optimal parallel jobs using common function
|
||||||
local num_jobs=12
|
local num_jobs=$(get_optimal_parallel_jobs "io")
|
||||||
if command -v sysctl &>/dev/null; then
|
# Cap at reasonable limits for I/O operations
|
||||||
local cpu_cores=$(sysctl -n hw.ncpu 2>/dev/null || echo 12)
|
[[ $num_jobs -gt 24 ]] && num_jobs=24
|
||||||
# Use more parallel jobs for better I/O utilization
|
[[ $num_jobs -lt 12 ]] && num_jobs=12
|
||||||
num_jobs=$((cpu_cores * 2))
|
|
||||||
[[ $num_jobs -gt 24 ]] && num_jobs=24
|
|
||||||
[[ $num_jobs -lt 12 ]] && num_jobs=12
|
|
||||||
fi
|
|
||||||
|
|
||||||
local temp_dirs="$output_file.dirs"
|
local temp_dirs="$output_file.dirs"
|
||||||
local temp_files="$output_file.files"
|
local temp_files="$output_file.files"
|
||||||
@@ -1262,7 +1260,7 @@ scan_directory_contents_fast() {
|
|||||||
if [[ "$show_progress" == "true" ]]; then
|
if [[ "$show_progress" == "true" ]]; then
|
||||||
printf "\033[?25l\033[H\033[J" >&2
|
printf "\033[?25l\033[H\033[J" >&2
|
||||||
echo "" >&2
|
echo "" >&2
|
||||||
printf " ${BLUE}📊 ⠋ Scanning...${NC}\r" >&2
|
printf " ${BLUE}📊 | Scanning...${NC}\r" >&2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Ultra-fast file scanning - batch stat for maximum speed
|
# Ultra-fast file scanning - batch stat for maximum speed
|
||||||
@@ -1311,14 +1309,27 @@ scan_directory_contents_fast() {
|
|||||||
|
|
||||||
# Show progress while waiting
|
# Show progress while waiting
|
||||||
if [[ "$show_progress" == "true" ]]; then
|
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 i=0
|
||||||
local max_wait=30 # Reduced to 30 seconds (fast fail)
|
local max_wait=30 # Reduced to 30 seconds (fast fail)
|
||||||
local elapsed=0
|
local elapsed=0
|
||||||
local tick=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
|
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++))
|
((i++))
|
||||||
sleep 0.1 # Faster animation (100ms per frame)
|
sleep 0.1 # Faster animation (100ms per frame)
|
||||||
((tick++))
|
((tick++))
|
||||||
|
|||||||
135
bin/clean.sh
135
bin/clean.sh
@@ -189,8 +189,8 @@ safe_clean() {
|
|||||||
|
|
||||||
# Show progress indicator for potentially slow operations
|
# Show progress indicator for potentially slow operations
|
||||||
if [[ ${#existing_paths[@]} -gt 3 ]]; then
|
if [[ ${#existing_paths[@]} -gt 3 ]]; then
|
||||||
[[ -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
|
||||||
local temp_dir=$(mktemp -d)
|
local temp_dir=$(create_temp_dir)
|
||||||
|
|
||||||
# Parallel processing (bash 3.2 compatible)
|
# Parallel processing (bash 3.2 compatible)
|
||||||
local -a pids=()
|
local -a pids=()
|
||||||
@@ -235,10 +235,10 @@ safe_clean() {
|
|||||||
((idx++))
|
((idx++))
|
||||||
done
|
done
|
||||||
|
|
||||||
rm -rf "$temp_dir"
|
# Temp dir will be auto-cleaned by cleanup_temp_files
|
||||||
else
|
else
|
||||||
# Show progress for small batches too (simpler jobs)
|
# 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
|
for path in "${existing_paths[@]}"; do
|
||||||
local size_bytes=$(du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0")
|
local size_bytes=$(du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0")
|
||||||
@@ -255,18 +255,12 @@ safe_clean() {
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clear progress indicator before showing result
|
# Clear progress / stop spinner before showing result
|
||||||
[[ -t 1 ]] && echo -ne "\r\033[K"
|
if [[ -t 1 ]]; then stop_inline_spinner; echo -ne "\r\033[K"; fi
|
||||||
|
|
||||||
if [[ $removed_any -eq 1 ]]; then
|
if [[ $removed_any -eq 1 ]]; then
|
||||||
local size_human
|
# Convert KB to bytes for bytes_to_human()
|
||||||
if [[ $total_size_bytes -gt $SIZE_1GB_KB ]]; then # > 1GB
|
local size_human=$(bytes_to_human "$((total_size_bytes * 1024))")
|
||||||
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
|
|
||||||
|
|
||||||
local label="$description"
|
local label="$description"
|
||||||
if [[ ${#targets[@]} -gt 1 ]]; then
|
if [[ ${#targets[@]} -gt 1 ]]; then
|
||||||
@@ -306,11 +300,7 @@ start_cleanup() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -t 0 ]]; then
|
if [[ -t 0 ]]; then
|
||||||
printf '\n'
|
echo -ne "${BLUE}System cleanup? Password to include (Enter skips)${NC}\n${BLUE}> ${NC}"; read -s password; echo ""
|
||||||
echo -e "${BLUE}System cleanup? Password to include (Enter skips)${NC}"
|
|
||||||
printf "${BLUE}> ${NC}"
|
|
||||||
read -s password
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
if [[ -n "$password" ]] && echo "$password" | sudo -S true 2>/dev/null; then
|
if [[ -n "$password" ]] && echo "$password" | sudo -S true 2>/dev/null; then
|
||||||
SYSTEM_CLEAN=true
|
SYSTEM_CLEAN=true
|
||||||
@@ -522,10 +512,7 @@ perform_cleanup() {
|
|||||||
# Node.js ecosystem
|
# Node.js ecosystem
|
||||||
if command -v npm >/dev/null 2>&1; then
|
if command -v npm >/dev/null 2>&1; then
|
||||||
if [[ "$DRY_RUN" != "true" ]]; then
|
if [[ "$DRY_RUN" != "true" ]]; then
|
||||||
[[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning npm cache...\r"
|
clean_tool_cache "npm cache" npm cache clean --force
|
||||||
npm cache clean --force >/dev/null 2>&1 || true
|
|
||||||
[[ -t 1 ]] && echo -ne "\r\033[K"
|
|
||||||
echo -e " ${GREEN}✓${NC} npm cache cleaned"
|
|
||||||
else
|
else
|
||||||
echo -e " ${YELLOW}→${NC} npm cache (would clean)"
|
echo -e " ${YELLOW}→${NC} npm cache (would clean)"
|
||||||
fi
|
fi
|
||||||
@@ -540,10 +527,7 @@ perform_cleanup() {
|
|||||||
# Python ecosystem
|
# Python ecosystem
|
||||||
if command -v pip3 >/dev/null 2>&1; then
|
if command -v pip3 >/dev/null 2>&1; then
|
||||||
if [[ "$DRY_RUN" != "true" ]]; then
|
if [[ "$DRY_RUN" != "true" ]]; then
|
||||||
[[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning pip cache...\r"
|
clean_tool_cache "pip cache" pip3 cache purge
|
||||||
pip3 cache purge >/dev/null 2>&1 || true
|
|
||||||
[[ -t 1 ]] && echo -ne "\r\033[K"
|
|
||||||
echo -e " ${GREEN}✓${NC} pip cache cleaned"
|
|
||||||
else
|
else
|
||||||
echo -e " ${YELLOW}→${NC} pip cache (would clean)"
|
echo -e " ${YELLOW}→${NC} pip cache (would clean)"
|
||||||
fi
|
fi
|
||||||
@@ -557,11 +541,7 @@ perform_cleanup() {
|
|||||||
# Go ecosystem
|
# Go ecosystem
|
||||||
if command -v go >/dev/null 2>&1; then
|
if command -v go >/dev/null 2>&1; then
|
||||||
if [[ "$DRY_RUN" != "true" ]]; then
|
if [[ "$DRY_RUN" != "true" ]]; then
|
||||||
[[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Go cache...\r"
|
clean_tool_cache "Go cache" bash -c 'go clean -modcache >/dev/null 2>&1 || true; go clean -cache >/dev/null 2>&1 || true'
|
||||||
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"
|
|
||||||
else
|
else
|
||||||
echo -e " ${YELLOW}→${NC} Go cache (would clean)"
|
echo -e " ${YELLOW}→${NC} Go cache (would clean)"
|
||||||
fi
|
fi
|
||||||
@@ -577,10 +557,7 @@ perform_cleanup() {
|
|||||||
# Docker (only clean build cache, preserve images and volumes)
|
# Docker (only clean build cache, preserve images and volumes)
|
||||||
if command -v docker >/dev/null 2>&1; then
|
if command -v docker >/dev/null 2>&1; then
|
||||||
if [[ "$DRY_RUN" != "true" ]]; then
|
if [[ "$DRY_RUN" != "true" ]]; then
|
||||||
[[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Docker build cache...\r"
|
clean_tool_cache "Docker build cache" docker builder prune -af
|
||||||
docker builder prune -af >/dev/null 2>&1 || true
|
|
||||||
[[ -t 1 ]] && echo -ne "\r\033[K"
|
|
||||||
echo -e " ${GREEN}✓${NC} Docker build cache cleaned"
|
|
||||||
else
|
else
|
||||||
echo -e " ${YELLOW}→${NC} Docker build cache (would clean)"
|
echo -e " ${YELLOW}→${NC} Docker build cache (would clean)"
|
||||||
fi
|
fi
|
||||||
@@ -602,10 +579,7 @@ perform_cleanup() {
|
|||||||
safe_clean /usr/local/var/homebrew/locks/* "Homebrew lock files (Intel)"
|
safe_clean /usr/local/var/homebrew/locks/* "Homebrew lock files (Intel)"
|
||||||
if command -v brew >/dev/null 2>&1; then
|
if command -v brew >/dev/null 2>&1; then
|
||||||
if [[ "$DRY_RUN" != "true" ]]; then
|
if [[ "$DRY_RUN" != "true" ]]; then
|
||||||
[[ -t 1 ]] && echo -ne " ${BLUE}◎${NC} Cleaning Homebrew...\r"
|
clean_tool_cache "Homebrew cleanup" brew cleanup
|
||||||
brew cleanup >/dev/null 2>&1 || true
|
|
||||||
[[ -t 1 ]] && echo -ne "\r\033[K"
|
|
||||||
echo -e " ${GREEN}✓${NC} Homebrew cache cleaned"
|
|
||||||
else
|
else
|
||||||
echo -e " ${YELLOW}→${NC} Homebrew (would cleanup)"
|
echo -e " ${YELLOW}→${NC} Homebrew (would cleanup)"
|
||||||
fi
|
fi
|
||||||
@@ -965,10 +939,10 @@ perform_cleanup() {
|
|||||||
local -r ORPHAN_AGE_THRESHOLD=$ORPHAN_AGE_DAYS
|
local -r ORPHAN_AGE_THRESHOLD=$ORPHAN_AGE_DAYS
|
||||||
|
|
||||||
# Build a comprehensive list of installed application bundle identifiers
|
# Build a comprehensive list of installed application bundle identifiers
|
||||||
echo -n " ${BLUE}◎${NC} Scanning installed applications..."
|
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning installed applications..." # ensure spinner function exists above
|
||||||
local installed_bundles=$(mktemp)
|
local installed_bundles=$(create_temp_file)
|
||||||
local running_bundles=$(mktemp)
|
local running_bundles=$(create_temp_file)
|
||||||
local launch_agents=$(mktemp)
|
local launch_agents=$(create_temp_file)
|
||||||
|
|
||||||
# Scan multiple possible application locations to avoid false positives
|
# Scan multiple possible application locations to avoid false positives
|
||||||
local -a search_paths=(
|
local -a search_paths=(
|
||||||
@@ -1035,7 +1009,8 @@ perform_cleanup() {
|
|||||||
mv "${installed_bundles}.final" "$installed_bundles"
|
mv "${installed_bundles}.final" "$installed_bundles"
|
||||||
|
|
||||||
local app_count=$(wc -l < "$installed_bundles" | tr -d ' ')
|
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
|
# Track statistics
|
||||||
local orphaned_count=0
|
local orphaned_count=0
|
||||||
@@ -1093,7 +1068,7 @@ perform_cleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Clean orphaned caches
|
# Clean orphaned caches
|
||||||
echo -n " ${BLUE}◎${NC} Scanning orphaned caches..."
|
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned caches..."
|
||||||
local cache_found=0
|
local cache_found=0
|
||||||
if ls ~/Library/Caches/com.* >/dev/null 2>&1; then
|
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
|
for cache_dir in ~/Library/Caches/com.* ~/Library/Caches/org.* ~/Library/Caches/net.* ~/Library/Caches/io.*; do
|
||||||
@@ -1109,10 +1084,11 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
echo " ${GREEN}✓${NC} Found $cache_found orphaned caches"
|
stop_inline_spinner
|
||||||
|
echo -e " ${GREEN}✓${NC} Found $cache_found orphaned caches"
|
||||||
|
|
||||||
# Clean orphaned logs
|
# Clean orphaned logs
|
||||||
echo -n " ${BLUE}◎${NC} Scanning orphaned logs..."
|
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned logs..."
|
||||||
local logs_found=0
|
local logs_found=0
|
||||||
if ls ~/Library/Logs/com.* >/dev/null 2>&1; then
|
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
|
for log_dir in ~/Library/Logs/com.* ~/Library/Logs/org.* ~/Library/Logs/net.* ~/Library/Logs/io.*; do
|
||||||
@@ -1128,10 +1104,11 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# 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
|
local states_found=0
|
||||||
if ls ~/Library/Saved\ Application\ State/*.savedState >/dev/null 2>&1; then
|
if ls ~/Library/Saved\ Application\ State/*.savedState >/dev/null 2>&1; then
|
||||||
for state_dir in ~/Library/Saved\ Application\ State/*.savedState; do
|
for state_dir in ~/Library/Saved\ Application\ State/*.savedState; do
|
||||||
@@ -1147,14 +1124,15 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# Clean orphaned containers
|
||||||
# NOTE: Container cleanup is DISABLED by default due to naming mismatch issues
|
# 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,
|
# 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.
|
# especially when system extensions are registered. This can cause false positives.
|
||||||
# To avoid deleting data from installed apps, we skip container cleanup.
|
# 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
|
local containers_found=0
|
||||||
if ls ~/Library/Containers/com.* >/dev/null 2>&1; then
|
if ls ~/Library/Containers/com.* >/dev/null 2>&1; then
|
||||||
# Count potential orphaned containers but don't delete them
|
# Count potential orphaned containers but don't delete them
|
||||||
@@ -1171,10 +1149,11 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# 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
|
local webkit_found=0
|
||||||
if ls ~/Library/WebKit/com.* >/dev/null 2>&1; then
|
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
|
for webkit_dir in ~/Library/WebKit/com.* ~/Library/WebKit/org.* ~/Library/WebKit/net.* ~/Library/WebKit/io.*; do
|
||||||
@@ -1190,10 +1169,11 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# 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
|
local http_found=0
|
||||||
if ls ~/Library/HTTPStorages/com.* >/dev/null 2>&1; then
|
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
|
for http_dir in ~/Library/HTTPStorages/com.* ~/Library/HTTPStorages/org.* ~/Library/HTTPStorages/net.* ~/Library/HTTPStorages/io.*; do
|
||||||
@@ -1209,10 +1189,11 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# Clean orphaned cookies
|
||||||
echo -n " ${BLUE}◎${NC} Scanning orphaned cookies..."
|
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning orphaned cookies..."
|
||||||
local cookies_found=0
|
local cookies_found=0
|
||||||
if ls ~/Library/Cookies/*.binarycookies >/dev/null 2>&1; then
|
if ls ~/Library/Cookies/*.binarycookies >/dev/null 2>&1; then
|
||||||
for cookie_file in ~/Library/Cookies/*.binarycookies; do
|
for cookie_file in ~/Library/Cookies/*.binarycookies; do
|
||||||
@@ -1228,7 +1209,8 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
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
|
# Calculate total
|
||||||
orphaned_count=$((cache_found + logs_found + states_found + containers_found + webkit_found + http_found + cookies_found))
|
orphaned_count=$((cache_found + logs_found + states_found + containers_found + webkit_found + http_found + cookies_found))
|
||||||
@@ -1309,40 +1291,15 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $files_cleaned -gt 0 && $total_items -gt 0 ]]; then
|
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
|
elif [[ $files_cleaned -gt 0 ]]; then
|
||||||
echo "📊 Files cleaned: $files_cleaned"
|
printf "📊 Files cleaned: %s\n" "$files_cleaned"
|
||||||
elif [[ $total_items -gt 0 ]]; then
|
elif [[ $total_items -gt 0 ]]; then
|
||||||
echo "🗂️ Categories processed: $total_items"
|
printf "🗂️ Categories processed: %s\n" "$total_items"
|
||||||
fi
|
fi
|
||||||
|
printf "====================================================================\n"
|
||||||
# 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 "===================================================================="
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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() {
|
main() {
|
||||||
# Parse args (only dry-run and help for minimal impact)
|
# Parse args (only dry-run and help for minimal impact)
|
||||||
@@ -1372,8 +1329,8 @@ main() {
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
hide_cursor
|
|
||||||
start_cleanup
|
start_cleanup
|
||||||
|
hide_cursor
|
||||||
perform_cleanup
|
perform_cleanup
|
||||||
show_cursor
|
show_cursor
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,8 +122,9 @@ scan_applications() {
|
|||||||
fi
|
fi
|
||||||
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
|
# Pre-cache current epoch to avoid repeated calls
|
||||||
local current_epoch=$(date "+%s")
|
local current_epoch=$(date "+%s")
|
||||||
|
|
||||||
@@ -320,13 +321,19 @@ scan_applications() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Sort by last used (oldest first) and cache the result
|
# 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"
|
rm -f "$temp_file"
|
||||||
|
|
||||||
# Update cache with app count metadata
|
# Update cache with app count metadata
|
||||||
cp "${temp_file}.sorted" "$cache_file" 2>/dev/null || true
|
cp "${temp_file}.sorted" "$cache_file" 2>/dev/null || true
|
||||||
echo "$current_app_count" > "$cache_meta" 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
|
# 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"
|
IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app"
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
log_info "Processing: $app_name"
|
|
||||||
|
|
||||||
# Check if app is running
|
# Check if app is running
|
||||||
if pgrep -f "$app_name" >/dev/null 2>&1; then
|
if pgrep -f "$app_name" >/dev/null 2>&1; then
|
||||||
@@ -475,7 +481,7 @@ uninstall_applications() {
|
|||||||
|
|
||||||
log_success "$app_name uninstalled successfully"
|
log_success "$app_name uninstalled successfully"
|
||||||
else
|
else
|
||||||
log_info "Skipped $app_name"
|
echo -e " ${BLUE}❂${NC} Skipped $app_name"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -518,6 +524,7 @@ main() {
|
|||||||
local apps_file=$(scan_applications)
|
local apps_file=$(scan_applications)
|
||||||
|
|
||||||
if [[ ! -f "$apps_file" ]]; then
|
if [[ ! -f "$apps_file" ]]; then
|
||||||
|
echo ""
|
||||||
log_error "Failed to scan applications"
|
log_error "Failed to scan applications"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -537,20 +544,26 @@ main() {
|
|||||||
# Restore cursor and show a concise summary before confirmation
|
# Restore cursor and show a concise summary before confirmation
|
||||||
show_cursor
|
show_cursor
|
||||||
clear
|
clear
|
||||||
printf '\n'
|
|
||||||
local selection_count=${#selected_apps[@]}
|
local selection_count=${#selected_apps[@]}
|
||||||
echo -e "${PURPLE}🗑️ Selected ${selection_count} app(s)${NC}"
|
if [[ $selection_count -eq 0 ]]; then
|
||||||
|
echo "No apps selected"; rm -f "$apps_file"; return 0
|
||||||
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}"
|
|
||||||
fi
|
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
|
batch_uninstall_applications
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
|
|||||||
35
install.sh
35
install.sh
@@ -10,6 +10,18 @@ BLUE='\033[0;34m'
|
|||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
NC='\033[0m'
|
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)
|
# Verbosity (0 = quiet, 1 = verbose)
|
||||||
VERBOSE=1
|
VERBOSE=1
|
||||||
@@ -81,14 +93,14 @@ resolve_source_dir() {
|
|||||||
|
|
||||||
# 3) Fallback: fetch repository to a temp directory (works for curl | bash)
|
# 3) Fallback: fetch repository to a temp directory (works for curl | bash)
|
||||||
local tmp
|
local tmp
|
||||||
tmp="$(mktemp -d)"
|
tmp="$(mktemp_dir)"
|
||||||
# Expand tmp now so trap doesn't depend on local scope
|
# Expand tmp now so trap doesn't depend on local scope
|
||||||
trap "rm -rf '$tmp'" EXIT
|
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
|
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
|
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"
|
tar -xzf "$tmp/mole.tar.gz" -C "$tmp"
|
||||||
# Extracted folder name: mole-main
|
# Extracted folder name: mole-main
|
||||||
if [[ -d "$tmp/mole-main" ]]; then
|
if [[ -d "$tmp/mole-main" ]]; then
|
||||||
@@ -97,14 +109,17 @@ resolve_source_dir() {
|
|||||||
fi
|
fi
|
||||||
fi
|
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 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
|
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"
|
SOURCE_DIR="$tmp/mole"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
stop_line_spinner
|
||||||
|
|
||||||
log_error "Failed to fetch source files. Ensure curl or git is available."
|
log_error "Failed to fetch source files. Ensure curl or git is available."
|
||||||
exit 1
|
exit 1
|
||||||
@@ -223,9 +238,7 @@ install_files() {
|
|||||||
|
|
||||||
# Copy main executable when destination differs
|
# Copy main executable when destination differs
|
||||||
if [[ -f "$SOURCE_DIR/mole" ]]; then
|
if [[ -f "$SOURCE_DIR/mole" ]]; then
|
||||||
if [[ "$source_dir_abs" == "$install_dir_abs" ]]; then
|
if [[ "$source_dir_abs" != "$install_dir_abs" ]]; then
|
||||||
log_info "Mole binary already present in $INSTALL_DIR"
|
|
||||||
else
|
|
||||||
if [[ "$INSTALL_DIR" == "/usr/local/bin" ]] && [[ ! -w "$INSTALL_DIR" ]]; then
|
if [[ "$INSTALL_DIR" == "/usr/local/bin" ]] && [[ ! -w "$INSTALL_DIR" ]]; then
|
||||||
sudo cp "$SOURCE_DIR/mole" "$INSTALL_DIR/mole"
|
sudo cp "$SOURCE_DIR/mole" "$INSTALL_DIR/mole"
|
||||||
sudo chmod +x "$INSTALL_DIR/mole"
|
sudo chmod +x "$INSTALL_DIR/mole"
|
||||||
@@ -428,9 +441,7 @@ uninstall_mole() {
|
|||||||
echo " $CONFIG_DIR"
|
echo " $CONFIG_DIR"
|
||||||
else
|
else
|
||||||
echo ""
|
echo ""
|
||||||
read -p "Remove configuration directory $CONFIG_DIR? (y/N): " -n 1 -r
|
read -p "Remove configuration directory $CONFIG_DIR? (y/N): " -n 1 -r; echo ""; if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||||
echo
|
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
||||||
rm -rf "$CONFIG_DIR"
|
rm -rf "$CONFIG_DIR"
|
||||||
log_success "Removed configuration directory"
|
log_success "Removed configuration directory"
|
||||||
else
|
else
|
||||||
@@ -476,10 +487,10 @@ perform_update() {
|
|||||||
update_via_homebrew "$VERSION"
|
update_via_homebrew "$VERSION"
|
||||||
else
|
else
|
||||||
# Fallback: inline implementation
|
# 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
|
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
|
local upgrade_output
|
||||||
upgrade_output=$(brew upgrade mole 2>&1) || true
|
upgrade_output=$(brew upgrade mole 2>&1) || true
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# Batch uninstall functionality with minimal confirmations
|
||||||
# Replaces the overly verbose individual confirmation approach
|
# Replaces the overly verbose individual confirmation approach
|
||||||
# Note: find_app_files() and calculate_total_size() functions now in lib/common.sh
|
# 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=()
|
local -a app_details=()
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
# Silent analysis without spinner output (avoid visual flicker)
|
||||||
# Show analyzing message with spinner
|
|
||||||
local spinner_chars="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
|
|
||||||
local spinner_idx=0
|
|
||||||
local analyzed=0
|
|
||||||
|
|
||||||
for selected_app in "${selected_apps[@]}"; do
|
for selected_app in "${selected_apps[@]}"; do
|
||||||
# Update spinner
|
[[ -z "$selected_app" ]] && continue
|
||||||
local spinner_char="${spinner_chars:$((spinner_idx % 10)):1}"
|
|
||||||
((analyzed++))
|
|
||||||
echo -ne "\r🗑️ ${spinner_char} Analyzing... $analyzed/${#selected_apps[@]}" >&2
|
|
||||||
((spinner_idx++))
|
|
||||||
IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app"
|
IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app"
|
||||||
|
|
||||||
# Check if app is running
|
# Check if app is running
|
||||||
@@ -57,36 +54,29 @@ batch_uninstall_applications() {
|
|||||||
app_details+=("$app_name|$app_path|$bundle_id|$total_kb|$encoded_files")
|
app_details+=("$app_name|$app_path|$bundle_id|$total_kb|$encoded_files")
|
||||||
done
|
done
|
||||||
|
|
||||||
# Clear spinner line
|
# Format size display (convert KB to bytes for bytes_to_human())
|
||||||
echo -ne "\r\033[K" >&2
|
local size_display=$(bytes_to_human "$((total_estimated_size * 1024))")
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# Request sudo access if needed (do this before confirmation)
|
# Request sudo access if needed (do this before confirmation)
|
||||||
if [[ ${#sudo_apps[@]} -gt 0 ]]; then
|
if [[ ${#sudo_apps[@]} -gt 0 ]]; then
|
||||||
echo ""
|
# Check if sudo is already cached
|
||||||
echo -e "${YELLOW}🔐 Admin privileges required for: ${BLUE}${sudo_apps[*]}${NC}"
|
if sudo -n true 2>/dev/null; then
|
||||||
echo -e "${BLUE}You will be prompted for your password before proceeding...${NC}"
|
echo "◎ Admin access confirmed for: ${sudo_apps[*]}"
|
||||||
if ! sudo -v; then
|
else
|
||||||
log_error "Administrator privileges required but not granted"
|
echo -n "◎ Admin required for: ${sudo_apps[*]}. "
|
||||||
return 1
|
if ! sudo -v; then
|
||||||
|
echo ""
|
||||||
|
log_error "Admin access denied"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo "✓ Granted"
|
||||||
fi
|
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) &
|
(while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null) &
|
||||||
local sudo_keepalive_pid=$!
|
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"
|
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
|
for signal in EXIT INT TERM; do
|
||||||
local existing_trap
|
local existing_trap; existing_trap=$(trap -p "$signal" | awk -F"'" '{print $2}')
|
||||||
existing_trap=$(trap -p "$signal" | awk -F"'" '{print $2}')
|
|
||||||
if [[ -n "$existing_trap" ]]; then
|
if [[ -n "$existing_trap" ]]; then
|
||||||
trap "$existing_trap; $_trap_cleanup_cmd" "$signal"
|
trap "$existing_trap; $_trap_cleanup_cmd" "$signal"
|
||||||
else
|
else
|
||||||
@@ -96,164 +86,98 @@ batch_uninstall_applications() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Show summary and get batch confirmation
|
# Show summary and get batch confirmation
|
||||||
printf '\n'
|
|
||||||
local app_total=${#selected_apps[@]}
|
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
|
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
|
fi
|
||||||
printf "%b" "${BLUE}Continue? Press Enter to proceed, or q/ESC to cancel:${NC} "
|
IFS= read -r -s -n1 key || key=""
|
||||||
local confirm_key=""
|
case "$key" in
|
||||||
IFS= read -r -s -n1 confirm_key || confirm_key=""
|
$'\e'|q|Q) echo ""; return 0 ;;
|
||||||
if [[ "$confirm_key" == $'\e' ]]; then
|
""|$'\n'|$'\r'|y|Y) echo "" ;;
|
||||||
while IFS= read -r -s -n1 -t 0 rest; do
|
*) echo ""; return 0 ;;
|
||||||
[[ -z "$rest" || "$rest" == $'\n' ]] && break
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
local cancel=false
|
|
||||||
case "$confirm_key" in
|
|
||||||
""|$'\n'|$'\r') ;;
|
|
||||||
$'\e'|"q"|"Q") cancel=true ;;
|
|
||||||
*) cancel=true ;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ "$cancel" == true ]]; then
|
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
|
||||||
log_info "Uninstallation cancelled"
|
echo -ne "\r\033[K"
|
||||||
# Clean up sudo keepalive if it was started
|
if [[ -t 1 ]]; then start_inline_spinner "Uninstalling apps..."; fi
|
||||||
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}"
|
|
||||||
|
|
||||||
# Force quit running apps first (batch)
|
# Force quit running apps first (batch)
|
||||||
if [[ ${#running_apps[@]} -gt 0 ]]; then
|
if [[ ${#running_apps[@]} -gt 0 ]]; then
|
||||||
echo ""
|
pkill -f "${running_apps[0]}" 2>/dev/null || true
|
||||||
log_info "Force quitting running applications..."
|
for app_name in "${running_apps[@]:1}"; do pkill -f "$app_name" 2>/dev/null || true; done
|
||||||
for app_name in "${running_apps[@]}"; do
|
sleep 2
|
||||||
echo " • Quitting $app_name..."
|
if pgrep -f "${running_apps[0]}" >/dev/null 2>&1; then sleep 1; fi
|
||||||
pkill -f "$app_name" 2>/dev/null || true
|
|
||||||
done
|
|
||||||
echo " • Waiting 3 seconds for apps to close..."
|
|
||||||
sleep 3
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Perform uninstallations without individual confirmations
|
# Perform uninstallations (compact output)
|
||||||
|
if [[ -t 1 ]]; then stop_inline_spinner; fi
|
||||||
echo ""
|
echo ""
|
||||||
log_info "Starting batch uninstallation..."
|
local success_count=0 failed_count=0
|
||||||
local success_count=0
|
local -a failed_items=()
|
||||||
local failed_count=0
|
|
||||||
|
|
||||||
for detail in "${app_details[@]}"; do
|
for detail in "${app_details[@]}"; do
|
||||||
IFS='|' read -r app_name app_path bundle_id total_kb encoded_files <<< "$detail"
|
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)
|
local related_files=$(echo "$encoded_files" | base64 -d)
|
||||||
|
local reason=""
|
||||||
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 needs_sudo=false
|
local needs_sudo=false
|
||||||
if [[ ! -w "$(dirname "$app_path")" ]] || [[ "$(stat -f%Su "$app_path" 2>/dev/null)" == "root" ]]; then
|
[[ ! -w "$(dirname "$app_path")" || "$(stat -f%Su "$app_path" 2>/dev/null)" == "root" ]] && needs_sudo=true
|
||||||
needs_sudo=true
|
if ! force_kill_app "$app_name"; then
|
||||||
|
reason="still running"
|
||||||
fi
|
fi
|
||||||
|
if [[ -z "$reason" ]]; then
|
||||||
# Remove the application with appropriate permissions
|
if [[ "$needs_sudo" == true ]]; then
|
||||||
local removal_success=false
|
sudo rm -rf "$app_path" 2>/dev/null || reason="remove failed"
|
||||||
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"
|
|
||||||
else
|
else
|
||||||
error_msg="Failed to remove with sudo (check permissions or SIP protection)"
|
rm -rf "$app_path" 2>/dev/null || reason="remove failed"
|
||||||
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)"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
if [[ -z "$reason" ]]; then
|
||||||
if [[ "$removal_success" == "true" ]]; then
|
|
||||||
|
|
||||||
# Remove related files
|
|
||||||
local files_removed=0
|
local files_removed=0
|
||||||
while IFS= read -r file; do
|
while IFS= read -r file; do
|
||||||
if [[ -n "$file" && -e "$file" ]]; then
|
[[ -n "$file" && -e "$file" ]] || continue
|
||||||
if rm -rf "$file" 2>/dev/null; then
|
rm -rf "$file" 2>/dev/null && ((files_removed++)) || true
|
||||||
((files_removed++))
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done <<< "$related_files"
|
done <<< "$related_files"
|
||||||
|
|
||||||
if [[ $files_removed -gt 0 ]]; then
|
|
||||||
echo -e " ${GREEN}✓${NC} Cleaned $files_removed related files"
|
|
||||||
fi
|
|
||||||
|
|
||||||
((total_size_freed += total_kb))
|
((total_size_freed += total_kb))
|
||||||
((success_count++))
|
((success_count++))
|
||||||
((files_cleaned++))
|
((files_cleaned++))
|
||||||
((total_items++))
|
((total_items++))
|
||||||
|
printf " ${GREEN}OK${NC} %-20s%s\n" "$app_name" $([[ $files_removed -gt 0 ]] && echo "+$files_removed" )
|
||||||
else
|
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_count++))
|
||||||
|
failed_items+=("$app_name:$reason")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# Show final summary
|
# Summary
|
||||||
echo ""
|
local freed_display="0B"
|
||||||
echo "===================================================================="
|
if [[ $total_size_freed -gt 0 ]]; then
|
||||||
echo "🎉 UNINSTALLATION COMPLETE!"
|
local freed_kb=$total_size_freed
|
||||||
|
if [[ $freed_kb -ge 1048576 ]]; then
|
||||||
if [[ $success_count -gt 0 ]]; then
|
freed_display=$(echo "$freed_kb" | awk '{printf "%.2fGB", $1/1024/1024}')
|
||||||
if [[ $total_size_freed -gt 1048576 ]]; then
|
elif [[ $freed_kb -ge 1024 ]]; then
|
||||||
local freed_display=$(echo "$total_size_freed" | awk '{printf "%.2fGB", $1/1024/1024}')
|
freed_display=$(echo "$freed_kb" | awk '{printf "%.1fMB", $1/1024}')
|
||||||
elif [[ $total_size_freed -gt 1024 ]]; then
|
|
||||||
local freed_display=$(echo "$total_size_freed" | awk '{printf "%.1fMB", $1/1024}')
|
|
||||||
else
|
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
|
fi
|
||||||
echo "🗑️ Apps uninstalled: $success_count | Space freed: ${GREEN}${freed_display}${NC}"
|
|
||||||
else
|
else
|
||||||
echo "🗑️ No applications were uninstalled"
|
echo -e "🚀 Removed: ${GREEN}$success_count${NC} | Failed: ${RED}$failed_count${NC} | Freed: ${GREEN}$freed_display${NC}"
|
||||||
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"
|
|
||||||
fi
|
fi
|
||||||
|
echo "$bar"
|
||||||
|
|
||||||
# Clean up sudo keepalive if it was started
|
# Clean up sudo keepalive if it was started
|
||||||
if [[ -n "${sudo_keepalive_pid:-}" ]]; then
|
if [[ -n "${sudo_keepalive_pid:-}" ]]; then
|
||||||
@@ -262,4 +186,5 @@ batch_uninstall_applications() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
((total_size_cleaned += total_size_freed))
|
((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 GRAY="${ESC}[0;90m"
|
||||||
readonly NC="${ESC}[0m"
|
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
|
# Logging configuration
|
||||||
readonly LOG_FILE="${HOME}/.config/mole/mole.log"
|
readonly LOG_FILE="${HOME}/.config/mole/mole.log"
|
||||||
readonly LOG_MAX_SIZE_DEFAULT=1048576 # 1MB
|
readonly LOG_MAX_SIZE_DEFAULT=1048576 # 1MB
|
||||||
@@ -264,11 +271,11 @@ request_sudo() {
|
|||||||
update_via_homebrew() {
|
update_via_homebrew() {
|
||||||
local version="${1:-unknown}"
|
local version="${1:-unknown}"
|
||||||
|
|
||||||
echo -e "${BLUE}◎${NC} Updating Homebrew..."
|
echo -e "${BLUE}|${NC} Updating Homebrew..."
|
||||||
# Filter out common noise but show important info
|
# Filter out common noise but show important info
|
||||||
brew update 2>&1 | grep -Ev "^(==>|Already up-to-date)" || true
|
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
|
local upgrade_output
|
||||||
upgrade_output=$(brew upgrade mole 2>&1) || true
|
upgrade_output=$(brew upgrade mole 2>&1) || true
|
||||||
|
|
||||||
@@ -307,6 +314,388 @@ load_config() {
|
|||||||
# Initialize configuration on sourcing
|
# Initialize configuration on sourcing
|
||||||
load_config
|
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
|
# App Management Functions
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|||||||
@@ -42,11 +42,25 @@ paginated_multi_select() {
|
|||||||
done
|
done
|
||||||
fi
|
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 function
|
||||||
cleanup() {
|
cleanup() {
|
||||||
show_cursor
|
restore_terminal
|
||||||
stty echo icanon 2>/dev/null || true
|
|
||||||
leave_alt_screen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Interrupt handler
|
# Interrupt handler
|
||||||
@@ -220,15 +234,13 @@ EOF
|
|||||||
|
|
||||||
# Remove the trap to avoid cleanup on normal exit
|
# Remove the trap to avoid cleanup on normal exit
|
||||||
trap - EXIT INT TERM
|
trap - EXIT INT TERM
|
||||||
|
|
||||||
# Store result in global variable
|
# Store result in global variable
|
||||||
MOLE_SELECTION_RESULT="$final_result"
|
MOLE_SELECTION_RESULT="$final_result"
|
||||||
|
|
||||||
# Manually cleanup terminal before returning
|
# Manually cleanup terminal before returning
|
||||||
show_cursor
|
restore_terminal
|
||||||
stty echo icanon 2>/dev/null || true
|
|
||||||
leave_alt_screen
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
@@ -42,15 +42,15 @@ collect_files_to_be_cleaned() {
|
|||||||
local clean_sh="$SCRIPT_DIR/../bin/clean.sh"
|
local clean_sh="$SCRIPT_DIR/../bin/clean.sh"
|
||||||
local -a items=()
|
local -a items=()
|
||||||
|
|
||||||
echo -e "${BLUE}◎${NC} Scanning cache files..."
|
echo -e "${BLUE}|${NC} Scanning cache files..."
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# Run clean.sh in dry-run mode
|
# 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
|
echo "" | bash "$clean_sh" --dry-run 2>&1 > "$temp_output" || true
|
||||||
|
|
||||||
# Strip ANSI color codes for parsing
|
# 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"
|
sed $'s/\033\[[0-9;]*m//g' "$temp_output" > "$temp_plain"
|
||||||
|
|
||||||
# Parse output: " → Description (size, dry)"
|
# Parse output: " → Description (size, dry)"
|
||||||
@@ -83,7 +83,7 @@ collect_files_to_be_cleaned() {
|
|||||||
fi
|
fi
|
||||||
done < "$temp_plain"
|
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
|
# Return early if no items found
|
||||||
if [[ ${#items[@]} -eq 0 ]]; then
|
if [[ ${#items[@]} -eq 0 ]]; then
|
||||||
|
|||||||
11
mole
11
mole
@@ -148,11 +148,11 @@ update_mole() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Download and run installer with progress
|
# 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 installer_url="https://raw.githubusercontent.com/tw93/mole/main/install.sh"
|
||||||
local tmp_installer
|
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
|
# Download installer with progress
|
||||||
if command -v curl >/dev/null 2>&1; then
|
if command -v curl >/dev/null 2>&1; then
|
||||||
@@ -181,7 +181,7 @@ update_mole() {
|
|||||||
local install_dir
|
local install_dir
|
||||||
install_dir="$(cd "$(dirname "$mole_path")" && pwd)"
|
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)
|
# Run installer with visible output (but capture for error handling)
|
||||||
local install_output
|
local install_output
|
||||||
@@ -286,10 +286,7 @@ remove_mole() {
|
|||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# Confirm removal
|
# Confirm removal
|
||||||
read -p "Are you sure you want to remove Mole? (y/N): " -n 1 -r
|
read -p "Are you sure you want to remove Mole? (y/N): " -n 1 -r; echo ""; if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||||
echo ""
|
|
||||||
|
|
||||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
||||||
echo "Cancelled."
|
echo "Cancelled."
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user