use ratatui::style::{Color as RColor, Modifier, Style};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Attributes {
pub bold: bool,
pub italic: bool,
pub underline: bool,
pub strikethrough: bool,
pub reversed: bool,
}
impl Attributes {
pub fn new() -> Self {
Self::default()
}
pub fn with_bold(mut self) -> Self {
self.bold = true;
self
}
pub fn with_italic(mut self) -> Self {
self.italic = true;
self
}
pub fn with_underline(mut self) -> Self {
self.underline = true;
self
}
pub fn to_modifier(&self) -> Modifier {
let mut modifier = Modifier::empty();
if self.bold {
modifier |= Modifier::BOLD;
}
if self.italic {
modifier |= Modifier::ITALIC;
}
if self.underline {
modifier |= Modifier::UNDERLINED;
}
if self.strikethrough {
modifier |= Modifier::CROSSED_OUT;
}
if self.reversed {
modifier |= Modifier::REVERSED;
}
modifier
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
Indexed(u8),
Rgb(u8, u8, u8),
#[default]
Default,
}
impl Color {
pub fn to_ratatui(&self) -> RColor {
match self {
Color::Black => RColor::Black,
Color::Red => RColor::Red,
Color::Green => RColor::Green,
Color::Yellow => RColor::Yellow,
Color::Blue => RColor::Blue,
Color::Magenta => RColor::Magenta,
Color::Cyan => RColor::Cyan,
Color::White => RColor::White,
Color::Indexed(n) => RColor::Indexed(*n),
Color::Rgb(r, g, b) => RColor::Rgb(*r, *g, *b),
Color::Default => RColor::Reset,
}
}
pub fn to_style(&self, bg: Option<Color>) -> Style {
let fg = self.to_ratatui();
match bg {
Some(bg_color) => Style::new().fg(fg).bg(bg_color.to_ratatui()),
None => Style::new().fg(fg),
}
}
}