1
0
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:
Tw93
2025-10-08 18:01:46 +08:00
parent 79b1ea4091
commit 1657ff584b
9 changed files with 623 additions and 308 deletions

View File

@@ -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
}