mirror of
https://github.com/tw93/Mole.git
synced 2026-02-08 19:39:17 +00:00
🎨 Loading optimization and better use of links
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user