1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-05 03:19:42 +00:00

optimize code structure and reduce duplication

This commit is contained in:
Tw93
2025-12-02 15:24:19 +08:00
parent 2ad69b3845
commit bb50a345b6
21 changed files with 421 additions and 409 deletions

View File

@@ -567,7 +567,7 @@ check_cache_size() {
for cache_path in "${cache_paths[@]}"; do
if [[ -d "$cache_path" ]]; then
local size_output
size_output=$(du -sk "$cache_path" 2> /dev/null | awk 'NR==1 {print $1}' | tr -d '[:space:]' || echo "")
size_output=$(get_path_size_kb "$cache_path")
[[ "$size_output" =~ ^[0-9]+$ ]] || size_output=0
cache_size_kb=$((cache_size_kb + size_output))
fi

View File

@@ -106,7 +106,7 @@ clean_media_players() {
has_offline_music=true
elif [[ -d "$spotify_cache" ]]; then
local cache_size_kb
cache_size_kb=$(du -sk "$spotify_cache" 2> /dev/null | awk '{print $1}' || echo "0")
cache_size_kb=$(get_path_size_kb "$spotify_cache")
# Large cache (>500MB) likely contains offline music
if [[ $cache_size_kb -ge 512000 ]]; then
has_offline_music=true

View File

@@ -232,7 +232,7 @@ clean_orphaned_app_data() {
if is_orphaned "$bundle_id" "$match"; then
# Use timeout to prevent du from hanging on large/problematic directories
local size_kb
size_kb=$(run_with_timeout 2 du -sk "$match" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(run_with_timeout 2 get_path_size_kb "$match")
if [[ -z "$size_kb" || "$size_kb" == "0" ]]; then
continue
fi

View File

@@ -75,7 +75,7 @@ clean_service_worker_cache() {
# Pattern matches: letters/numbers, hyphens, then dot, then TLD
# Example: "abc123_https_example.com_0" → "example.com"
local domain=$(basename "$cache_dir" | grep -oE '[a-zA-Z0-9][-a-zA-Z0-9]*\.[a-zA-Z]{2,}' | head -1 || echo "")
local size=$(du -sk "$cache_dir" 2> /dev/null | awk '{print $1}')
local size=$(get_path_size_kb "$cache_dir")
# Check if domain is protected
local is_protected=false

View File

@@ -40,7 +40,7 @@ clean_broken_preferences() {
# Validate plist using plutil
if ! plutil -lint "$plist_file" > /dev/null 2>&1; then
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
rm -f "$plist_file" 2> /dev/null || true
@@ -67,7 +67,7 @@ clean_broken_preferences() {
if ! plutil -lint "$plist_file" > /dev/null 2>&1; then
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
rm -f "$plist_file" 2> /dev/null || true
@@ -143,7 +143,7 @@ clean_broken_login_items() {
# Program doesn't exist - this is a broken login item
local size_kb
size_kb=$(du -sk "$plist_file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$plist_file")
if [[ "$DRY_RUN" != "true" ]]; then
# Unload first if loaded

View File

@@ -123,7 +123,7 @@ clean_time_machine_failed_backups() {
continue
fi
local size_kb=$(du -sk "$inprogress_file" 2> /dev/null | awk '{print $1}' || echo "0")
local size_kb=$(get_path_size_kb "$inprogress_file")
if [[ "$size_kb" -gt 0 ]]; then
local backup_name=$(basename "$inprogress_file")
@@ -175,7 +175,7 @@ clean_time_machine_failed_backups() {
continue
fi
local size_kb=$(du -sk "$inprogress_file" 2> /dev/null | awk '{print $1}' || echo "0")
local size_kb=$(get_path_size_kb "$inprogress_file")
if [[ "$size_kb" -gt 0 ]]; then
local backup_name=$(basename "$inprogress_file")

View File

@@ -273,7 +273,7 @@ clean_application_support_logs() {
check_ios_device_backups() {
local backup_dir="$HOME/Library/Application Support/MobileSync/Backup"
if [[ -d "$backup_dir" ]] && find "$backup_dir" -mindepth 1 -maxdepth 1 | read -r _; then
local backup_kb=$(du -sk "$backup_dir" 2> /dev/null | awk '{print $1}')
local backup_kb=$(get_path_size_kb "$backup_dir")
if [[ -n "${backup_kb:-}" && "$backup_kb" -gt 102400 ]]; then
local backup_human=$(du -sh "$backup_dir" 2> /dev/null | awk '{print $1}')
note_activity

View File

@@ -78,6 +78,13 @@ is_sip_enabled() {
fi
}
# Check if running in interactive terminal
# Returns: 0 if interactive (stdout is a terminal), 1 otherwise
# Usage: if is_interactive; then echo "Interactive mode"; fi
is_interactive() {
[[ -t 1 ]]
}
# Get spinner characters (overridable via MO_SPINNER_CHARS)
mo_spinner_chars() {
local chars="${MO_SPINNER_CHARS:-|/-\\}"
@@ -1131,6 +1138,15 @@ clean_tool_cache() {
# ============================================================================
# Size helpers
# ============================================================================
# Get path size in KB using du
# Args: $1 - path to measure
# Returns: size in KB, or 0 if path doesn't exist or error occurs
get_path_size_kb() {
local path="$1"
du -sk "$path" 2> /dev/null | awk '{print $1}' || echo "0"
}
bytes_to_human_kb() { bytes_to_human "$((${1:-0} * 1024))"; }
# ============================================================================
@@ -2064,7 +2080,7 @@ calculate_total_size() {
while IFS= read -r file; do
if [[ -n "$file" && -e "$file" ]]; then
local size_kb
size_kb=$(du -sk "$file" 2> /dev/null | awk '{print $1}' || echo "0")
size_kb=$(get_path_size_kb "$file")
((total_kb += size_kb))
fi
done <<< "$files"

View File

@@ -3,15 +3,6 @@
set -euo pipefail
_opt_get_dir_size_kb() {
local path="$1"
[[ -e "$path" ]] || {
echo 0
return
}
du -sk "$path" 2> /dev/null | awk '{print $1}' || echo 0
}
# System maintenance: rebuild databases and flush caches
opt_system_maintenance() {
echo -e "${BLUE}${ICON_ARROW}${NC} Rebuilding LaunchServices database..."
@@ -204,7 +195,7 @@ opt_mail_downloads() {
local total_kb=0
for target_path in "${mail_dirs[@]}"; do
total_kb=$((total_kb + $(_opt_get_dir_size_kb "$target_path")))
total_kb=$((total_kb + $(get_path_size_kb "$target_path")))
done
if [[ $total_kb -lt $MOLE_MAIL_DOWNLOADS_MIN_KB ]]; then
@@ -498,12 +489,6 @@ get_uptime_days() {
}
# Get directory size in KB
dir_size_kb() {
local path="$1"
[[ ! -e "$path" ]] && echo "0" && return
du -sk "$path" 2> /dev/null | awk '{print $1}' || echo "0"
}
# Format size from KB
format_size_kb() {
local kb="$1"
@@ -525,7 +510,7 @@ format_size_kb() {
# Check cache size
check_cache_refresh() {
local cache_dir="$HOME/Library/Caches"
local size_kb=$(dir_size_kb "$cache_dir")
local size_kb=$(get_path_size_kb "$cache_dir")
local desc="Refresh Finder previews, Quick Look, and Safari caches"
if [[ $size_kb -gt 0 ]]; then
@@ -545,7 +530,7 @@ check_mail_downloads() {
local total_kb=0
for dir in "${dirs[@]}"; do
total_kb=$((total_kb + $(dir_size_kb "$dir")))
total_kb=$((total_kb + $(get_path_size_kb "$dir")))
done
if [[ $total_kb -gt 0 ]]; then
@@ -557,7 +542,7 @@ check_mail_downloads() {
# Check saved state
check_saved_state() {
local state_dir="$HOME/Library/Saved Application State"
local size_kb=$(dir_size_kb "$state_dir")
local size_kb=$(get_path_size_kb "$state_dir")
if [[ $size_kb -gt 0 ]]; then
local size_str=$(format_size_kb "$size_kb")
@@ -605,7 +590,7 @@ check_developer_cleanup() {
local total_kb=0
for dir in "${dirs[@]}"; do
total_kb=$((total_kb + $(dir_size_kb "$dir")))
total_kb=$((total_kb + $(get_path_size_kb "$dir")))
done
if [[ $total_kb -gt 0 ]]; then

View File

@@ -129,7 +129,7 @@ batch_uninstall_applications() {
fi
# Calculate size for summary (including system files)
local app_size_kb=$(du -sk "$app_path" 2> /dev/null | awk '{print $1}' || echo "0")
local app_size_kb=$(get_path_size_kb "$app_path")
local related_files=$(find_app_files "$bundle_id" "$app_name")
local related_size_kb=$(calculate_total_size "$related_files")
local system_files=$(find_app_system_files "$bundle_id" "$app_name")