1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use ratatui::{style::Stylize, text::Span};

/// The result of a prompt.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Status {
    #[default]
    Pending,
    Aborted,
    Done,
}

impl Status {
    #[must_use]
    pub const fn is_pending(&self) -> bool {
        matches!(self, Self::Pending)
    }

    #[must_use]
    pub const fn is_aborted(&self) -> bool {
        matches!(self, Self::Aborted)
    }

    #[must_use]
    pub const fn is_done(&self) -> bool {
        matches!(self, Self::Done)
    }

    #[must_use]
    pub const fn is_finished(&self) -> bool {
        matches!(self, Self::Done | Self::Aborted)
    }

    #[must_use]
    pub fn symbol(&self) -> Span<'static> {
        match self {
            Self::Pending => Symbols::default().pending,
            Self::Aborted => Symbols::default().aborted,
            Self::Done => Symbols::default().done,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Symbols {
    pub pending: Span<'static>,
    pub aborted: Span<'static>,
    pub done: Span<'static>,
}

impl Default for Symbols {
    fn default() -> Self {
        Self {
            pending: "?".cyan(),
            aborted: "✘".red(),
            done: "✔".green(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn status_symbols() {
        assert_eq!(Status::Pending.symbol(), "?".cyan());
        assert_eq!(Status::Aborted.symbol(), "✘".red());
        assert_eq!(Status::Done.symbol(), "✔".green());
    }

    #[test]
    fn status_is_pending() {
        assert!(Status::Pending.is_pending());
        assert!(!Status::Aborted.is_pending());
        assert!(!Status::Done.is_pending());
    }

    #[test]
    fn status_is_aborted() {
        assert!(!Status::Pending.is_aborted());
        assert!(Status::Aborted.is_aborted());
        assert!(!Status::Done.is_aborted());
    }

    #[test]
    fn status_is_done() {
        assert!(!Status::Pending.is_done());
        assert!(!Status::Aborted.is_done());
        assert!(Status::Done.is_done());
    }

    #[test]
    fn status_is_finished() {
        assert!(!Status::Pending.is_finished());
        assert!(Status::Aborted.is_finished());
        assert!(Status::Done.is_finished());
    }

    #[test]
    fn status_default() {
        assert_eq!(Status::default(), Status::Pending);
    }

    #[test]
    fn symbols_default() {
        assert_eq!(
            Symbols::default(),
            Symbols {
                pending: "?".cyan(),
                aborted: "✘".red(),
                done: "✔".green(),
            }
        );
    }

    #[test]
    fn symbols_custom() {
        let symbols = Symbols {
            pending: "P".cyan(),
            aborted: "A".red(),
            done: "D".green(),
        };
        assert_eq!(symbols.pending, "P".cyan());
        assert_eq!(symbols.aborted, "A".red());
        assert_eq!(symbols.done, "D".green());
    }
}