gitbay/MRs/MRFilter.swift
45 lines · 1432 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 /// 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 author: String?
24 var milestone: String?
25
26 func flags() -> [String] {
27 var flags = ["--state", state.rawValue]
28 flags += filterFlag("--search", search)
29 flags += filterFlag("--author", author)
30 flags += filterFlag("--milestone", milestone)
31 return flags
32 }
33
34 var isNarrowed: Bool { flags().count > 2 }
35
36 var emptyMessage: String {
37 let noun = "merge requests"
38 if isNarrowed {
39 return state == .all
40 ? "No \(noun) match these filters."
41 : "No \(state.label.lowercased()) \(noun) match these filters."
42 }
43 return state == .all ? "No \(noun)." : "No \(state.label.lowercased()) \(noun)."
44 }
45}