1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-10 18:54:17 +00:00

Neat and uniform output

This commit is contained in:
Tw93
2025-10-09 14:24:00 +08:00
parent d34bf99d8f
commit 70c5db8c9a
9 changed files with 361 additions and 159 deletions

View File

@@ -1220,17 +1220,38 @@ display_file_types() {
return
fi
# Analyze common file types
local -A type_map=(
["Videos"]="kMDItemContentType == 'public.movie' || kMDItemContentType == 'public.video'"
["Images"]="kMDItemContentType == 'public.image'"
["Archives"]="kMDItemContentType == 'public.archive' || kMDItemContentType == 'public.zip-archive'"
["Documents"]="kMDItemContentType == 'com.adobe.pdf' || kMDItemContentType == 'public.text'"
["Audio"]="kMDItemContentType == 'public.audio'"
)
for type_name in "${!type_map[@]}"; do
local query="${type_map[$type_name]}"
# Analyze common file types (bash 3.2 compatible - no associative arrays)
local -a type_names=("Videos" "Images" "Archives" "Documents" "Audio")
local type_name
for type_name in "${type_names[@]}"; do
local query=""
local badge="$BADGE_FILE"
# Map type name to query and badge
case "$type_name" in
"Videos")
query="kMDItemContentType == 'public.movie' || kMDItemContentType == 'public.video'"
badge="$BADGE_MEDIA"
;;
"Images")
query="kMDItemContentType == 'public.image'"
badge="$BADGE_MEDIA"
;;
"Archives")
query="kMDItemContentType == 'public.archive' || kMDItemContentType == 'public.zip-archive'"
badge="$BADGE_BUNDLE"
;;
"Documents")
query="kMDItemContentType == 'com.adobe.pdf' || kMDItemContentType == 'public.text'"
badge="$BADGE_FILE"
;;
"Audio")
query="kMDItemContentType == 'public.audio'"
badge="🎵"
;;
esac
local files=$(mdfind -onlyin "$CURRENT_PATH" "$query" 2>/dev/null)
local count=$(echo "$files" | grep -c . || echo "0")
local total_size=0
@@ -1245,13 +1266,6 @@ display_file_types() {
if [[ $total_size -gt 0 ]]; then
local human_size=$(bytes_to_human "$total_size")
local badge="$BADGE_FILE"
case "$type_name" in
"Videos"|"Images") badge="$BADGE_MEDIA" ;;
"Archives") badge="$BADGE_BUNDLE" ;;
"Documents") badge="$BADGE_FILE" ;;
"Audio") badge="🎵" ;;
esac
printf " %s %-12s %8s (%d files)\n" "$badge" "$type_name:" "$human_size" "$count"
fi
fi

View File

