gitbay/MRs/MRFilter.swift

v1.4.0
gitbay-ios/gitbay/MRs/MRFilter.swift history · blame · raw

47 lines · 1481 bytes

 1import Foundation
 2
 3/// What `mr list` is narrowed to. The command takes no `--assignee`, so
 4/// this offers none.
 5nonisolated struct MRFilter: Equatable, Sendable {
 6
 7    enum State: String, CaseIterable, Identifiable, Sendable {
 8        case open, merged, closed
 9        case sourceGone = "source_gone"
10        case all
11
12        var id: String { rawValue }
13        /// Segment text. Five segments share a phone's width, so the
14        /// source-gone state is one word; the rows and chips still say
15        /// "source gone" in full.
16        var label: String {
17            self == .sourceGone ? "Gone" : rawValue.capitalized
18        }
19    }
20
21    var state: State = .open
22    var search = ""
23    var label: String?
24    var author: String?
25    var milestone: String?
26
27    func flags() -> [String] {
28        var flags = ["--state", state.rawValue]
29        flags += filterFlag("--search", search)
30        flags += filterFlag("--label", label)
31        flags += filterFlag("--author", author)
32        flags += filterFlag("--milestone", milestone)
33        return flags
34    }
35
36    var isNarrowed: Bool { flags().count > 2 }
37
38    var emptyMessage: String {
39        let noun = "merge requests"
40        if isNarrowed {
41            return state == .all
42                ? "No \(noun) match these filters."
43                : "No \(state.label.lowercased()) \(noun) match these filters."
44        }
45        return state == .all ? "No \(noun)." : "No \(state.label.lowercased()) \(noun)."
46    }
47}