Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Mac: search for tags with AND, OR and NOT

gelenmedyab538c714f69f
diff --git a/macos/Sources/RustyFiles/BrowserWindowController.swift b/macos/Sources/RustyFiles/BrowserWindowController.swift
index 9235fca4..ca492029 100644
--- a/macos/Sources/RustyFiles/BrowserWindowController.swift
+++ b/macos/Sources/RustyFiles/BrowserWindowController.swift
@@ -989,6 +989,24 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate, NSMen
t.searchHits = []
t.searchDone = false
let pl = listFor(t) ?? list
+ // "tag:red AND work", "tag:red OR blue NOT done" (Windows' tag query): Spotlight's tags
+ if let expr = TagQuery.expression(spec.query) {
+ let (column, ascending, first, hidden) = (pl.sortKey.sort, pl.ascending, pl.foldersFirst, pl.showHidden)
+ let names = TagStore.all.map(\.name)
+ let label = t.url.absoluteString
+ Task.detached(priority: .userInitiated) {
+ let paths = TagQuery.run(expr, under: spec.root.path, known: names)
+ let f = Folder.gathered(label: label, paths: paths, column: column, ascending: ascending, foldersFirst: first, showHidden: hidden)
+ await MainActor.run { [weak self] in
+ guard let self, t.loadID == id else { return }
+ t.folder = f
+ t.error = nil
+ t.searchDone = true
+ self.refreshPane(t)
+ }
+ }
+ return
+ }
let job = SearchJob.start(root: spec.root.path, query: spec.query, includeHidden: pl.showHidden)
t.search = job
if t.folder == nil || t.searchHits.isEmpty {
diff --git a/macos/Sources/RustyFiles/SelfTest.swift b/macos/Sources/RustyFiles/SelfTest.swift
index a38a7e44..1ead787e 100644
--- a/macos/Sources/RustyFiles/SelfTest.swift
+++ b/macos/Sources/RustyFiles/SelfTest.swift
@@ -61,6 +61,10 @@ enum SelfTest {
check(GitRepo.isValidBranchName("feature/new-look") && !GitRepo.isValidBranchName("bad name")
&& !GitRepo.isValidBranchName("a..b") && !GitRepo.isValidBranchName("-x") && !GitRepo.isValidBranchName("x.lock"),
"a branch name is checked as git does")
+ let tagged: [String: Set<String>] = ["red": ["/a", "/b"], "work": ["/b", "/c"], "done": ["/c"]]
+ let tq = { (q: String) in TagQuery.eval(TagQuery.parse(TagQuery.expression(q) ?? ""), with: { tagged[$0] ?? [] }, all: { ["/a", "/b", "/c"] }) }
+ check(tq("tag:red AND work") == ["/b"] && tq("tag:red OR work NOT done") == ["/a", "/b"] && tq("etiket:değil done") == ["/a", "/b"],
+ "tag searches with AND, OR and NOT read left to right")
// 0. the Git look-up ends at the disk's root and finds a repository from inside it (issue 429)
check(GitRepo.repoRoot(URL(fileURLWithPath: "/")) == nil, "no repository above the root")
diff --git a/macos/Sources/RustyFiles/Tags.swift b/macos/Sources/RustyFiles/Tags.swift
index a4ee95fc..041b26ab 100644
--- a/macos/Sources/RustyFiles/Tags.swift
+++ b/macos/Sources/RustyFiles/Tags.swift
@@ -175,3 +175,66 @@ extension SettingsContent {
]
}
}
+
+
+/// A search for tags: "tag:red", "tag:red AND work", "tag:red OR blue NOT done", read left to
+/// right as on Windows (tags.rs parse_query / eval_query); "etiket:" and "ve / veya / değil" too.
+enum TagQuery {
+ enum Join: Equatable { case and, or, not }
+
+ /// The expression after "tag:", or nil when the search is not one for tags.
+ static func expression(_ query: String) -> String? {
+ let q = query.trimmingCharacters(in: .whitespaces)
+ for prefix in ["tag:", "etiket:"] where q.lowercased().hasPrefix(prefix) {
+ let rest = String(q.dropFirst(prefix.count)).trimmingCharacters(in: .whitespaces)
+ return rest.isEmpty ? nil : rest
+ }
+ return nil
+ }
+
+ static func parse(_ text: String) -> [(Join, String)] {
+ var out: [(Join, String)] = []
+ var join = Join.or
+ var name = ""
+ func flush() {
+ if !name.isEmpty { out.append((join, name)); name = "" }
+ }
+ for word in text.split(whereSeparator: \.isWhitespace).map(String.init) {
+ switch word.lowercased() {
+ case "and", "ve", "&", "&&": flush(); join = .and
+ case "or", "veya", "|", "||": flush(); join = .or
+ case "not", "değil", "degil", "!": flush(); join = .not
+ default: name += (name.isEmpty ? "" : " ") + word
+ }
+ }
+ flush()
+ return out
+ }
+
+ static func eval(_ terms: [(Join, String)], with: (String) -> Set<String>, all: () -> Set<String>) -> Set<String> {
+ var set = Set<String>()
+ for (i, (join, name)) in terms.enumerated() {
+ let tagged = with(name)
+ switch join {
+ case .or: set.formUnion(tagged)
+ case .and: set.formIntersection(tagged)
+ case .not:
+ if i == 0 { set = all() }
+ set.subtract(tagged)
+ }
+ }
+ return set
+ }
+
+ /// The paths under `root` that the expression asks for; a tag is named as in `known`,
+ /// whatever its letter case.
+ nonisolated static func run(_ expr: String, under root: String, known: [String]) -> [String] {
+ let inside = root == "/" ? "/" : root + "/"
+ func tagged(_ name: String) -> Set<String> {
+ let real = known.first { $0.compare(name, options: [.caseInsensitive, .diacriticInsensitive]) == .orderedSame } ?? name
+ return Set(TagStore.files(tagged: real).filter { $0.hasPrefix(inside) })
+ }
+ let result = eval(parse(expr), with: tagged, all: { known.reduce(into: Set<String>()) { $0.formUnion(tagged($1)) } })
+ return result.sorted()
+ }
+}