aboutsummaryrefslogtreecommitdiff
path: root/src/sequences/color.rs
blob: b019f0fddfc9949a65b56a169fdcf4e2a43e0b53 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::fmt;

/// A color.
///
/// This is **NOT** a full ANSI sequence. `Color` must be used along with
/// the:
///
/// * [`SetBackgroundColor`](struct.SetBackgroundColor.html)
/// * [`SetForegroundColor`](struct.SetForegroundColor.html)
///
/// # Examples
///
/// ```no_run
/// use std::io::{stdout, Write};
/// use anes::{Color, SetForegroundColor};
///
/// let mut stdout = stdout();
/// // Set the foreground color to red
/// write!(stdout, "{}", SetForegroundColor(Color::Red));
/// ```
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Color {
    /// Default color.
    Default,
    /// Black color.
    Black,
    /// Dark red color.
    DarkRed,
    /// Dark green color.
    DarkGreen,
    /// Dark yellow color.
    DarkYellow,
    /// Dark blue color.
    DarkBlue,
    /// Dark magenta color.
    DarkMagenta,
    /// Dark cyan color.
    DarkCyan,
    /// Dark gray color.
    ///
    /// Also knows as light (bright) black.
    DarkGray,
    /// Light (bright) gray color.
    ///
    /// Also known as dark white.
    Gray,
    /// Light (bright) red color.
    Red,
    /// Light (bright) green color.
    Green,
    /// Light (bright) yellow color.
    Yellow,
    /// Light (bright) blue color.
    Blue,
    /// Light (bright) magenta color.
    Magenta,
    /// Light (bright) cyan color.
    Cyan,
    /// White color.
    White,
    /// A color from the predefined set of ANSI colors.
    ///
    /// ```text
    ///    0 - 7:  standard colors (as in ESC [ 30–37 m)
    ///    8- 15:  high intensity colors (as in ESC [ 90–97 m)
    ///   16-231:  6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
    ///  232-255:  grayscale from black to white in 24 steps
    /// ```
    ///
    /// See [8-bit](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) for more information.
    Ansi(u8),
    /// An RGB color.
    ///
    /// See [24-bit](https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit) for more information.
    Rgb(u8, u8, u8),
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            // Color::Default is handled in the SetBackgroundColor & SetForegroundColor
            Color::Default => Ok(()),
            Color::Black => write!(f, "5;0"),
            Color::DarkRed => write!(f, "5;1"),
            Color::DarkGreen => write!(f, "5;2"),
            Color::DarkYellow => write!(f, "5;3"),
            Color::DarkBlue => write!(f, "5;4"),
            Color::DarkMagenta => write!(f, "5;5"),
            Color::DarkCyan => write!(f, "5;6"),
            Color::Gray => write!(f, "5;7"),
            Color::DarkGray => write!(f, "5;8"),
            Color::Red => write!(f, "5;9"),
            Color::Green => write!(f, "5;10"),
            Color::Yellow => write!(f, "5;11"),
            Color::Blue => write!(f, "5;12"),
            Color::Magenta => write!(f, "5;13"),
            Color::Cyan => write!(f, "5;14"),
            Color::White => write!(f, "5;15"),
            Color::Ansi(value) => write!(f, "5;{}", value),
            Color::Rgb(r, g, b) => write!(f, "2;{};{};{}", r, g, b),
        }
    }
}

sequence! {
    /// Sets the foreground color.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::{Color, SetForegroundColor};
    ///
    /// let mut stdout = stdout();
    /// // Set the foreground color to blue
    /// write!(stdout, "{}", SetForegroundColor(Color::Blue));
    /// ```
    struct SetForegroundColor(Color) =>
    |this, f| match this.0 {
        Color::Default => write!(f, sgr!("39")),
        _ => write!(f, sgr!("38;{}"), this.0),
    }
}

sequence! {
    /// Sets the background color.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::{Color, SetBackgroundColor};
    ///
    /// let mut stdout = stdout();
    /// // Set the background color to yellow
    /// write!(stdout, "{}", SetBackgroundColor(Color::Yellow));
    /// ```
    struct SetBackgroundColor(Color) =>
    |this, f| match this.0 {
        Color::Default => write!(f, sgr!("49")),
        _ => write!(f, sgr!("48;{}"), this.0),
    }
}

#[cfg(test)]
test_sequences!(
    set_foreground_color(
        SetForegroundColor(Color::Default) => "\x1B[39m",
        SetForegroundColor(Color::Black) => "\x1B[38;5;0m",
        SetForegroundColor(Color::DarkRed) => "\x1B[38;5;1m",
        SetForegroundColor(Color::DarkGreen) => "\x1B[38;5;2m",
        SetForegroundColor(Color::DarkYellow) => "\x1B[38;5;3m",
        SetForegroundColor(Color::DarkBlue) => "\x1B[38;5;4m",
        SetForegroundColor(Color::DarkMagenta) => "\x1B[38;5;5m",
        SetForegroundColor(Color::DarkCyan) => "\x1B[38;5;6m",
        SetForegroundColor(Color::DarkGray) => "\x1B[38;5;8m",
        SetForegroundColor(Color::Gray) => "\x1B[38;5;7m",
        SetForegroundColor(Color::Red) => "\x1B[38;5;9m",
        SetForegroundColor(Color::Green) => "\x1B[38;5;10m",
        SetForegroundColor(Color::Yellow) => "\x1B[38;5;11m",
        SetForegroundColor(Color::Blue) => "\x1B[38;5;12m",
        SetForegroundColor(Color::Magenta) => "\x1B[38;5;13m",
        SetForegroundColor(Color::Cyan) => "\x1B[38;5;14m",
        SetForegroundColor(Color::White) => "\x1B[38;5;15m",
        SetForegroundColor(Color::Ansi(200)) => "\x1B[38;5;200m",
        SetForegroundColor(Color::Rgb(1, 2, 3)) => "\x1B[38;2;1;2;3m",
    ),
    set_background_color(
        SetBackgroundColor(Color::Default) => "\x1B[49m",
        SetBackgroundColor(Color::Black) => "\x1B[48;5;0m",
        SetBackgroundColor(Color::DarkRed) => "\x1B[48;5;1m",
        SetBackgroundColor(Color::DarkGreen) => "\x1B[48;5;2m",
        SetBackgroundColor(Color::DarkYellow) => "\x1B[48;5;3m",
        SetBackgroundColor(Color::DarkBlue) => "\x1B[48;5;4m",
        SetBackgroundColor(Color::DarkMagenta) => "\x1B[48;5;5m",
        SetBackgroundColor(Color::DarkCyan) => "\x1B[48;5;6m",
        SetBackgroundColor(Color::DarkGray) => "\x1B[48;5;8m",
        SetBackgroundColor(Color::Gray) => "\x1B[48;5;7m",
        SetBackgroundColor(Color::Red) => "\x1B[48;5;9m",
        SetBackgroundColor(Color::Green) => "\x1B[48;5;10m",
        SetBackgroundColor(Color::Yellow) => "\x1B[48;5;11m",
        SetBackgroundColor(Color::Blue) => "\x1B[48;5;12m",
        SetBackgroundColor(Color::Magenta) => "\x1B[48;5;13m",
        SetBackgroundColor(Color::Cyan) => "\x1B[48;5;14m",        
        SetBackgroundColor(Color::White) => "\x1B[48;5;15m",
        SetBackgroundColor(Color::Ansi(200)) => "\x1B[48;5;200m",
        SetBackgroundColor(Color::Rgb(1, 2, 3)) => "\x1B[48;2;1;2;3m",
    )
);