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.

Search iCloud Drive by what its rows show

Țugui Dragoș59344f237805
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudFolders.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudFolders.swift
index ce64cdb1..7a8c8d75 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudFolders.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudFolders.swift
@@ -24,6 +24,16 @@ public struct CloudFolder: Sendable, Hashable, Identifiable {
public static func ordered(_ folders: [CloudFolder]) -> [CloudFolder] {
folders.sorted { $0.size != $1.size ? $0.size > $1.size : $0.names.lexicographicallyPrecedes($1.names) }
}
+
+ /// A search over `files` that reads only what their rows and their folders' rows show, never the part of the path
+ /// every file shares. A line end keeps a query from matching across two names.
+ public static func searchable(_ files: [CloudFile], in folders: [CloudFolder]) -> SearchableList<CloudFile> {
+ var shown: [URL: [String]] = [:]
+ for folder in folders {
+ for file in folder.files { shown[file.url] = folder.names }
+ }
+ return SearchableList(files) { ((shown[$0.url] ?? []) + [$0.name]).joined(separator: "\n") }
+ }
}
extension CloudStorage {
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 87fa6857..5f32bbd4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -410,11 +410,3 @@ public enum CloudStorage {
return parts.last.map(String.init) ?? folder
}
}
-
-extension CloudFile {
- /// What a search looks in: the folder it belongs to, and the whole path, which ends in its name, since the row
- /// shows an abbreviated path. A line end keeps a query from matching across the two.
- public var searchKey: String {
- container + "\n" + url.path(percentEncoded: false)
- }
-}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
index f7908217..e8244542 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
@@ -49,21 +49,23 @@ struct SearchingTests {
#expect(!artifact.matches("DerivedData"))
}
- @Test func findsAnICloudFileByNameFolderOrPath() {
+ @Test func findsAnICloudFileByWhatItsRowsShow() {
+ let home = URL(filePath: "/Users/someone", directoryHint: .isDirectory)
let file = CloudFile(
- url: URL(filePath: "/Users/x/Library/Mobile Documents/com~apple~CloudDocs/Muzica FARA NUME.mov"),
+ url: home.appending(path: "Library/Mobile Documents/com~apple~CloudDocs/Concerts/Muzica FARA NUME.mov"),
name: "Muzica FARA NUME.mov",
container: "iCloud Drive",
size: 10,
modified: nil
)
- let list = SearchableList([file], key: \.searchKey)
- #expect(list.matching("muzica") == [file])
- #expect(list.matching("CloudDocs") == [file])
- #expect(list.matching("iCloud") == [file])
- #expect(list.matching("Wallpaper").isEmpty)
- #expect(list.matching("Drive/Users").isEmpty)
+ let list = CloudFolder.searchable([file], in: CloudStorage.folders(of: [file], home: home))
+ for shown in ["muzica", "iCloud", "Concerts"] {
+ #expect(list.matching(shown) == [file], "\(shown)")
+ }
+ for hidden in ["Mobile", "Library", "CloudDocs", "someone", "Users", "Drive Concerts", "Wallpaper"] {
+ #expect(list.matching(hidden).isEmpty, "\(hidden)")
+ }
}
@Test func findsIntelSoftwareByNameOrTheAppItSitsIn() {
@@ -89,7 +91,7 @@ struct SearchingTests {
modified: nil
)
- let list = SearchableList([file], key: \.searchKey)
+ let list = CloudFolder.searchable([file], in: [])
#expect(list.matching("sedinta") == [file])
#expect(list.matching("ȘEDINȚĂ") == [file])
}
@@ -97,7 +99,7 @@ struct SearchingTests {
@Test func anEmptyQueryKeepsEverything() {
#expect(duplicates("/a/b.txt").matches(""))
let file = CloudFile(url: URL(filePath: "/a"), name: "a", container: "c", size: 1, modified: nil)
- #expect(SearchableList([file], key: \.searchKey).matching("") == [file])
+ #expect(CloudFolder.searchable([file], in: []).matching("") == [file])
#expect(IntelFinding(url: URL(filePath: "/a"), kind: .app, name: "a", owner: nil, size: 1).matches(""))
}
diff --git a/Peel/Model/CloudLibrary.swift b/Peel/Model/CloudLibrary.swift
index 3854b0e4..f4518233 100644
--- a/Peel/Model/CloudLibrary.swift
+++ b/Peel/Model/CloudLibrary.swift
@@ -6,14 +6,13 @@ import PeelCore
final class CloudLibrary {
private(set) var files: [CloudFile]? {
didSet {
- searchable = SearchableList(files ?? [], key: \.searchKey)
lastMatch = nil
lastFolders = nil
sizes = Dictionary((files ?? []).map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first })
updateSelectedSize()
}
}
- @ObservationIgnored private var searchable = SearchableList<CloudFile>([], key: \.searchKey)
+ @ObservationIgnored private var searchable = CloudFolder.searchable([], in: [])
/// The last search and what it found. A view asks again whenever it is drawn, a checkbox included.
@ObservationIgnored private var lastMatch: (query: String, files: [CloudFile])?
@ObservationIgnored private var sizes: [URL: Int64] = [:]
@@ -86,6 +85,7 @@ final class CloudLibrary {
})
else { return }
folders = scan.folders
+ searchable = CloudFolder.searchable(scan.files, in: scan.folders)
files = scan.files
cutShort = scan.cutShort
couldNotRead = scan.couldNotRead