1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 12:41:46 +00:00

Enhanced optimization for better use

This commit is contained in:
Tw93
2025-12-08 15:40:39 +08:00
parent 78e6743666
commit 9e7fc41445
4 changed files with 30 additions and 10 deletions

View File

@@ -222,14 +222,24 @@ scan_applications() {
app_size=$(bytes_to_human "$((app_size_kb * 1024))")
fi
# Get last used date - simplified for speed
# Note: mdls can be slow (40ms+ per app), so we use file mtime instead
# Get last used date
local last_used="Never"
local last_used_epoch=0
if [[ -d "$app_path" ]]; then
# Use file modification time (much faster than mdls)
last_used_epoch=$(get_file_mtime "$app_path")
# Try mdls first with short timeout (0.05s) for accuracy, fallback to mtime for speed
local metadata_date
metadata_date=$(run_with_timeout 0.05 mdls -name kMDItemLastUsedDate -raw "$app_path" 2> /dev/null || echo "")
if [[ "$metadata_date" != "(null)" && -n "$metadata_date" ]]; then
last_used_epoch=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "$metadata_date" "+%s" 2> /dev/null || echo "0")
fi
# Fallback if mdls failed or returned nothing
if [[ "$last_used_epoch" -eq 0 ]]; then
last_used_epoch=$(get_file_mtime "$app_path")
fi
if [[ $last_used_epoch -gt 0 ]]; then
local days_ago=$(((current_epoch - last_used_epoch) / 86400))
@@ -282,7 +292,6 @@ scan_applications() {
done
) &
spinner_pid=$!
disown 2> /dev/null || true
# Process apps in parallel batches
for app_data_tuple in "${app_data_tuples[@]}"; do

View File

@@ -84,6 +84,12 @@ get_uptime_days() {
echo "$uptime_days"
}
# JSON escape helper
json_escape() {
# Escape backslash, double quote, tab, and newline
echo -n "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g' | tr '\n' ' '
}
# Generate JSON output
generate_health_json() {
# System info
@@ -127,6 +133,11 @@ EOF
for item in "${items[@]}"; do
IFS='|' read -r action name desc safe <<< "$item"
# Escape strings
action=$(json_escape "$action")
name=$(json_escape "$name")
desc=$(json_escape "$desc")
[[ "$first" == "true" ]] && first=false || echo ","
cat << EOF

View File

@@ -420,7 +420,7 @@ find_app_files() {
[[ -f ~/Library/Preferences/"$bundle_id".plist ]] && files_to_clean+=("$HOME/Library/Preferences/$bundle_id.plist")
[[ -d ~/Library/Preferences/ByHost ]] && while IFS= read -r -d '' pref; do
files_to_clean+=("$pref")
done < <(find ~/Library/Preferences/ByHost \( -name \"$bundle_id*.plist\" \) -print0 2> /dev/null)
done < <(find ~/Library/Preferences/ByHost \( -name "$bundle_id*.plist" \) -print0 2> /dev/null)
# Logs
[[ -d ~/Library/Logs/"$app_name" ]] && files_to_clean+=("$HOME/Library/Logs/$app_name")
@@ -435,7 +435,7 @@ find_app_files() {
# Group Containers
[[ -d ~/Library/Group\ Containers ]] && while IFS= read -r -d '' container; do
files_to_clean+=("$container")
done < <(find ~/Library/Group\ Containers -type d \( -name \"*$bundle_id*\" \) -print0 2> /dev/null)
done < <(find ~/Library/Group\ Containers -type d \( -name "*$bundle_id*" \) -print0 2> /dev/null)
# WebKit data
[[ -d ~/Library/WebKit/"$bundle_id" ]] && files_to_clean+=("$HOME/Library/WebKit/$bundle_id")
@@ -514,7 +514,7 @@ find_app_system_files() {
# Privileged Helper Tools
[[ -d /Library/PrivilegedHelperTools ]] && while IFS= read -r -d '' helper; do
system_files+=("$helper")
done < <(find /Library/PrivilegedHelperTools \( -name \"$bundle_id*\" \) -print0 2> /dev/null)
done < <(find /Library/PrivilegedHelperTools \( -name "$bundle_id*" \) -print0 2> /dev/null)
# System Preferences
[[ -f /Library/Preferences/"$bundle_id".plist ]] && system_files+=("/Library/Preferences/$bundle_id.plist")
@@ -522,7 +522,7 @@ find_app_system_files() {
# Installation Receipts
[[ -d /private/var/db/receipts ]] && while IFS= read -r -d '' receipt; do
system_files+=("$receipt")
done < <(find /private/var/db/receipts \( -name \"*$bundle_id*\" \) -print0 2> /dev/null)
done < <(find /private/var/db/receipts \( -name "*$bundle_id*" \) -print0 2> /dev/null)
# System Logs
[[ -d /Library/Logs/"$app_name" ]] && system_files+=("/Library/Logs/$app_name")

View File

@@ -81,7 +81,7 @@ run_with_timeout() {
shift || true
# No timeout if duration is invalid or zero
if [[ ! "$duration" =~ ^[0-9]+$ ]] || [[ "$duration" -le 0 ]]; then
if [[ ! "$duration" =~ ^[0-9]+(\.[0-9]+)?$ ]] || [[ $(echo "$duration <= 0" | bc -l 2> /dev/null) -eq 1 ]]; then
"$@"
return $?
fi