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.

Move Duplicates' part last when one Move to Trash covers several pages

Țugui Dragoșff6fd2831c36
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift b/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
index e4974c2d..acd1aad7 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/CarriedSelection.swift
@@ -24,13 +24,19 @@ public struct CarriedSelection: Sendable {
public let source: String
public let sourceKey: String?
public let sizes: [URL: Int64?]
+ /// The tool checks right before its move what the parts before it left, as Duplicates does for the copy it
+ /// keeps, so it moves after every other part.
+ public let movesLast: Bool
- public init(page: Page, title: String, source: String, sourceKey: String?, sizes: [URL: Int64?]) {
+ public init(
+ page: Page, title: String, source: String, sourceKey: String?, sizes: [URL: Int64?], movesLast: Bool = false
+ ) {
self.page = page
self.title = title
self.source = source
self.sourceKey = sourceKey
self.sizes = sizes
+ self.movesLast = movesLast
}
public var id: Page { page }
@@ -38,7 +44,7 @@ public struct CarriedSelection: Sendable {
public var total: SizeTotal { SizeTotal(sizes.values) }
func keeping(_ sizes: [URL: Int64?]) -> Part {
- Part(page: page, title: title, source: source, sourceKey: sourceKey, sizes: sizes)
+ Part(page: page, title: title, source: source, sourceKey: sourceKey, sizes: sizes, movesLast: movesLast)
}
public var removalPart: RemovalPart {
@@ -68,9 +74,7 @@ public struct CarriedSelection: Sendable {
let urls = Set(result.failures.filter(\.reason.isAboutTheItem).map(\.url))
let sizes = part.sizes.filter { urls.contains($0.key) }
guard !sizes.isEmpty else { return nil }
- return Part(
- page: part.page, title: part.title, source: part.source, sourceKey: part.sourceKey, sizes: sizes
- )
+ return part.keeping(sizes)
}
}
}
@@ -100,10 +104,12 @@ public struct CarriedSelection: Sendable {
}
/// The parts that move: those of `candidates` whose page was seen and has something selected, in the order of
- /// `tools`, which is the sidebar's, and within a tool in the order its pages were seen.
+ /// `tools`, which is the sidebar's, and within a tool in the order its pages were seen, a part that moves last
+ /// after all of them.
public func parts(from candidates: [Part], order tools: [String], asking page: Page? = nil) -> [Part] {
- func place(_ part: Part) -> (Int, Int) {
- (tools.firstIndex(of: part.page.tool) ?? tools.count, seen.firstIndex(of: part.page) ?? seen.count)
+ func place(_ part: Part) -> (Int, Int, Int) {
+ (part.movesLast ? 1 : 0, tools.firstIndex(of: part.page.tool) ?? tools.count,
+ seen.firstIndex(of: part.page) ?? seen.count)
}
let parts = candidates.filter { !$0.sizes.isEmpty && seen.contains($0.page) }.sorted { place($0) < place($1) }
return Self.eachItemOnce(in: parts, asking: page)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
index d435344b..55758c15 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackDuplicateTests.swift
@@ -89,6 +89,43 @@ struct AttackDuplicateTests {
}
}
+ @Test func aCopyAnotherPageMovesFirstLeavesTheOtherInPlace() async throws {
+ let directory = try TemporaryDirectory()
+ let contents = bytes(4_000)
+ try directory.file("home/Downloads/film.mov", contents: contents)
+ try directory.file("home/Movies/film.mov", contents: contents)
+ let found = try await scan(directory, folders: ["home"])
+ let group = try #require(found.groups.first)
+ let selected = try #require(found.suggestedSelection.first)
+ let kept = try #require(group.files.map(\.url).first { $0 != selected })
+ var carried = CarriedSelection()
+ for tool in ["duplicates", "fileSearch"] {
+ carried.saw(CarriedSelection.Page(tool: tool, scope: ""))
+ }
+ let parts = carried.parts(from: [
+ CarriedSelection.Part(
+ page: CarriedSelection.Page(tool: "duplicates", scope: ""), title: "Duplicates", source: "Duplicates",
+ sourceKey: nil, sizes: [selected: 4_000], movesLast: true
+ ),
+ CarriedSelection.Part(
+ page: CarriedSelection.Page(tool: "fileSearch", scope: ""), title: "File Search", source: "File Search",
+ sourceKey: nil, sizes: [kept: 4_000]
+ ),
+ ], order: ["duplicates", "fileSearch"])
+ let trashService = try service(directory)
+
+ _ = await CarriedSelection.pass(parts) { part in
+ part.page.tool == "duplicates"
+ ? await DuplicateRemoval.trash(Set(part.sizes.keys), from: found, using: trashService)
+ : await trashService.trash(Array(part.sizes.keys))
+ }
+
+ let left = group.files.map(\.url).filter {
+ FileManager.default.fileExists(atPath: $0.path(percentEncoded: false))
+ }
+ #expect(left == [selected], "ATTACK SUCCEEDED: a pass over two pages took every copy")
+ }
+
/// Something else (a sync client, Finder, another window) can take the kept copy away while the removal runs.
/// The kept copy is checked again before every move, so the group never loses its last copy.
@Test func theKeptCopyIsOnlyCheckedBeforeTheFirstMove() async throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
index 6239f99f..ea309262 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CarriedSelectionTests.swift
@@ -62,6 +62,23 @@ struct CarriedSelectionTests {
])
}
+ @Test func aPartWhoseToolChecksWhatTheOthersLeftMovesLast() {
+ var selection = CarriedSelection()
+ for tool in ["duplicates", "fileSearch", "developer"] {
+ selection.saw(Page(tool: tool, scope: ""))
+ }
+ let duplicates = part("duplicates", "", ["Downloads/Film.mov": 900])
+
+ let parts = selection.parts(from: [
+ Part(page: duplicates.page, title: duplicates.title, source: duplicates.source, sourceKey: nil,
+ sizes: duplicates.sizes, movesLast: true),
+ part("fileSearch", "", ["Movies/Film.mov": 900]),
+ part("developer", "", ["Library/Caches/pip": 300]),
+ ], order: tools)
+
+ #expect(parts.map(\.page.tool) == ["developer", "fileSearch", "duplicates"])
+ }
+
/// A page with nothing selected carries nothing.
@Test func aPageWithNothingSelectedIsLeftOut() {
var selection = CarriedSelection()
diff --git a/Peel/Model/DuplicateLibrary.swift b/Peel/Model/DuplicateLibrary.swift
index 2cc5cfbb..f55baed0 100644
--- a/Peel/Model/DuplicateLibrary.swift
+++ b/Peel/Model/DuplicateLibrary.swift
@@ -280,7 +280,8 @@ extension DuplicateLibrary: CarriesSelection {
title: String(localized: Tool.duplicates.title),
source: Tool.duplicates.title.inEnglish,
sourceKey: "tool",
- sizes: selectedSizes
+ sizes: selectedSizes,
+ movesLast: true
)]
}