gitbay/MRs/MRFilter.swift

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

42 lines · 1262 bytes

 1import Foundation
 2
 3/// What `mr list` is narrowed to. The command takes no `--label` and no
 4/// `--assignee`, so this offers neither.
 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        var label: String {
14            self == .sourceGone ? "Source gone" : rawValue.capitalized
15        }
16    }
17
18    var state: State = .open
19    var search = ""
20    var author: String?
21    var milestone: String?
22
23    func flags() -> [String] {
24        var flags = ["--state", state.rawValue]
25        flags += filterFlag("--search", search)
26        flags += filterFlag("--author", author)
27        flags += filterFlag("--milestone", milestone)
28        return flags
29    }
30
31    var isNarrowed: Bool { flags().count > 2 }
32
33    var emptyMessage: String {
34        let noun = "merge requests"
35        if isNarrowed {
36            return state == .all
37                ? "No \(noun) match these filters."
38                : "No \(state.label.lowercased()) \(noun) match these filters."
39        }
40        return state == .all ? "No \(noun)." : "No \(state.label.lowercased()) \(noun)."
41    }
42}