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 what is selected when File Search's field is emptied
Țugui Dragoș10a1c564dfb9
diff --git a/GUIDE.md b/GUIDE.mdindex 628e4e2b..dbee707d 100644--- a/GUIDE.md+++ b/GUIDE.md@@ -167,7 +167,7 @@ Space shows what fills your disk, area by area, and what each area is.syncs to your other devices, what you excluded, what sits where Peel never moves anything, and what macOS didn't letPeel look at. A file's size is what moving it frees, the list goes by it, and a file that frees less than itslength, such as a copy that shares its storage, says so. A file you selected that a newer search no longer lists- stays selected, and the page says how many, with Keep Selected and Deselect.+ stays selected, also once the search field is empty, and the page says how many, with Keep Selected and Deselect.<details><summary>Developer, Build Artifacts, and Space, in detail</summary>diff --git a/Peel/Model/FileSearchLibrary.swift b/Peel/Model/FileSearchLibrary.swiftindex 18eb0155..d9e50308 100644--- a/Peel/Model/FileSearchLibrary.swift+++ b/Peel/Model/FileSearchLibrary.swift@@ -84,24 +84,27 @@ final class FileSearchLibrary {recommendedURLs = Set(found.files.filter { !$0.requiresPrivileges && !$0.belongsToAnApp && $0.heldBack == nil }.map(\.url))- let outsideBefore = Set(selection.outside.keys)- selectedURLs = selection.update(selectedURLs, was: known, now: found.files)- if Set(selection.outside.keys) != outsideBefore { keepsWhatIsOutside = false }- sizes = Dictionary(- (found.files + selection.outside.values).map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first }- )+ keepSelection(was: known, now: found.files)// The search ran under the exclusions as they were when it started, so leave out anything excluded since.await leaveOut(exclusionsNow())}private func forgetResults() {+ let known = results?.files ?? []listsLanded += 1results = nilanswered = nil- selectedURLs = []- selection = SearchSelection()recommendedURLs = []- sizes = [:]+ keepSelection(was: known, now: [])+ }++ private func keepSelection(was known: [FoundFile], now files: [FoundFile]) {+ let outsideBefore = Set(selection.outside.keys)+ selectedURLs = selection.update(selectedURLs, was: known, now: files)+ if Set(selection.outside.keys) != outsideBefore { keepsWhatIsOutside = false }+ sizes = Dictionary(+ (files + selection.outside.values).map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first }+ )}/// Keeps selected the files no longer listed, and stops saying so until another search leaves out others.@@ -120,7 +123,7 @@ 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 }+ guard results != nil || !selection.outside.isEmpty, 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()@@ -136,10 +139,11 @@ final class FileSearchLibrary {}private func narrow(to exclusions: Exclusions) async {- guard let results, exclusions != filteredBy else { return }+ guard exclusions != filteredBy else { return }+ let listed = results?.files ?? []let current = generationlet list = listsLanded- let kept = await narrowing(results.files, exclusions)+ let kept = await narrowing(listed, exclusions)let outside = Array(selection.outside.values)let keptOutside = await narrowing(outside, exclusions)guard current == generation else { return }@@ -151,8 +155,8 @@ final class FileSearchLibrary {let goneOutside = Set(outside.map(\.url)).subtracting(keptOutside.map(\.url))selection.forget(goneOutside)selectedURLs.subtract(goneOutside)- guard kept.count < results.files.count else { return }- let gone = Set(results.files.map(\.url)).subtracting(kept.map(\.url))+ guard kept.count < listed.count else { return }+ let gone = Set(listed.map(\.url)).subtracting(kept.map(\.url))self.results?.files = keptself.results?.leftOut[.excluded, default: 0] += gone.countselectedURLs.subtract(gone)diff --git a/Peel/Views/FileSearch/FileSearchList.swift b/Peel/Views/FileSearch/FileSearchList.swiftindex b581609f..f110b0a6 100644--- a/Peel/Views/FileSearch/FileSearchList.swift+++ b/Peel/Views/FileSearch/FileSearchList.swift@@ -167,7 +167,7 @@ struct FileSearchList: View {}}.edgeBar(.bottom) {- if search.results?.files.isEmpty == false {+ if search.results?.files.isEmpty == false || !search.selectedURLs.isEmpty {RemovalBar(page: Tool.fileSearch.page(), isScanning: search.isSearching)}}diff --git a/PeelTests/FileSearchLibraryTests.swift b/PeelTests/FileSearchLibraryTests.swiftindex 422e48be..b8d27cd4 100644--- a/PeelTests/FileSearchLibraryTests.swift+++ b/PeelTests/FileSearchLibraryTests.swift@@ -138,6 +138,53 @@ struct FileSearchLibraryTests {#expect(library.selectedURLs == [invoice])}+ @Test func clearingTheWordsKeepsWhatIsSelected() async throws {+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: folder) }+ let kept = folder.appending(path: "kept.dmg")+ let gone = folder.appending(path: "gone.dmg")+ for url in [kept, gone] { try Data(count: 16).write(to: url) }+ let records = [kept, gone].map {+ FoundFile(url: $0, size: 4_096, length: 16, modificationDate: .distantPast, requiresPrivileges: false)+ }+ let library = FileSearchLibrary { _, _ in FileSearchResults(files: records) }+ library.criteria.name = "dmg"+ await library.search()+ library.selectedURLs = [kept, gone]+ try FileManager.default.removeItem(at: gone)++ library.criteria.name = ""+ await library.search()++ #expect(library.results == nil)+ #expect(library.selectedURLs == [kept])+ #expect(Set(library.selection.outside.keys) == [kept])+ #expect(library.carriedParts.first?.sizes == [kept: 4_096])+ }++ @Test func anExclusionReachesWhatIsSelectedWithNoListOnThePage() async throws {+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: folder) }+ let file = folder.appending(path: "file.dmg")+ try Data(count: 16).write(to: file)+ let record = FoundFile(+ url: file, size: 4_096, length: 16, modificationDate: .distantPast, requiresPrivileges: false+ )+ let library = FileSearchLibrary(run: { _, _ in FileSearchResults(files: [record]) }, exclusionsNow: { .none })+ library.criteria.name = "dmg"+ await library.search()+ library.selectedURLs = [file]+ library.criteria.name = ""+ await library.search()++ await library.leaveOut(Exclusions(paths: [file]))++ #expect(library.selectedURLs.isEmpty)+ #expect(library.selection.outside.isEmpty)+ }+@Test func keepsSelectedWhatANewerSearchLeavesOut() async throws {let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)