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.

Narrow File Search's list in turn, and search again once exclusions can be read

Țugui Dragoșe5c92d27a351
diff --git a/Peel/Model/FileSearchLibrary.swift b/Peel/Model/FileSearchLibrary.swift
index c51cc947..18eb0155 100644
--- a/Peel/Model/FileSearchLibrary.swift
+++ b/Peel/Model/FileSearchLibrary.swift
@@ -31,13 +31,25 @@ final class FileSearchLibrary {
/// The exclusions the list is filtered by: those the search ran under, or those it was narrowed to since.
private var filteredBy = Exclusions.none
private let run: @Sendable (FileSearchCriteria, Exclusions) async -> FileSearchResults
+ private let narrowing: @Sendable ([FoundFile], Exclusions) async -> [FoundFile]
+ private let exclusionsNow: () -> Exclusions
+ /// Moves whenever another list lands, so that narrowing the one before it writes nothing over it.
+ private var listsLanded = 0
+ /// The last narrowing asked for. Each waits for the one before, or both would count what they leave out.
+ @ObservationIgnored private var lastNarrowing: Task<Void, Never>?
init(
run: @escaping @Sendable (FileSearchCriteria, Exclusions) async -> FileSearchResults = {
await FileSearch.run($0, exclusions: $1)
- }
+ },
+ narrowing: @escaping @Sendable ([FoundFile], Exclusions) async -> [FoundFile] = {
+ await keeping($0, under: $1)
+ },
+ exclusionsNow: @escaping () -> Exclusions = { ExclusionsStore.shared.exclusions }
) {
self.run = run
+ self.narrowing = narrowing
+ self.exclusionsNow = exclusionsNow
}
var chosenFile: FoundFile? {
@@ -54,7 +66,7 @@ final class FileSearchLibrary {
forgetResults()
return
}
- let exclusions = ExclusionsStore.shared.exclusions
+ let exclusions = exclusionsNow()
let run = run
guard let found = await scanRun.run({ await run(criteria, exclusions) }) else {
// Canceled with the task that asked for it, as when the page goes away, the search answered nothing,
@@ -65,6 +77,7 @@ final class FileSearchLibrary {
return
}
let known = results?.files ?? []
+ listsLanded += 1
results = found
answered = criteria
filteredBy = exclusions
@@ -78,10 +91,11 @@ final class FileSearchLibrary {
(found.files + selection.outside.values).map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first }
)
// The search ran under the exclusions as they were when it started, so leave out anything excluded since.
- await leaveOut(ExclusionsStore.shared.exclusions)
+ await leaveOut(exclusionsNow())
}
private func forgetResults() {
+ listsLanded += 1
results = nil
answered = nil
selectedURLs = []
@@ -106,12 +120,33 @@ final class FileSearchLibrary {
/// Narrows the list to what `exclusions` leave. File Search runs only when asked, so without this a file
/// excluded from its own row would stay listed and selected until the move refused it.
func leaveOut(_ exclusions: Exclusions) async {
+ guard results != nil, exclusions != filteredBy else { return }
+ // Exclusions Peel couldn't read left everything out for a reason now gone, and narrowing brings nothing back.
+ if !filteredBy.isKnown, exclusions.isKnown {
+ await search()
+ return
+ }
+ let before = lastNarrowing
+ let turn = Task {
+ await before?.value
+ await narrow(to: exclusions)
+ }
+ lastNarrowing = turn
+ await turn.value
+ }
+
+ private func narrow(to exclusions: Exclusions) async {
guard let results, exclusions != filteredBy else { return }
let current = generation
- let kept = await Self.keeping(results.files, under: exclusions)
+ let list = listsLanded
+ let kept = await narrowing(results.files, exclusions)
let outside = Array(selection.outside.values)
- let keptOutside = await Self.keeping(outside, under: exclusions)
+ let keptOutside = await narrowing(outside, exclusions)
guard current == generation else { return }
+ guard list == listsLanded else {
+ await narrow(to: exclusions)
+ return
+ }
filteredBy = exclusions
let goneOutside = Set(outside.map(\.url)).subtracting(keptOutside.map(\.url))
selection.forget(goneOutside)
diff --git a/PeelTests/FileSearchLibraryTests.swift b/PeelTests/FileSearchLibraryTests.swift
index e10058f1..422e48be 100644
--- a/PeelTests/FileSearchLibraryTests.swift
+++ b/PeelTests/FileSearchLibraryTests.swift
@@ -28,6 +28,83 @@ struct FileSearchLibraryTests {
return (library, started.makeAsyncIterator())
}
+ private func found(_ urls: URL...) -> FileSearchResults {
+ FileSearchResults(files: urls.map {
+ FoundFile(url: $0, size: 1, length: 1, modificationDate: .distantPast, requiresPrivileges: false)
+ })
+ }
+
+ @Test func narrowingAnOldListLeavesTheNewOneAlone() async throws {
+ let budget = URL(filePath: "/org.example/budget.pdf")
+ let (first, second) = (found(invoice), found(budget))
+ let calls = OSAllocatedUnfairLock(initialState: 0)
+ let landing = OSAllocatedUnfairLock<CheckedContinuation<Void, Never>?>(initialState: nil)
+ let narrowingHeld = OSAllocatedUnfairLock<CheckedContinuation<Void, Never>?>(initialState: nil)
+ let holding = OSAllocatedUnfairLock(initialState: true)
+ let library = FileSearchLibrary(run: { _, _ in
+ guard calls.withLock({ $0 += 1; return $0 }) > 1 else { return first }
+ await withCheckedContinuation { continuation in landing.withLock { $0 = continuation } }
+ return second
+ }, narrowing: { files, exclusions in
+ if holding.withLock({ $0 }) {
+ holding.withLock { $0 = false }
+ await withCheckedContinuation { continuation in narrowingHeld.withLock { $0 = continuation } }
+ }
+ return exclusions.keeping(files, url: \.url)
+ }, exclusionsNow: { .none })
+ library.criteria.name = "invoice"
+ await library.search()
+
+ library.criteria.name = "budget"
+ let searching = Task { await library.search() }
+ while landing.withLock({ $0 }) == nil { await Task.yield() }
+ let narrowing = Task { await library.leaveOut(Exclusions(paths: [invoice])) }
+ while narrowingHeld.withLock({ $0 }) == nil { await Task.yield() }
+ landing.withLock { $0 }?.resume()
+ while library.results?.files.map(\.url) != [budget] { await Task.yield() }
+ narrowingHeld.withLock { $0 }?.resume()
+ await narrowing.value
+ await searching.value
+
+ #expect(library.results?.files.map(\.url) == [budget])
+ #expect(library.results?.leftOut[.excluded] == nil)
+ }
+
+ @Test func twoNarrowingsCountWhatTheyLeaveOutOnce() async throws {
+ let results = found(invoice)
+ let library = FileSearchLibrary(run: { _, _ in results }, exclusionsNow: { .none })
+ library.criteria.name = "invoice"
+ await library.search()
+ let excluded = Exclusions(paths: [invoice])
+
+ async let one: Void = library.leaveOut(excluded)
+ async let other: Void = library.leaveOut(Exclusions(paths: [invoice, URL(filePath: "/org.example/other.pdf")]))
+ _ = await (one, other)
+
+ #expect(library.results?.files.isEmpty == true)
+ #expect(library.results?.leftOut[.excluded] == 1)
+ }
+
+ @Test func aSearchAllLeftOutForUnreadableExclusionsRunsAgainOnceTheyAreRead() async throws {
+ let calls = OSAllocatedUnfairLock(initialState: 0)
+ var exclusions = Exclusions.unreadable
+ let library = FileSearchLibrary(
+ run: { _, _ in
+ calls.withLock { $0 += 1 }
+ return FileSearchResults()
+ },
+ exclusionsNow: { exclusions }
+ )
+ library.criteria.name = "invoice"
+ await library.search()
+ #expect(calls.withLock { $0 } == 1)
+
+ exclusions = .none
+ await library.leaveOut(.none)
+
+ #expect(calls.withLock { $0 } == 2)
+ }
+
@Test func aSearchStoppedForOtherWordsLeavesNothingOfTheOldOnes() async {
var (library, started) = library()
library.criteria.name = "invoice"