#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum Color {
Reset,
Black,
DarkGrey,
Red,
DarkRed,
Green,
DarkGreen,
Yellow,
DarkYellow,
Blue,
DarkBlue,
Magenta,
DarkMagenta,
Cyan,
DarkCyan,
White,
Grey,
Rgb { r: u8, g: u8, b: u8 },
AnsiValue(u8),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Style {
pub fg: Option<Color>,
pub bg: Option<Color>,
pub bold: bool,
pub dim: bool,
pub italic: bool,
pub underline: bool,
pub crossedout: bool,
pub reverse: bool,
}
impl Style {
pub const fn new() -> Self {
Self {
fg: None,
bg: None,
bold: false,
dim: false,
italic: false,
underline: false,
crossedout: false,
reverse: false,
}
}
pub fn fg(mut self, color: Color) -> Self {
self.fg = Some(color);
self
}
pub fn bg(mut self, color: Color) -> Self {
self.bg = Some(color);
self
}
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
pub fn dim(mut self) -> Self {
self.dim = true;
self
}
pub fn italic(mut self) -> Self {
self.italic = true;
self
}
pub fn underline(mut self) -> Self {
self.underline = true;
self
}
pub fn crossedout(mut self) -> Self {
self.crossedout = true;
self
}
pub fn reverse(mut self) -> Self {
self.reverse = true;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn style_builder_methods_set_their_fields() {
let s = Style::new()
.fg(Color::Red)
.bg(Color::Blue)
.bold()
.dim()
.italic()
.underline()
.crossedout()
.reverse();
assert_eq!(
s,
Style {
fg: Some(Color::Red),
bg: Some(Color::Blue),
bold: true,
dim: true,
italic: true,
underline: true,
crossedout: true,
reverse: true,
}
);
}
}