1use std::fmt;
2
3#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
22pub enum Color {
23 Default,
25 Black,
27 DarkRed,
29 DarkGreen,
31 DarkYellow,
33 DarkBlue,
35 DarkMagenta,
37 DarkCyan,
39 DarkGray,
43 Gray,
47 Red,
49 Green,
51 Yellow,
53 Blue,
55 Magenta,
57 Cyan,
59 White,
61 Ansi(u8),
72 Rgb(u8, u8, u8),
76}
77
78impl fmt::Display for Color {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 Color::Default => Ok(()),
83 Color::Black => write!(f, "5;0"),
84 Color::DarkRed => write!(f, "5;1"),
85 Color::DarkGreen => write!(f, "5;2"),
86 Color::DarkYellow => write!(f, "5;3"),
87 Color::DarkBlue => write!(f, "5;4"),
88 Color::DarkMagenta => write!(f, "5;5"),
89 Color::DarkCyan => write!(f, "5;6"),
90 Color::Gray => write!(f, "5;7"),
91 Color::DarkGray => write!(f, "5;8"),
92 Color::Red => write!(f, "5;9"),
93 Color::Green => write!(f, "5;10"),
94 Color::Yellow => write!(f, "5;11"),
95 Color::Blue => write!(f, "5;12"),
96 Color::Magenta => write!(f, "5;13"),
97 Color::Cyan => write!(f, "5;14"),
98 Color::White => write!(f, "5;15"),
99 Color::Ansi(value) => write!(f, "5;{}", value),
100 Color::Rgb(r, g, b) => write!(f, "2;{};{};{}", r, g, b),
101 }
102 }
103}
104
105sequence! {
106 struct SetForegroundColor(Color) =>
119 |this, f| match this.0 {
120 Color::Default => write!(f, sgr!("39")),
121 _ => write!(f, sgr!("38;{}"), this.0),
122 }
123}
124
125sequence! {
126 struct SetBackgroundColor(Color) =>
139 |this, f| match this.0 {
140 Color::Default => write!(f, sgr!("49")),
141 _ => write!(f, sgr!("48;{}"), this.0),
142 }
143}
144
145#[cfg(test)]
146test_sequences!(
147 set_foreground_color(
148 SetForegroundColor(Color::Default) => "\x1B[39m",
149 SetForegroundColor(Color::Black) => "\x1B[38;5;0m",
150 SetForegroundColor(Color::DarkRed) => "\x1B[38;5;1m",
151 SetForegroundColor(Color::DarkGreen) => "\x1B[38;5;2m",
152 SetForegroundColor(Color::DarkYellow) => "\x1B[38;5;3m",
153 SetForegroundColor(Color::DarkBlue) => "\x1B[38;5;4m",
154 SetForegroundColor(Color::DarkMagenta) => "\x1B[38;5;5m",
155 SetForegroundColor(Color::DarkCyan) => "\x1B[38;5;6m",
156 SetForegroundColor(Color::DarkGray) => "\x1B[38;5;8m",
157 SetForegroundColor(Color::Gray) => "\x1B[38;5;7m",
158 SetForegroundColor(Color::Red) => "\x1B[38;5;9m",
159 SetForegroundColor(Color::Green) => "\x1B[38;5;10m",
160 SetForegroundColor(Color::Yellow) => "\x1B[38;5;11m",
161 SetForegroundColor(Color::Blue) => "\x1B[38;5;12m",
162 SetForegroundColor(Color::Magenta) => "\x1B[38;5;13m",
163 SetForegroundColor(Color::Cyan) => "\x1B[38;5;14m",
164 SetForegroundColor(Color::White) => "\x1B[38;5;15m",
165 SetForegroundColor(Color::Ansi(200)) => "\x1B[38;5;200m",
166 SetForegroundColor(Color::Rgb(1, 2, 3)) => "\x1B[38;2;1;2;3m",
167 ),
168 set_background_color(
169 SetBackgroundColor(Color::Default) => "\x1B[49m",
170 SetBackgroundColor(Color::Black) => "\x1B[48;5;0m",
171 SetBackgroundColor(Color::DarkRed) => "\x1B[48;5;1m",
172 SetBackgroundColor(Color::DarkGreen) => "\x1B[48;5;2m",
173 SetBackgroundColor(Color::DarkYellow) => "\x1B[48;5;3m",
174 SetBackgroundColor(Color::DarkBlue) => "\x1B[48;5;4m",
175 SetBackgroundColor(Color::DarkMagenta) => "\x1B[48;5;5m",
176 SetBackgroundColor(Color::DarkCyan) => "\x1B[48;5;6m",
177 SetBackgroundColor(Color::DarkGray) => "\x1B[48;5;8m",
178 SetBackgroundColor(Color::Gray) => "\x1B[48;5;7m",
179 SetBackgroundColor(Color::Red) => "\x1B[48;5;9m",
180 SetBackgroundColor(Color::Green) => "\x1B[48;5;10m",
181 SetBackgroundColor(Color::Yellow) => "\x1B[48;5;11m",
182 SetBackgroundColor(Color::Blue) => "\x1B[48;5;12m",
183 SetBackgroundColor(Color::Magenta) => "\x1B[48;5;13m",
184 SetBackgroundColor(Color::Cyan) => "\x1B[48;5;14m",
185 SetBackgroundColor(Color::White) => "\x1B[48;5;15m",
186 SetBackgroundColor(Color::Ansi(200)) => "\x1B[48;5;200m",
187 SetBackgroundColor(Color::Rgb(1, 2, 3)) => "\x1B[48;2;1;2;3m",
188 )
189);