Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Keep the plan of a Space area that started last
Țugui Dragoș11b5feaf8b31
diff --git a/Peel/Model/SpaceLibrary.swift b/Peel/Model/SpaceLibrary.swiftindex e08c5e70..aa595719 100644--- a/Peel/Model/SpaceLibrary.swift+++ b/Peel/Model/SpaceLibrary.swift@@ -24,6 +24,17 @@ final class SpaceLibrary: RowSelection {private(set) var models: SizeTotal?@ObservationIgnored private var modelsMeasurement = 0@ObservationIgnored private var quitCheck: Task<Void, Never>?+ /// How many plans of each area have started, so that a plan that started earlier and ended later is dropped: it can+ /// name folders a move has taken since.+ @ObservationIgnored private var plansStarted: [SpaceItem.ID: Int] = [:]+ private let makePlan: @Sendable (SpaceItem, Exclusions, [RunningCopies.Process]) async -> SpaceRemoval.Plan++ init(+ makingPlans makePlan: @escaping @Sendable (SpaceItem, Exclusions, [RunningCopies.Process]) async+ -> SpaceRemoval.Plan = { await SpaceRemoval.plan(for: $0, exclusions: $1, running: $2) }+ ) {+ self.makePlan = makePlan+ }var selectedItem: SpaceItem? {report?.items.first { $0.id == selection }@@ -48,7 +59,7 @@ final class SpaceLibrary: RowSelection {let made = await newPlan(for: item)// A plan cut short measured only part of the area, and would read the rest as unknown.guard !Task.isCancelled else { return }- keep(made.plan, for: item, madeWith: made.exclusions)+ keep(made, for: item)await measureWhatIsLeftToDeveloper(in: item)}@@ -75,19 +86,25 @@ final class SpaceLibrary: RowSelection {plans[item.id]?.keptSizes = keptSizes}- private func newPlan(for item: SpaceItem) async -> (plan: SpaceRemoval.Plan, exclusions: Int) {+ private struct MadePlan {+ let plan: SpaceRemoval.Plan+ /// The exclusions' revision it was made with.+ let exclusions: Int+ let number: Int+ }++ private func newPlan(for item: SpaceItem) async -> MadePlan {+ let number = (plansStarted[item.id] ?? 0) + 1+ plansStarted[item.id] = numberlet revision = ExclusionsStore.shared.revision- let plan = await SpaceRemoval.plan(- for: item,- exclusions: ExclusionsStore.shared.exclusions,- running: RunningCopies.current- )- return (plan, revision)+ let plan = await makePlan(item, ExclusionsStore.shared.exclusions, RunningCopies.current)+ return MadePlan(plan: plan, exclusions: revision, number: number)}- private func keep(_ plan: SpaceRemoval.Plan, for item: SpaceItem, madeWith revision: Int) {+ private func keep(_ made: MadePlan, for item: SpaceItem) {+ guard plansStarted[item.id] == made.number else { return }let previous = plans[item.id]- var plan = plan+ var plan = made.plan// The sizes measured for the same folders stand until they are measured again, so the page's notice holds.if let previous, previous.leftToDeveloper == plan.leftToDeveloper {plan.developerSizes = previous.developerSizes@@ -97,7 +114,7 @@ final class SpaceLibrary: RowSelection {selectedURLs.subtract(previous?.removable ?? [])selectedURLs.formUnion(chosen)plans[item.id] = plan- plannedFor[item.id] = (item, revision)+ plannedFor[item.id] = (item, made.exclusions)}/// Makes the plans of the areas in `items` that have none yet, as the page's scan: Rescan turns into Stop@@ -106,16 +123,15 @@ final class SpaceLibrary: RowSelection {let unplanned = items.filter { !$0.isReadOnly && plans[$0.id] == nil }guard !unplanned.isEmpty else { return true }let answer = await scanRun.run {- var made: [(item: SpaceItem, plan: SpaceRemoval.Plan, exclusions: Int)] = []+ var made: [(item: SpaceItem, plan: MadePlan)] = []for item in unplanned where !Task.isCancelled {- let new = await self.newPlan(for: item)- made.append((item, new.plan, new.exclusions))+ made.append((item, await self.newPlan(for: item)))}return made}guard let made = answer else { return false }- for (item, plan, revision) in made {- keep(plan, for: item, madeWith: revision)+ for (item, plan) in made {+ keep(plan, for: item)}return true}@@ -140,13 +156,12 @@ final class SpaceLibrary: RowSelection {let keeping = keepingTheOpenArea ? Set([selection].compactMap(\.self)) : []let answer = await scanRun.run {let result = await SpaceInventory.scan(keeping: keeping, exclusions: exclusions)- var made: [(item: SpaceItem, plan: SpaceRemoval.Plan, exclusions: Int)] = []+ var made: [(item: SpaceItem, plan: MadePlan)] = []for item in result.items where self.plans[item.id] != nil && !Task.isCancelled {let planned = self.plannedFor[item.id]let changed = planned?.item != item || planned?.exclusions != ExclusionsStore.shared.revisionguard replanning.contains(item.id) || changed else { continue }- let new = await self.newPlan(for: item)- made.append((item, new.plan, new.exclusions))+ made.append((item, await self.newPlan(for: item)))}return (result, made, await DiskSpace.read())}@@ -162,10 +177,10 @@ final class SpaceLibrary: RowSelection {if let selection, !result.items.contains(where: { $0.id == selection }) {self.selection = nil}- for (item, plan, revision) in made {- keep(plan, for: item, madeWith: revision)+ for (item, plan) in made {+ keep(plan, for: item)}- for (item, _, _) in made {+ for (item, _) in made {await measureWhatIsLeftToDeveloper(in: item)}if #available(macOS 27, *) {diff --git a/PeelTests/SpaceLibraryTests.swift b/PeelTests/SpaceLibraryTests.swiftindex 40466466..631d6463 100644--- a/PeelTests/SpaceLibraryTests.swift+++ b/PeelTests/SpaceLibraryTests.swift@@ -1,7 +1,10 @@+import Foundation@testable import Peel-import PeelCore+@testable import PeelCore+import Synchronizationimport Testing+@MainActorstruct SpaceLibraryTests {@Test func readsTheDiskAgainWithoutScanning() async {let space = SpaceLibrary()@@ -11,4 +14,33 @@ struct SpaceLibraryTests {#expect(space.disk.map { $0.storage.total > 0 } == true)#expect(space.report == nil)}++ @Test func aPlanThatStartedEarlierAndEndedLaterIsDropped() async throws {+ let older = URL(filePath: "/Users/x/Library/Caches/org.example.moved")+ let newer = URL(filePath: "/Users/x/Library/Caches/org.example.left")+ let waiting = Mutex<[CheckedContinuation<SpaceRemoval.Plan, Never>]>([])+ let space = SpaceLibrary(makingPlans: { _, _, _ in+ await withCheckedContinuation { continuation in waiting.withLock { $0.append(continuation) } }+ })+ let item = SpaceItem(+ id: "caches", category: .library, urls: [URL(filePath: "/Users/x/Library/Caches")],+ size: SizeTotal(known: 0, isComplete: true), handling: .trash+ )+ func plan(_ url: URL) -> SpaceRemoval.Plan {+ SpaceRemoval.Plan(removable: [url], inUse: [], leftToDeveloper: [], sizes: [url: 1])+ }++ let first = Task { await space.plan(item) }+ while waiting.withLock({ $0.count }) < 1 { await Task.yield() }+ let second = Task { await space.plan(item) }+ while waiting.withLock({ $0.count }) < 2 { await Task.yield() }+ let (one, two) = waiting.withLock { ($0[0], $0[1]) }++ two.resume(returning: plan(newer))+ await second.value+ one.resume(returning: plan(older))+ await first.value++ #expect(space.plans["caches"]?.removable == [newer])+ }}