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
use termion::{color, style};

/// May append contents to a string.
pub(crate) trait StringAppender {
    /// Appends contents to the specified string.
    fn append_to_string(&self, string: &mut String);
}

/// A color to be applied to text foreground or background in the interface.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
}

impl StringAppender for Color {
    fn append_to_string(&self, string: &mut String) {
        match *self {
            Color::Black => string.push_str(&color::Fg(color::Black).to_string()),
            Color::Red => string.push_str(&color::Fg(color::Red).to_string()),
            Color::Green => string.push_str(&color::Fg(color::Green).to_string()),
            Color::Yellow => string.push_str(&color::Fg(color::Yellow).to_string()),
            Color::Blue => string.push_str(&color::Fg(color::Blue).to_string()),
            Color::Magenta => string.push_str(&color::Fg(color::Magenta).to_string()),
            Color::Cyan => string.push_str(&color::Fg(color::Cyan).to_string()),
            Color::White => string.push_str(&color::Fg(color::White).to_string()),
        }
    }
}

/// Styling to be applied to text in the interface.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Style {
    Bold,
    Italic,
    Underline,
}

impl ToString for Style {
    fn to_string(&self) -> String {
        match *self {
            Style::Bold => style::Bold.to_string(),
            Style::Italic => style::Italic.to_string(),
            Style::Underline => style::Underline.to_string(),
        }
    }
}

impl StringAppender for Vec<Style> {
    fn append_to_string(&self, string: &mut String) {
        for style in self {
            string.push_str(&style.to_string());
        }
    }
}