Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Apps whose size can't be measured read Unknown and sort first

Țugui Dragoș7db83370e380
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
index f41cc809..cec04f2c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
@@ -12,6 +12,12 @@ public enum AppOrder {
}
}
+ /// Sorts `apps` by size, largest first. An app whose size could not be measured comes first, since it is most
+ /// likely the biggest, and one still being measured comes last, after every size already known.
+ public static func sortedBySize(_ apps: [InstalledApp], sizes: [URL: Int64], unmeasured: Set<URL>) -> [InstalledApp] {
+ sorted(apps) { sizes[$0.id] ?? (unmeasured.contains($0.id) ? .max : -1) }
+ }
+
/// Whether `app` has not been opened in the last `months` months. An app with no record of being opened
/// counts from the day it was added, since it is exactly the kind of app this filter is meant to find. With
/// neither date, the app does not count as unused.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
index 5793f714..b4693333 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
@@ -16,6 +16,16 @@ struct AppOrderTests {
#expect(AppOrder.sorted(apps) { sizes[$0.name] ?? 0 }.map(\.name) == ["Big", "arc", "Bear", "Zed"])
}
+ /// An app whose size could not be measured is most likely the biggest, so it comes first, as an unknown size
+ /// does everywhere in Peel. One still being measured comes after every size already known.
+ @Test func putsAnAppThatCouldNotBeMeasuredFirst() {
+ let xcode = app("Xcode"), small = app("Small"), big = app("Big"), waiting = app("Waiting")
+ let sizes = [small.id: Int64(5_000_000), big.id: 900_000_000]
+
+ let order = AppOrder.sortedBySize([small, waiting, big, xcode], sizes: sizes, unmeasured: [xcode.id])
+ #expect(order.map(\.name) == ["Xcode", "Big", "Small", "Waiting"])
+ }
+
@Test func countsAnAppNobodyEverOpenedFromTheDayItWasAdded() {
let longAgo = now.addingTimeInterval(-400 * 86_400)
let lastWeek = now.addingTimeInterval(-7 * 86_400)
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 5dd6f1aa..6d5c5088 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -97,6 +97,11 @@ final class AppLibrary {
private(set) var sizes: [InstalledApp.ID: Int64] = [:] {
didSet { sizesRevision += 1 }
}
+ /// The apps whose bundle could not be measured, by time or by macOS, so their size is not known rather than
+ /// still to come. The list reads "Unknown" for them and sorts them first.
+ private(set) var unmeasured: Set<InstalledApp.ID> = [] {
+ didSet { sizesRevision += 1 }
+ }
/// Incremented when a size arrives. Only the size sort depends on it, so other sorts keep their cached list.
private(set) var sizesRevision = 0
@@ -222,6 +227,7 @@ final class AppLibrary {
let stale = Set(changed.map(\.id))
let keeps = { (id: InstalledApp.ID) in present.contains(id) && !stale.contains(id) }
sizes = sizes.filter { keeps($0.key) }
+ unmeasured = unmeasured.filter(keeps)
updateStatuses = updateStatuses.filter { keeps($0.key) }
lastUpdateChecks = lastUpdateChecks.filter { keeps($0.key) }
selection.formIntersection(present)
@@ -263,10 +269,18 @@ final class AppLibrary {
}
/// Measures the apps with no size yet. Each bundle needs a walk of every file, so sizes arrive after the list.
+ /// A bundle that could not be measured is asked once more after `remeasureDelay`: a walk that ran out of time
+ /// goes on, and its answer is kept for the next question.
func loadSizes() async {
- let missing = apps.filter { sizes[$0.id] == nil }
- guard !missing.isEmpty else { return }
+ await measure(apps.filter { sizes[$0.id] == nil })
+ guard !unmeasured.isEmpty, (try? await Task.sleep(for: Self.remeasureDelay)) != nil else { return }
+ await measure(apps.filter { unmeasured.contains($0.id) })
+ }
+
+ private static let remeasureDelay = Duration.seconds(30)
+ private func measure(_ missing: [InstalledApp]) async {
+ guard !missing.isEmpty else { return }
let measured = Dictionary(missing.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
await withTaskGroup(of: (InstalledApp.ID, Int64?).self) { group in
var pending = missing.makeIterator()
@@ -275,11 +289,15 @@ final class AppLibrary {
_ = group.addTaskUnlessCancelled { (app.id, await FileSize.reclaimableSize(of: app.url)) }
}
while let (id, size) = await group.next() {
- // A bundle that did not answer in time stays unknown (blank in the list) and is tried
- // again at the next revision, when its walk has usually finished. A bundle replaced while
- // it was walked is another build, so this size is dropped.
- if let size, let app = measured[id], apps.contains(where: { $0.id == id && Self.isTheSameBuild($0, app) }) {
- sizes[id] = size
+ // A walk given up because the list changed says nothing about the bundle, and a bundle replaced
+ // while it was walked is another build, so neither answer is kept.
+ if !Task.isCancelled, let app = measured[id], apps.contains(where: { $0.id == id && Self.isTheSameBuild($0, app) }) {
+ if let size {
+ sizes[id] = size
+ unmeasured.remove(id)
+ } else {
+ unmeasured.insert(id)
+ }
}
if !Task.isCancelled, let app = pending.next() {
_ = group.addTaskUnlessCancelled { (app.id, await FileSize.reclaimableSize(of: app.url)) }
@@ -415,7 +433,7 @@ final class AppLibrary {
case .name:
apps.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
case .size:
- AppOrder.sorted(apps) { sizes[$0.id] ?? 0 }
+ AppOrder.sortedBySize(apps, sizes: sizes, unmeasured: unmeasured)
case .lastOpened:
AppOrder.sorted(apps) { $0.lastUsedDate ?? .distantPast }
case .dateAdded:
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index e07edcd7..8032c8e9 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -94,6 +94,7 @@ struct AppList: View {
app: app,
updateStatus: library.updateStatuses[app.id],
size: library.sizes[app.id],
+ isUnmeasured: library.unmeasured.contains(app.id),
teamChange: library.teamChanges[app.bundleIdentifier],
sort: library.sort,
hasUpdate: library.hasUpdate(app),
@@ -235,6 +236,9 @@ private struct AppRow: View {
let app: InstalledApp
let updateStatus: UpdateStatus?
let size: Int64?
+ /// True when the bundle could not be measured, so the size reads "Unknown" rather than staying blank as it does
+ /// while it is measured.
+ let isUnmeasured: Bool
let teamChange: TeamRegistry.Change?
let sort: AppSort
let hasUpdate: Bool
@@ -271,7 +275,7 @@ private struct AppRow: View {
.accessibilityLabel(Text(.excludedApp))
}
// Always drawn, even before the sizes arrive, so nothing shifts when they do.
- Text(size?.byteCount ?? "")
+ Text(size?.byteCount ?? (isUnmeasured ? String(localized: "Unknown") : ""))
.font(.subheadline)
.monospacedDigit()
.foregroundStyle(.secondary)