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.

Read Space's disk band as often as Home reads its own

Țugui Dragoșb802f3a2825d
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/DiskSpace.swift b/Packages/PeelCore/Sources/PeelCore/Space/DiskSpace.swift
new file mode 100644
index 00000000..32f4ee77
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Space/DiskSpace.swift
@@ -0,0 +1,23 @@
+public import Foundation
+
+/// The disk the home folder is on, as Space's band shows it: read on its own, as often as Home reads it, since a scan
+/// of the areas can be hours old.
+public struct DiskSpace: Sendable, Hashable {
+ public let storage: DeviceInfo.Storage
+ /// Space macOS can reclaim by itself, mostly local snapshots and caches. Nil when the volume did not say.
+ public let purgeable: Int64?
+
+ @concurrent
+ public static func read(of home: URL = .homeDirectory) async -> DiskSpace {
+ DiskSpace(storage: DeviceInfo.storage(of: home), purgeable: purgeable(of: home))
+ }
+
+ static func purgeable(of url: URL) -> Int64? {
+ let values = try? url.resourceValues(forKeys: [
+ .volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityKey,
+ ])
+ guard let important = values?.volumeAvailableCapacityForImportantUsage,
+ let free = values?.volumeAvailableCapacity else { return nil }
+ return max(0, important - Int64(free))
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
index aed0f669..fe07f365 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceReport.swift
@@ -52,9 +52,6 @@ public struct SpaceItem: Sendable, Hashable, Identifiable {
public struct SpaceReport: Sendable {
public let items: [SpaceItem]
- public let storage: DeviceInfo.Storage
- /// Space macOS can reclaim by itself, mostly local snapshots and caches. Nil when the volume did not say.
- public let purgeable: Int64?
/// Local snapshots: copies of the disk kept on the disk itself. Peel explains them but never removes one,
/// because deleting a snapshot cannot be undone. Nil when `diskutil` did not say.
public let snapshots: [LocalSnapshot]?
@@ -558,8 +555,6 @@ public enum SpaceInventory {
return SpaceReport(
// An unknown size sorts first: an area that ran out of time is most likely one of the biggest.
items: items.sorted { ($0.size ?? .max) > ($1.size ?? .max) },
- storage: DeviceInfo.storage(of: home),
- purgeable: purgeableSpace(of: home),
snapshots: await LocalSnapshots.list(),
needsFullDiskAccess: items.contains {
$0.size == nil && $0.urls.contains { FullDiskAccess.canList($0) == .missing }
@@ -590,13 +585,4 @@ public enum SpaceInventory {
managingAppIsMissing: definition.managedBy.map { !$0.oneIsOnThisMac } ?? false
)
}
-
- static func purgeableSpace(of url: URL) -> Int64? {
- let values = try? url.resourceValues(forKeys: [
- .volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityKey,
- ])
- guard let important = values?.volumeAvailableCapacityForImportantUsage,
- let free = values?.volumeAvailableCapacity else { return nil }
- return max(0, important - Int64(free))
- }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
index 9cb9ca1a..3c72e9b1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceInventoryTests.swift
@@ -551,8 +551,8 @@ struct SpaceInventoryTests {
}
@Test func knowsNoPurgeableSpaceForAVolumeItCannotRead() {
- #expect(SpaceInventory.purgeableSpace(of: URL(filePath: "/Volumes/org.example.missing")) == nil)
- #expect(SpaceInventory.purgeableSpace(of: .homeDirectory) != nil)
+ #expect(DiskSpace.purgeable(of: URL(filePath: "/Volumes/org.example.missing")) == nil)
+ #expect(DiskSpace.purgeable(of: .homeDirectory) != nil)
}
/// Scanning the real Mac must work and change nothing. It walks the real home folder, so it runs only when
@@ -560,9 +560,10 @@ struct SpaceInventoryTests {
@Test(.enabled(if: ProcessInfo.processInfo.environment["PEEL_TEST_THIS_MAC"] != nil))
func measuresThisMacWithoutComplaining() async {
let report = await SpaceInventory.scan()
- #expect(report.storage.total > 0)
- #expect(report.storage.free <= report.storage.total)
- #expect((report.purgeable ?? 0) <= report.storage.total)
+ let disk = await DiskSpace.read()
+ #expect(disk.storage.total > 0)
+ #expect(disk.storage.free <= disk.storage.total)
+ #expect((disk.purgeable ?? 0) <= disk.storage.total)
#expect(report.snapshots?.allSatisfy { !$0.name.isEmpty } == true)
#expect(report.items.allSatisfy { !$0.urls.isEmpty })
}
diff --git a/Peel/Model/SpaceLibrary.swift b/Peel/Model/SpaceLibrary.swift
index 32a45da3..f8728da1 100644
--- a/Peel/Model/SpaceLibrary.swift
+++ b/Peel/Model/SpaceLibrary.swift
@@ -5,6 +5,8 @@ import PeelCore
@Observable
final class SpaceLibrary: RowSelection {
private(set) var report: SpaceReport?
+ /// The disk the band shows, read on its own: after each scan, when the page appears, and when Peel comes forward.
+ private(set) var disk: DiskSpace?
/// The scan this page runs, which a newer one or the Stop button ends.
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
@@ -30,6 +32,10 @@ final class SpaceLibrary: RowSelection {
await refresh(replanning: [])
}
+ func readDisk() async {
+ disk = await DiskSpace.read()
+ }
+
/// Scans again and makes every area's plan again, as Rescan asks: a plan also depends on which apps are open,
/// which the area's size does not show.
func rescan() async {
@@ -124,10 +130,12 @@ final class SpaceLibrary: RowSelection {
let new = await self.newPlan(for: item)
made.append((item, new.plan, new.exclusions))
}
- return (result, made)
+ return (result, made, await DiskSpace.read())
}
- guard let (result, made) = answer else { return }
+ guard let (result, made, disk) = answer else { return }
+ // Together, so the page never shows the areas without the disk's band.
report = result
+ self.disk = disk
for id in plans.keys.filter({ id in !result.items.contains { $0.id == id } }) {
selectedURLs.subtract(plans[id]?.removable ?? [])
plans[id] = nil
diff --git a/Peel/Views/Space/SpaceList.swift b/Peel/Views/Space/SpaceList.swift
index e88e009a..0384977f 100644
--- a/Peel/Views/Space/SpaceList.swift
+++ b/Peel/Views/Space/SpaceList.swift
@@ -95,23 +95,29 @@ struct SpaceList: View {
}
}
.task {
+ if space.report != nil { await space.readDisk() }
guard space.report == nil, !space.isScanning, !space.scanRun.wasStopped else { return }
await space.refresh()
}
+ .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
+ Task { await space.readDisk() }
+ }
.rescanOnExclusionChange("SpaceList") { await space.refresh() }
.rescanAfterPutBack(.space) { await space.refresh() }
}
private func storage(_ report: SpaceReport) -> some View {
VStack(alignment: .leading, spacing: 8) {
- StorageStrip(storage: report.storage)
- if let purgeable = report.purgeable, purgeable > 0 {
+ if let disk = space.disk {
+ StorageStrip(storage: disk.storage)
+ }
+ if let purgeable = space.disk?.purgeable, purgeable > 0 {
// The strip's available figure already includes these bytes, so the note ties them to it, not
// to the used figure.
Text("The available space includes \(purgeable.byteCount) that macOS clears when something needs the room.")
.font(.caption)
.foregroundStyle(.secondary)
- } else if report.purgeable == nil {
+ } else if space.disk != nil, space.disk?.purgeable == nil {
Text("Peel couldn’t find out how much of the available space macOS clears by itself.")
.font(.caption)
.foregroundStyle(.secondary)
diff --git a/PeelTests/SpaceLibraryTests.swift b/PeelTests/SpaceLibraryTests.swift
new file mode 100644
index 00000000..40466466
--- /dev/null
+++ b/PeelTests/SpaceLibraryTests.swift
@@ -0,0 +1,14 @@
+@testable import Peel
+import PeelCore
+import Testing
+
+struct SpaceLibraryTests {
+ @Test func readsTheDiskAgainWithoutScanning() async {
+ let space = SpaceLibrary()
+
+ await space.readDisk()
+
+ #expect(space.disk.map { $0.storage.total > 0 } == true)
+ #expect(space.report == nil)
+ }
+}