use std::fmt::Display;
#[derive(Clone, Copy, Debug, Default)]
#[must_use]
pub struct ColorSettings {
pub fore: Fore,
pub back: Back,
pub style: Style,
}
impl ColorSettings {
pub const RESET: Self = Self {
fore: Fore::Reset,
back: Back::Reset,
style: Style::Default,
};
pub const fn from_fore(fore: Fore) -> Self {
Self {
fore,
back: Back::Reset,
style: Style::Default,
}
}
pub const fn from_back(back: Back) -> Self {
Self {
fore: Fore::Reset,
back,
style: Style::Default,
}
}
pub const fn new(fore: Fore, back: Back) -> Self {
Self {
fore,
back,
style: Style::Default,
}
}
pub const fn with_fore(self, fore: Fore) -> Self {
Self {
fore,
back: self.back,
style: self.style,
}
}
pub const fn with_back(self, back: Back) -> Self {
Self {
fore: self.fore,
back,
style: self.style,
}
}
pub const fn with_style(self, style: Style) -> Self {
Self {
fore: self.fore,
back: self.back,
style,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[must_use]
pub enum Fore {
#[default]
Reset,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
}
#[derive(Clone, Copy, Debug, Default)]
#[must_use]
pub enum Back {
#[default]
Reset,
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
}
#[derive(Clone, Copy, Debug, Default)]
pub enum Style {
#[default]
Default,
Wide,
Underline,
Reserve,
Blink,
Hide,
}
impl Display for Fore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Reset => "\x1b[0m",
Self::Black => "\x1b[30m",
Self::Red => "\x1b[31m",
Self::Green => "\x1b[32m",
Self::Yellow => "\x1b[33m",
Self::Blue => "\x1b[34m",
Self::Magenta => "\x1b[35m",
Self::Cyan => "\x1b[36m",
Self::White => "\x1b[37m",
Self::BrightBlack => "\x1b[90m",
Self::BrightRed => "\x1b[91m",
Self::BrightGreen => "\x1b[92m",
Self::BrightYellow => "\x1b[93m",
Self::BrightBlue => "\x1b[94m",
Self::BrightMagenta => "\x1b[95m",
Self::BrightCyan => "\x1b[96m",
Self::BrightWhite => "\x1b[97m",
}
)
}
}
impl Display for Back {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Reset => "\x1b[0m",
Self::Black => "\x1b[40m",
Self::Red => "\x1b[41m",
Self::Green => "\x1b[42m",
Self::Yellow => "\x1b[43m",
Self::Blue => "\x1b[44m",
Self::Magenta => "\x1b[45m",
Self::Cyan => "\x1b[46m",
Self::White => "\x1b[47m",
Self::BrightBlack => "\x1b[100m",
Self::BrightRed => "\x1b[101m",
Self::BrightGreen => "\x1b[102m",
Self::BrightYellow => "\x1b[103m",
Self::BrightBlue => "\x1b[104m",
Self::BrightMagenta => "\x1b[105m",
Self::BrightCyan => "\x1b[106m",
Self::BrightWhite => "\x1b[107m",
}
)
}
}
impl Display for Style {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Default => "\x1b0m",
Self::Wide => "\x1b1m",
Self::Underline => "\x1b4m",
Self::Blink => "\x1b5m",
Self::Reserve => "\x1b[7m",
Self::Hide => "\x1b[8m",
}
)
}
}
impl Display for ColorSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match (self.fore, self.back, self.style) {
(Fore::Reset, Back::Reset, Style::Default) => write!(f, "\x1b[0m"),
(fr, Back::Reset, Style::Default) => write!(f, "\x1b[0m{fr}"),
(fr, Back::Reset, st) => write!(f, "\x1b[0m{fr}{st}"),
(Fore::Reset, bg, Style::Default) => write!(f, "\x1b[0m{bg}"),
(Fore::Reset, bg, st) => write!(f, "\x1b[0m{bg}{st}"),
(fr, bg, Style::Default) => write!(f, "\x1b[0m{fr}{bg}"),
(fr, bg, st) => write!(f, "{fr}{bg}{st}"),
}
}
}