use std::fmt;
use crate::{Color, Style};
pub struct StyledText<T> {
pub text: T,
pub fg: Option<Color>,
pub bg: Option<Color>,
pub styles: Vec<Style>,
}
impl<T> StyledText<T> {
pub fn new(text: T) -> Self {
Self {
text,
fg: None,
bg: None,
styles: Vec::new(),
}
}
pub fn color(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 fixed(mut self, n: u8) -> Self {
self.fg = Some(Color::Fixed(n));
self
}
pub fn rgb(mut self, r: u8, g: u8, b: u8) -> Self {
self.fg = Some(Color::Rgb(r, g, b));
self
}
pub fn bg_fixed(mut self, n: u8) -> Self {
self.bg = Some(Color::Fixed(n));
self
}
pub fn bg_rgb(mut self, r: u8, g: u8, b: u8) -> Self {
self.bg = Some(Color::Rgb(r, g, b));
self
}
pub fn reset(mut self) -> Self {
self.fg = Some(Color::Reset);
self.bg = Some(Color::Reset);
self.styles.clear();
self
}
pub fn black(mut self) -> Self {
self.fg = Some(Color::Black);
self
}
pub fn bright_black(mut self) -> Self {
self.fg = Some(Color::BrightBlack);
self
}
pub fn bright_red(mut self) -> Self {
self.fg = Some(Color::BrightRed);
self
}
pub fn bright_green(mut self) -> Self {
self.fg = Some(Color::BrightGreen);
self
}
pub fn bright_yellow(mut self) -> Self {
self.fg = Some(Color::BrightYellow);
self
}
pub fn bright_blue(mut self) -> Self {
self.fg = Some(Color::BrightBlue);
self
}
pub fn bright_magenta(mut self) -> Self {
self.fg = Some(Color::BrightMagenta);
self
}
pub fn bright_cyan(mut self) -> Self {
self.fg = Some(Color::BrightCyan);
self
}
pub fn bright_white(mut self) -> Self {
self.fg = Some(Color::BrightWhite);
self
}
pub fn red(mut self) -> Self {
self.fg = Some(Color::Red);
self
}
pub fn green(mut self) -> Self {
self.fg = Some(Color::Green);
self
}
pub fn yellow(mut self) -> Self {
self.fg = Some(Color::Yellow);
self
}
pub fn blue(mut self) -> Self {
self.fg = Some(Color::Blue);
self
}
pub fn magenta(mut self) -> Self {
self.fg = Some(Color::Magenta);
self
}
pub fn cyan(mut self) -> Self {
self.fg = Some(Color::Cyan);
self
}
pub fn white(mut self) -> Self {
self.fg = Some(Color::White);
self
}
pub fn bg_black(mut self) -> Self {
self.bg = Some(Color::Black);
self
}
pub fn bg_bright_black(mut self) -> Self {
self.bg = Some(Color::BrightBlack);
self
}
pub fn bg_bright_red(mut self) -> Self {
self.bg = Some(Color::BrightRed);
self
}
pub fn bg_bright_green(mut self) -> Self {
self.bg = Some(Color::BrightGreen);
self
}
pub fn bg_bright_yellow(mut self) -> Self {
self.bg = Some(Color::BrightYellow);
self
}
pub fn bg_bright_blue(mut self) -> Self {
self.bg = Some(Color::BrightBlue);
self
}
pub fn bg_bright_magenta(mut self) -> Self {
self.bg = Some(Color::BrightMagenta);
self
}
pub fn bg_bright_cyan(mut self) -> Self {
self.bg = Some(Color::BrightCyan);
self
}
pub fn bg_bright_white(mut self) -> Self {
self.bg = Some(Color::BrightWhite);
self
}
pub fn bg_red(mut self) -> Self {
self.bg = Some(Color::Red);
self
}
pub fn bg_green(mut self) -> Self {
self.bg = Some(Color::Green);
self
}
pub fn bg_yellow(mut self) -> Self {
self.bg = Some(Color::Yellow);
self
}
pub fn bg_blue(mut self) -> Self {
self.bg = Some(Color::Blue);
self
}
pub fn bg_magenta(mut self) -> Self {
self.bg = Some(Color::Magenta);
self
}
pub fn bg_cyan(mut self) -> Self {
self.bg = Some(Color::Cyan);
self
}
pub fn bg_white(mut self) -> Self {
self.bg = Some(Color::White);
self
}
pub fn underline(mut self) -> Self {
self.styles.push(Style::Underline);
self
}
pub fn dim(mut self) -> Self {
self.styles.push(Style::Dim);
self
}
pub fn italic(mut self) -> Self {
self.styles.push(Style::Italic);
self
}
pub fn blink(mut self) -> Self {
self.styles.push(Style::Blink);
self
}
pub fn reverse(mut self) -> Self {
self.styles.push(Style::Reverse);
self
}
pub fn hidden(mut self) -> Self {
self.styles.push(Style::Hidden);
self
}
pub fn strikethrough(mut self) -> Self {
self.styles.push(Style::Strikethrough);
self
}
pub fn bold(mut self) -> Self {
self.styles.push(Style::Bold);
self
}
}
impl<T: fmt::Display> fmt::Display for StyledText<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for style in &self.styles {
write!(f, "{}", style.to_str())?;
}
if let Some(fg) = &self.fg {
write!(f, "{}", fg.to_fg_str())?;
}
if let Some(bg) = &self.bg {
write!(f, "{}", bg.to_bg_str())?;
}
write!(f, "{}", self.text)?;
write!(f, "{}", Style::Reset.to_str())
}
}