use crate::{Color, Format};
use std::fmt;
pub struct Txtly<T: fmt::Display> {
content: T,
fg_color: Option<Color>,
bg_color: Option<Color>,
format: Option<Format>,
}
impl<T: fmt::Display> Txtly<T> {
pub fn new(content: T) -> Self {
Txtly {
content,
fg_color: None,
bg_color: None,
format: None,
}
}
pub fn style(mut self, color: Color, format: Format) -> Self {
self.fg_color = Some(color);
self.format = Some(format);
self
}
pub fn bg(mut self, color: Color) -> Self {
self.bg_color = Some(color);
self
}
}
impl<T: fmt::Display> fmt::Display for Txtly<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fg_code = self.fg_color.map_or("", |c| c.fg_code());
let bg_code = self.bg_color.map_or("", |c| c.bg_code());
let format_code = self.format.map_or("", |f| f.code());
write!(
f,
"{}{}{}{}{}", fg_code,
bg_code,
format_code,
self.content,
"\x1b[0m" )
}
}