@@ -1325,11 +1325,30 @@ perform_cleanup() {
local freed_gb=$(echo "$total_size_cleaned" | awk '{printf "%.2f", $1/1024/1024}')
if [[ "$DRY_RUN" == "true" ]]; then
echo "Potential reclaimable space: ${GREEN}${freed_gb}GB${NC} (no changes made) | Free space now: $(get_free_space)"
# Show file/category stats for dry run
if [[ $files_cleaned -gt 0 && $total_items -gt 0 ]]; then
printf "Files to clean: %s | Categories: %s\n" "$files_cleaned" "$total_items"
elif [[ $files_cleaned -gt 0 ]]; then
printf "Files to clean: %s\n" "$files_cleaned"
elif [[ $total_items -gt 0 ]]; then
printf "Categories: %s\n" "$total_items"
fi
echo ""
echo "To protect specific cache files from deletion, run: mole clean --whitelist"
else
echo "Space freed: ${GREEN}${freed_gb}GB${NC} | Free space now: $(get_free_space)"
fi
if [[ "$DRY_RUN" != "true" ]]; then
# Show file/category stats for actual cleanup
if [[ $files_cleaned -gt 0 && $total_items -gt 0 ]]; then
printf "Files cleaned: %s | Categories: %s\n" "$files_cleaned" "$total_items"
elif [[ $files_cleaned -gt 0 ]]; then
printf "Files cleaned: %s\n" "$files_cleaned"
elif [[ $total_items -gt 0 ]]; then
printf "Categories: %s\n" "$total_items"
fi
if [[ $(echo "$freed_gb" | awk '{print ($1 >= 1) ? 1 : 0}') -eq 1 ]]; then
local movies=$(echo "$freed_gb" | awk '{printf "%.0f", $1/4.5}')
if [[ $movies -gt 0 ]]; then
@@ -1344,14 +1363,6 @@ perform_cleanup() {
echo "No significant space was freed (system was already clean) | Free space: $(get_free_space)"
fi
fi
if [[ $files_cleaned -gt 0 && $total_items -gt 0 ]]; then
printf "Files cleaned: %s | Categories processed: %s\n" "$files_cleaned" "$total_items"
elif [[ $files_cleaned -gt 0 ]]; then
printf "Files cleaned: %s\n" "$files_cleaned"
elif [[ $total_items -gt 0 ]]; then
printf "Categories processed: %s\n" "$total_items"
fi
printf "====================================================================\n"
}

View File

@@ -122,7 +122,7 @@ scan_applications() {
fi
fi
local temp_file=$(mktemp_file)
local temp_file=$(create_temp_file)
echo "" >&2 # Add space before scanning output without breaking stdout return
# Pre-cache current epoch to avoid repeated calls
@@ -375,7 +375,8 @@ load_applications() {
uninstall_applications() {
local total_size_freed=0
log_header "Uninstalling selected applications"
echo ""
echo -e "${PURPLE}▶ Uninstalling selected applications${NC}"
if [[ ${#selected_apps[@]} -eq 0 ]]; then
log_warning "No applications selected for uninstallation"
@@ -389,14 +390,14 @@ uninstall_applications() {
# Check if app is running
if pgrep -f "$app_name" >/dev/null 2>&1; then
log_warning "$app_name is currently running"
echo -e "${YELLOW}$app_name is currently running${NC}"
read -p " Force quit $app_name? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
pkill -f "$app_name" 2>/dev/null || true
sleep 2
else
log_warning "Skipping $app_name (still running)"
echo -e " ${BLUE}${NC} Skipped $app_name"
continue
fi
fi
@@ -414,7 +415,7 @@ uninstall_applications() {
local total_kb=$((app_size_kb + related_size_kb + system_size_kb))
# Show what will be removed
echo -e " ${YELLOW}Files to be removed:${NC}"
echo -e "${BLUE}${NC} $app_name - Files to be removed:"
echo -e " ${GREEN}${NC} Application: $(echo "$app_path" | sed "s|$HOME|~|")"
# Show user-level files
@@ -425,7 +426,7 @@ uninstall_applications() {
# Show system-level files
if [[ -n "$system_files" ]]; then
while IFS= read -r file; do
[[ -n "$file" && -e "$file" ]] && echo -e " ${YELLOW}${NC} [System] $file"
[[ -n "$file" && -e "$file" ]] && echo -e " ${BLUE}${NC} System: $file"
done <<< "$system_files"
fi
@@ -448,7 +449,7 @@ uninstall_applications() {
if rm -rf "$app_path" 2>/dev/null; then
echo -e " ${GREEN}${NC} Removed application"
else
log_error "Failed to remove $app_path"
echo -e " ${RED}${NC} Failed to remove $app_path"
continue
fi
@@ -463,13 +464,13 @@ uninstall_applications() {
# Remove system-level files (requires sudo)
if [[ -n "$system_files" ]]; then
echo -e " ${YELLOW}System-level files require administrator privileges${NC}"
echo -e " ${BLUE}${NC} Admin access required for system files"
while IFS= read -r file; do
if [[ -n "$file" && -e "$file" ]]; then
if sudo rm -rf "$file" 2>/dev/null; then
echo -e " ${GREEN}${NC} Removed [System] $(basename "$file")"
echo -e " ${GREEN}${NC} Removed $(basename "$file")"
else
log_warning "Failed to remove system file: $file"
echo -e " ${YELLOW}${NC} Failed to remove: $file"
fi
fi
done <<< "$system_files"
@@ -479,15 +480,15 @@ uninstall_applications() {
((files_cleaned++))
((total_items++))
log_success "$app_name uninstalled successfully"
echo -e " ${GREEN}${NC} $app_name uninstalled successfully"
else
echo -e " ${BLUE}${NC} Skipped $app_name"
echo -e " ${BLUE}${NC} Skipped $app_name"
fi
done
# Show final summary
echo ""
log_header "Uninstallation Summary"
echo -e "${PURPLE}Uninstallation Summary${NC}"
if [[ $total_size_freed -gt 0 ]]; then
if [[ $total_size_freed -gt 1048576 ]]; then # > 1GB
@@ -498,10 +499,10 @@ uninstall_applications() {
local freed_display="${total_size_freed}KB"
fi
log_success "Freed $freed_display of disk space"
echo -e " ${GREEN}${NC} Freed $freed_display of disk space"
fi
echo "Applications uninstalled: $files_cleaned"
echo -e " ${GREEN}${NC} Applications uninstalled: $files_cleaned"
((total_size_cleaned += total_size_freed))
}
@@ -561,7 +562,7 @@ main() {
local extra=$((selection_count-3))
local list="${names[*]}"
[[ $extra -gt 0 ]] && list+=" +${extra}"
echo " ${selection_count} apps: ${list}"
echo -e "${BLUE}${NC} ${selection_count} apps: ${list}"
# Execute batch uninstallation (handles confirmation)
batch_uninstall_applications