|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// createInsightEntries returns the list of hidden-space insight entries |
| 16 | +// to show in the overview screen alongside the standard directory entries. |
| 17 | +func createInsightEntries() []dirEntry { |
| 18 | + home := os.Getenv("HOME") |
| 19 | + if home == "" { |
| 20 | + return nil |
| 21 | + } |
| 22 | + |
| 23 | + var entries []dirEntry |
| 24 | + |
| 25 | + // iOS Backups — ~/Library/Application Support/MobileSync/Backup |
| 26 | + backupPath := filepath.Join(home, "Library", "Application Support", "MobileSync", "Backup") |
| 27 | + if info, err := os.Stat(backupPath); err == nil && info.IsDir() { |
| 28 | + entries = append(entries, dirEntry{ |
| 29 | + Name: "iOS Backups", |
| 30 | + Path: backupPath, |
| 31 | + IsDir: true, |
| 32 | + Size: -1, |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + // Old Downloads — ~/Downloads (files older than 90 days) |
| 37 | + downloadsPath := filepath.Join(home, "Downloads") |
| 38 | + if info, err := os.Stat(downloadsPath); err == nil && info.IsDir() { |
| 39 | + entries = append(entries, dirEntry{ |
| 40 | + Name: "Old Downloads (90d+)", |
| 41 | + Path: downloadsPath, |
| 42 | + IsDir: true, |
| 43 | + Size: -1, |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + // Cleanable paths — things mo clean can remove or the user can safely delete. |
| 48 | + cleanablePaths := []struct { |
| 49 | + name string |
| 50 | + path string |
| 51 | + }{ |
| 52 | + // Universal (everyone has these) |
| 53 | + {"System Caches", filepath.Join(home, "Library", "Caches")}, |
| 54 | + {"System Logs", filepath.Join(home, "Library", "Logs")}, |
| 55 | + {"Homebrew Cache", filepath.Join(home, "Library", "Caches", "Homebrew")}, |
| 56 | + |
| 57 | + // Developer-specific (only shown if path exists) |
| 58 | + {"Xcode DerivedData", filepath.Join(home, "Library", "Developer", "Xcode", "DerivedData")}, |
| 59 | + {"Xcode Simulators", filepath.Join(home, "Library", "Developer", "CoreSimulator", "Devices")}, |
| 60 | + {"Xcode Archives", filepath.Join(home, "Library", "Developer", "Xcode", "Archives")}, |
| 61 | + {"Spotify Cache", filepath.Join(home, "Library", "Application Support", "Spotify", "PersistentCache")}, |
| 62 | + {"JetBrains Cache", filepath.Join(home, "Library", "Caches", "JetBrains")}, |
| 63 | + {"Docker Data", filepath.Join(home, "Library", "Containers", "com.docker.docker", "Data")}, |
| 64 | + {"pip Cache", filepath.Join(home, "Library", "Caches", "pip")}, |
| 65 | + {"Gradle Cache", filepath.Join(home, ".gradle", "caches")}, |
| 66 | + {"CocoaPods Cache", filepath.Join(home, "Library", "Caches", "CocoaPods")}, |
| 67 | + } |
| 68 | + cacheBreakdownPaths := cleanablePaths |
| 69 | + for _, c := range cacheBreakdownPaths { |
| 70 | + if info, err := os.Stat(c.path); err == nil && info.IsDir() { |
| 71 | + entries = append(entries, dirEntry{ |
| 72 | + Name: c.name, |
| 73 | + Path: c.path, |
| 74 | + IsDir: true, |
| 75 | + Size: -1, |
| 76 | + }) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return entries |
| 81 | +} |
| 82 | + |
| 83 | +// measureInsightSize measures the size of an insight entry. |
| 84 | +// Some insights need special measurement (e.g., Old Downloads only counts old files). |
| 85 | +func measureInsightSize(entry dirEntry) (int64, error) { |
| 86 | + home := os.Getenv("HOME") |
| 87 | + |
| 88 | + // Old Downloads: only count files older than 90 days. |
| 89 | + if home != "" && entry.Path == filepath.Join(home, "Downloads") { |
| 90 | + return measureOldDownloads(entry.Path, 90) |
| 91 | + } |
| 92 | + |
| 93 | + // All others: standard directory size measurement. |
| 94 | + return measureOverviewSize(entry.Path) |
| 95 | +} |
| 96 | + |
| 97 | +// measureOldDownloads calculates total size of files in a directory |
| 98 | +// that haven't been modified in the given number of days. |
| 99 | +func measureOldDownloads(dir string, daysOld int) (int64, error) { |
| 100 | + cutoff := time.Now().AddDate(0, 0, -daysOld) |
| 101 | + var total int64 |
| 102 | + |
| 103 | + entries, err := os.ReadDir(dir) |
| 104 | + if err != nil { |
| 105 | + return 0, err |
| 106 | + } |
| 107 | + |
| 108 | + for _, entry := range entries { |
| 109 | + // Skip hidden files. |
| 110 | + if strings.HasPrefix(entry.Name(), ".") { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + info, err := entry.Info() |
| 115 | + if err != nil { |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + if info.ModTime().Before(cutoff) { |
| 120 | + if entry.IsDir() { |
| 121 | + // Use du for directories. |
| 122 | + if size, err := getDirSizeFast(filepath.Join(dir, entry.Name())); err == nil { |
| 123 | + total += size |
| 124 | + } |
| 125 | + } else { |
| 126 | + total += info.Size() |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return total, nil |
| 132 | +} |
| 133 | + |
| 134 | +// insightIcon returns an appropriate icon for an overview entry. |
| 135 | +func insightIcon(entry dirEntry) string { |
| 136 | + switch entry.Name { |
| 137 | + case "iOS Backups": |
| 138 | + return "📱" |
| 139 | + case "Old Downloads (90d+)": |
| 140 | + return "📥" |
| 141 | + case "System Caches", "Homebrew Cache", "pip Cache", "CocoaPods Cache", "Gradle Cache": |
| 142 | + return "💾" |
| 143 | + case "System Logs": |
| 144 | + return "📋" |
| 145 | + case "Xcode DerivedData", "Xcode Archives": |
| 146 | + return "🔨" |
| 147 | + case "Xcode Simulators": |
| 148 | + return "📲" |
| 149 | + case "Spotify Cache", "JetBrains Cache": |
| 150 | + return "💾" |
| 151 | + case "Docker Data": |
| 152 | + return "🐳" |
| 153 | + default: |
| 154 | + return "📁" |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// getDirSizeFast measures directory size using du. |
| 159 | +func getDirSizeFast(path string) (int64, error) { |
| 160 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 161 | + defer cancel() |
| 162 | + |
| 163 | + cmd := exec.CommandContext(ctx, "du", "-sk", path) |
| 164 | + output, err := cmd.Output() |
| 165 | + if err != nil { |
| 166 | + return 0, err |
| 167 | + } |
| 168 | + |
| 169 | + fields := strings.Fields(string(output)) |
| 170 | + if len(fields) == 0 { |
| 171 | + return 0, nil |
| 172 | + } |
| 173 | + |
| 174 | + kb, err := strconv.ParseInt(fields[0], 10, 64) |
| 175 | + if err != nil { |
| 176 | + return 0, err |
| 177 | + } |
| 178 | + |
| 179 | + return kb * 1024, nil |
| 180 | +} |
0 commit comments