use std::fmt;
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Color {
Default,
Black,
DarkRed,
DarkGreen,
DarkYellow,
DarkBlue,
DarkMagenta,
DarkCyan,
DarkGray,
Gray,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
Ansi(u8),
Rgb(u8, u8, u8),
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
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! {
struct SetForegroundColor(Color) =>
|this, f| match this.0 {
Color::Default => write!(f, sgr!("39")),
_ => write!(f, sgr!("38;{}"), this.0),
}
}
sequence! {
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",
)
);