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 a Developer tool by the names of its folders

Țugui Dragoș265f0c13aebc
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index 003ed100..fd0c6f0f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -1651,3 +1651,10 @@ public enum DeveloperCaches {
)
}
}
+
+extension DeveloperEnvironment {
+ /// What a search looks in: the tool's name and the last two names in the path of each folder it keeps.
+ public var searchKey: String {
+ ([name] + locations.flatMap { $0.url.pathComponents.suffix(2) }).joined(separator: "\n")
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index 98bd7b5c..ab0cc7a5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -63,6 +63,21 @@ struct DeveloperCachesTests {
#expect(!refused.isRecommended && !slow.isRecommended)
}
+ @Test func aSearchFindsAToolByTheNamesOfItsFolders() {
+ let hub = URL(filePath: "/Users/me/.cache/huggingface/hub", directoryHint: .isDirectory)
+ let tool = DeveloperEnvironment(
+ id: "huggingface", name: "Hugging Face", systemImage: "hammer", appBundleIdentifiers: [],
+ locations: [DeveloperEnvironment.Location(url: hub, kind: .models, size: 1, source: "https://example.com")]
+ )
+
+ let list = SearchableList([tool]) { $0.searchKey }
+
+ #expect(list.matching("").count == 1)
+ #expect(list.matching("hugging").count == 1)
+ #expect(list.matching("huggingface").count == 1, "the folder's own name found nothing")
+ #expect(list.matching("DerivedData").isEmpty)
+ }
+
/// Xcode's caches wait on Simulator too, so the request to quit names whichever of an environment's apps is
/// running: the environment's name would ask for Xcode while only Simulator runs, and the request would keep
/// coming back. Finder runs on every Mac, and an identifier nobody uses stands for an app that is closed.
diff --git a/Peel/Model/DeveloperLibrary.swift b/Peel/Model/DeveloperLibrary.swift
index 686a02d1..b3736c51 100644
--- a/Peel/Model/DeveloperLibrary.swift
+++ b/Peel/Model/DeveloperLibrary.swift
@@ -5,6 +5,9 @@ import PeelCore
@Observable
final class DeveloperLibrary {
private(set) var environments: [DeveloperEnvironment]?
+ @ObservationIgnored private var searchable = SearchableList<DeveloperEnvironment>([]) { $0.searchKey }
+ /// The last search and what it found. A view asks again whenever it is drawn.
+ @ObservationIgnored private var lastMatch: (query: String, environments: [DeveloperEnvironment])?
/// The scan this page runs, which a newer one or the Stop button ends.
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
@@ -16,12 +19,23 @@ final class DeveloperLibrary {
environments?.first { $0.id == selection }
}
+ /// The tools whose name or folders hold `query`, every tool for an empty one.
+ func environments(matching query: String) -> [DeveloperEnvironment] {
+ guard environments != nil else { return [] }
+ if let lastMatch, lastMatch.query == query { return lastMatch.environments }
+ let found = searchable.matching(query)
+ lastMatch = (query, found)
+ return found
+ }
+
func refresh() async {
guard
let result = await scanRun.run({
await DeveloperCaches.scan(exclusions: ExclusionsStore.shared.exclusions)
})
else { return }
+ searchable = SearchableList(result) { $0.searchKey }
+ lastMatch = nil
environments = result
selectedURLs.formIntersection(Set(result.flatMap(\.locations).map(\.url)))
if let selection, !result.contains(where: { $0.id == selection }) {
diff --git a/Peel/Views/Developer/DeveloperList.swift b/Peel/Views/Developer/DeveloperList.swift
index 8b423068..35dd29b1 100644
--- a/Peel/Views/Developer/DeveloperList.swift
+++ b/Peel/Views/Developer/DeveloperList.swift
@@ -7,9 +7,7 @@ struct DeveloperList: View {
@State private var searchText = ""
private var listed: [DeveloperEnvironment] {
- let all = developer.environments ?? []
- guard !searchText.isEmpty else { return all }
- return all.filter { SearchText.matches($0.name, searchText) }
+ developer.environments(matching: searchText)
}
var body: some View {