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.

Find which page holds the folders above an item once per folder

Țugui Dragoș8b465c2fe725
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
index 44ffcfd4..63bcbad5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
@@ -120,16 +120,18 @@ public struct CarriedSelection: Sendable {
private static func eachItemOnce(in parts: [Part], asking page: Page?) -> [Part] {
guard parts.count > 1 else { return parts }
let paths = parts.map { part in part.sizes.keys.map { (url: $0, path: writtenPath(of: $0)) } }
- var holder: [Substring: Int] = [:]
+ var holder = [Substring: Int](minimumCapacity: paths.reduce(0) { $0 + $1.count })
let asking = parts.indices.filter { parts[$0].page == page }
- for index in asking + parts.indices.filter({ !asking.contains($0) }) {
- for (_, path) in paths[index] where holder[path] == nil {
+ // Written last to first, so a path two parts share is left to the first.
+ for index in (asking + parts.indices.filter { !asking.contains($0) }).reversed() {
+ for (_, path) in paths[index] {
holder[path] = index
}
}
+ var above = FolderHolders(holder: holder)
return parts.indices.compactMap { index in
let kept = paths[index].filter { _, path in
- holder[path] == index && !folders(above: path).contains { holder[$0].map { $0 != index } ?? false }
+ holder[path] == index && above.noPartBut(index, holdsAFolderAbove: path)
}
guard kept.count < parts[index].sizes.count else { return parts[index] }
let urls = Set(kept.map(\.url))
@@ -137,6 +139,44 @@ public struct CarriedSelection: Sendable {
}
}
+ /// Which parts hold the folders above a path, worked out once for each folder: the items of a selection share
+ /// their folders, and the bar asks for the parts whenever a selection changes.
+ private struct FolderHolders {
+ private enum Holders {
+ case none, one(Int), several
+ }
+
+ let holder: [Substring: Int]
+ private var known: [Substring: Holders] = [:]
+
+ init(holder: [Substring: Int]) {
+ self.holder = holder
+ }
+
+ mutating func noPartBut(_ index: Int, holdsAFolderAbove path: Substring) -> Bool {
+ switch holders(above: path) {
+ case .none: true
+ case .one(let holder): holder == index
+ case .several: false
+ }
+ }
+
+ /// The parts holding a folder above `path`, from those holding the folder above its own.
+ private mutating func holders(above path: Substring) -> Holders {
+ guard let slash = path.utf8.lastIndex(of: UInt8(ascii: "/")), slash != path.startIndex else { return .none }
+ let folder = path[..<slash]
+ if let holders = known[folder] { return holders }
+ var holders = holders(above: folder)
+ switch (holders, holder[folder]) {
+ case (.none, let index?): holders = .one(index)
+ case (.one(let other), let index?) where other != index: holders = .several
+ default: break
+ }
+ known[folder] = holders
+ return holders
+ }
+ }
+
/// Moves each part in turn with `move`, which runs the part's own tool with its own checks and answers nil when
/// the tool kept the whole part (an app it waits for was opened, for example). Every part is tried, whatever
/// came of the ones before.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
index 1733478e..ae3dfe0e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
@@ -207,6 +207,38 @@ struct CarriedSelectionTests {
#expect(parts.itemCount == 2)
}
+ /// A page's own folder and an item inside it stay as its tool made them when another page carries a selection too.
+ @Test func aPagesOwnFolderAndItsItemStayBesideAnotherPage() {
+ var selection = CarriedSelection()
+ selection.saw(Page(tool: "installers", scope: "updateDownload"))
+ selection.saw(Page(tool: "space", scope: "caches"))
+
+ let parts = selection.parts(from: [
+ part("installers", "updateDownload", ["Library/Caches/notes-updater": 300]),
+ part("space", "caches", ["Library/Caches/org.example.notes": 900, "Library/Caches/org.example.notes/a": 1]),
+ ], order: tools)
+
+ #expect(parts.itemCount == 3)
+ }
+
+ /// An item under folders two other pages selected moves with the outermost, whichever page asks.
+ @Test func anItemUnderFoldersTwoPagesSelectedMovesWithTheOutermost() {
+ var selection = CarriedSelection()
+ selection.saw(Page(tool: "installers", scope: "updateDownload"))
+ selection.saw(Page(tool: "duplicates", scope: ""))
+ selection.saw(Page(tool: "space", scope: "caches"))
+ let sparkle = "Library/Caches/org.example.notes/org.sparkle-project.Sparkle"
+
+ let parts = selection.parts(from: [
+ part("installers", "updateDownload", [sparkle: 300]),
+ part("duplicates", "", ["\(sparkle)/1/update.zip": 200, "Downloads/a.dmg": 50]),
+ part("space", "caches", ["Library/Caches/org.example.notes": 900]),
+ ], order: tools, asking: Page(tool: "duplicates", scope: ""))
+
+ #expect(parts.map(\.page.tool) == ["duplicates", "space"])
+ #expect(parts.total.known == 950)
+ }
+
/// After a pass, every part that holds an item that moved, or one inside a folder that moved, so its tool is
/// brought up to date even when another page moved it.
@Test func findsThePartsHoldingWhatMoved() {