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.

Space keeps choices by the same rule

Țugui Dragoș1470bca7ae90
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
index fa479304..95871b63 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/SpaceRemoval.swift
@@ -23,12 +23,9 @@ public enum SpaceRemoval {
Set(inUse.map(\.name)).sorted()
}
- /// What to select of this plan when it replaces `previous`, the area's last plan, `selected` being what
- /// was selected: a child that was already there keeps its checkbox as it was, and a child new to the area
- /// is selected when its size is known, as every child is in an area's first plan.
- public func selection(replacing previous: Plan?, selected: Set<URL>) -> Set<URL> {
- let known = Set(previous?.removable ?? [])
- return Set(removable.filter { known.contains($0) ? selected.contains($0) : sizes[$0] != nil })
+ /// What Peel selects of this plan for the person: every child it could measure.
+ public var suggested: Set<URL> {
+ Set(sizes.keys)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
index 90d2f594..354fcf5f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SpaceRemovalTests.swift
@@ -189,27 +189,31 @@ struct SpaceRemovalTests {
/// An area's plan is made again when the disk changes, and what the person chose in it stays chosen: a child
/// that was already there keeps its checkbox as it was, one new to the area is selected when its size is
- /// known, as every child is in the area's first plan, and one that went is no longer selected.
+ /// known, as every child is in the area's first plan, and one that went is no longer selected. A child Peel
+ /// selected whose size can no longer be measured is no longer selected either.
@Test func aPlanMadeAgainKeepsWhatThePersonChose() {
let caches = URL(filePath: "/Users/x/Library/Caches", directoryHint: .isDirectory)
- let (kept, deselected, chosenByHand, unmeasured, new, gone) = (
+ let (kept, deselected, chosenByHand, unmeasured, new, gone, noLongerMeasured) = (
caches.appending(path: "com.a"), caches.appending(path: "com.b"), caches.appending(path: "com.c"),
- caches.appending(path: "com.d"), caches.appending(path: "com.e"), caches.appending(path: "com.f")
+ caches.appending(path: "com.d"), caches.appending(path: "com.e"), caches.appending(path: "com.f"),
+ caches.appending(path: "com.g")
)
let first = SpaceRemoval.Plan(
- removable: [kept, deselected, chosenByHand, unmeasured, gone],
+ removable: [kept, deselected, chosenByHand, unmeasured, gone, noLongerMeasured],
inUse: [],
leftToDeveloper: [],
- sizes: [kept: 1, deselected: 2, gone: 5]
+ sizes: [kept: 1, deselected: 2, gone: 5, noLongerMeasured: 7]
)
- #expect(first.selection(replacing: nil, selected: []) == [kept, deselected, gone])
+ var choices = KeptSelection()
+ #expect(choices.update([], selectable: Set(first.removable), suggested: first.suggested) == [kept, deselected, gone, noLongerMeasured])
let again = SpaceRemoval.Plan(
- removable: [kept, deselected, chosenByHand, unmeasured, new],
+ removable: [kept, deselected, chosenByHand, unmeasured, new, noLongerMeasured],
inUse: [],
leftToDeveloper: [],
sizes: [kept: 1, deselected: 2, chosenByHand: 3, unmeasured: 4, new: 6]
)
- #expect(again.selection(replacing: first, selected: [kept, chosenByHand, gone]) == [kept, chosenByHand, new])
+ let selected: Set = [kept, chosenByHand, gone, noLongerMeasured]
+ #expect(choices.update(selected, selectable: Set(again.removable), suggested: again.suggested) == [kept, chosenByHand, new])
}
}
diff --git a/Peel/Model/SpaceLibrary.swift b/Peel/Model/SpaceLibrary.swift
index 069124c4..f28a6abd 100644
--- a/Peel/Model/SpaceLibrary.swift
+++ b/Peel/Model/SpaceLibrary.swift
@@ -16,6 +16,8 @@ final class SpaceLibrary: RowSelection {
private(set) var plans: [SpaceItem.ID: SpaceRemoval.Plan] = [:]
/// The area as it was, and the exclusions' revision, when its plan was made.
@ObservationIgnored private var plannedFor: [SpaceItem.ID: (item: SpaceItem, exclusions: Int)] = [:]
+ /// What was chosen in each area, kept through its plans.
+ @ObservationIgnored private var choices: [SpaceItem.ID: KeptSelection] = [:]
var selectedItem: SpaceItem? {
report?.items.first { $0.id == selection }
@@ -25,8 +27,7 @@ final class SpaceLibrary: RowSelection {
await refresh(replanning: [])
}
- /// Makes the area's plan from what is on disk now, keeping what was chosen in it
- /// (`SpaceRemoval.Plan.selection(replacing:selected:)`).
+ /// Makes the area's plan from what is on disk now, keeping what was chosen in it.
func plan(_ item: SpaceItem) async {
let revision = ExclusionsStore.shared.revision
let plan = await SpaceRemoval.plan(
@@ -37,7 +38,11 @@ final class SpaceLibrary: RowSelection {
// A plan cut short measured only part of the area, and would read the rest as unknown.
guard !Task.isCancelled else { return }
let previous = plans[item.id]
- let chosen = plan.selection(replacing: previous, selected: selectedURLs)
+ let chosen = choices[item.id, default: KeptSelection()].update(
+ selectedURLs,
+ selectable: Set(plan.removable),
+ suggested: plan.suggested
+ )
selectedURLs.subtract(previous?.removable ?? [])
selectedURLs.formUnion(chosen)
plans[item.id] = plan
@@ -53,6 +58,7 @@ final class SpaceLibrary: RowSelection {
selectedURLs.subtract(plans[id]?.removable ?? [])
plans[id] = nil
plannedFor[id] = nil
+ choices[id] = nil
}
if let selection, !result.items.contains(where: { $0.id == selection }) {
self.selection = nil