tui_prompts/
status.rs

1use ratatui_core::style::Stylize;
2use ratatui_core::text::Span;
3
4/// The result of a prompt.
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum Status {
7    #[default]
8    Pending,
9    Aborted,
10    Done,
11}
12
13impl Status {
14    #[must_use]
15    pub const fn is_pending(&self) -> bool {
16        matches!(self, Self::Pending)
17    }
18
19    #[must_use]
20    pub const fn is_aborted(&self) -> bool {
21        matches!(self, Self::Aborted)
22    }
23
24    #[must_use]
25    pub const fn is_done(&self) -> bool {
26        matches!(self, Self::Done)
27    }
28
29    #[must_use]
30    pub const fn is_finished(&self) -> bool {
31        matches!(self, Self::Done | Self::Aborted)
32    }
33
34    #[must_use]
35    pub fn symbol(&self) -> Span<'static> {
36        match self {
37            Self::Pending => Symbols::default().pending,
38            Self::Aborted => Symbols::default().aborted,
39            Self::Done => Symbols::default().done,
40        }
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45struct Symbols {
46    pub pending: Span<'static>,
47    pub aborted: Span<'static>,
48    pub done: Span<'static>,
49}
50
51impl Default for Symbols {
52    fn default() -> Self {
53        Self {
54            pending: "?".cyan(),
55            aborted: "✘".red(),
56            done: "✔".green(),
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn status_symbols() {
67        assert_eq!(Status::Pending.symbol(), "?".cyan());
68        assert_eq!(Status::Aborted.symbol(), "✘".red());
69        assert_eq!(Status::Done.symbol(), "✔".green());
70    }
71
72    #[test]
73    fn status_is_pending() {
74        assert!(Status::Pending.is_pending());
75        assert!(!Status::Aborted.is_pending());
76        assert!(!Status::Done.is_pending());
77    }
78
79    #[test]
80    fn status_is_aborted() {
81        assert!(!Status::Pending.is_aborted());
82        assert!(Status::Aborted.is_aborted());
83        assert!(!Status::Done.is_aborted());
84    }
85
86    #[test]
87    fn status_is_done() {
88        assert!(!Status::Pending.is_done());
89        assert!(!Status::Aborted.is_done());
90        assert!(Status::Done.is_done());
91    }
92
93    #[test]
94    fn status_is_finished() {
95        assert!(!Status::Pending.is_finished());
96        assert!(Status::Aborted.is_finished());
97        assert!(Status::Done.is_finished());
98    }
99
100    #[test]
101    fn status_default() {
102        assert_eq!(Status::default(), Status::Pending);
103    }
104
105    #[test]
106    fn symbols_default() {
107        assert_eq!(
108            Symbols::default(),
109            Symbols {
110                pending: "?".cyan(),
111                aborted: "✘".red(),
112                done: "✔".green(),
113            }
114        );
115    }
116
117    #[test]
118    fn symbols_custom() {
119        let symbols = Symbols {
120            pending: "P".cyan(),
121            aborted: "A".red(),
122            done: "D".green(),
123        };
124        assert_eq!(symbols.pending, "P".cyan());
125        assert_eq!(symbols.aborted, "A".red());
126        assert_eq!(symbols.done, "D".green());
127    }
128}