use glyphs::{style, Color};
use lacquer::{Border, Position, Style as LacquerStyle};
pub fn style_text(text: impl Into<String>) -> StyleCmd {
StyleCmd::new(text)
}
pub struct StyleCmd {
text: String,
foreground: Option<Color>,
background: Option<Color>,
bold: bool,
italic: bool,
underline: bool,
strikethrough: bool,
dim: bool,
padding: Option<(u16, u16)>,
margin: Option<(u16, u16)>,
border: Option<Border>,
width: Option<u16>,
align: Position,
}
impl StyleCmd {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
foreground: None,
background: None,
bold: false,
italic: false,
underline: false,
strikethrough: false,
dim: false,
padding: None,
margin: None,
border: None,
width: None,
align: Position::Start,
}
}
#[must_use]
pub fn foreground(mut self, color: Color) -> Self {
self.foreground = Some(color);
self
}
#[must_use]
pub fn foreground_hex(self, hex: &str) -> Self {
self.foreground(Color::from_hex(hex))
}
#[must_use]
pub fn background(mut self, color: Color) -> Self {
self.background = Some(color);
self
}
#[must_use]
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
#[must_use]
pub fn italic(mut self) -> Self {
self.italic = true;
self
}
#[must_use]
pub fn underline(mut self) -> Self {
self.underline = true;
self
}
#[must_use]
pub fn strikethrough(mut self) -> Self {
self.strikethrough = true;
self
}
#[must_use]
pub fn dim(mut self) -> Self {
self.dim = true;
self
}
#[must_use]
pub fn padding(mut self, vertical: u16, horizontal: u16) -> Self {
self.padding = Some((vertical, horizontal));
self
}
#[must_use]
pub fn margin(mut self, vertical: u16, horizontal: u16) -> Self {
self.margin = Some((vertical, horizontal));
self
}
#[must_use]
pub fn border(mut self, border: Border) -> Self {
self.border = Some(border);
self
}
#[must_use]
pub fn width(mut self, width: u16) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub fn align(mut self, align: Position) -> Self {
self.align = align;
self
}
#[must_use]
pub fn center(self) -> Self {
self.align(Position::Center)
}
#[must_use]
pub fn render(self) -> String {
if self.padding.is_some() || self.margin.is_some() || self.border.is_some() || self.width.is_some() {
let mut style = LacquerStyle::new();
if let Some((v, h)) = self.padding {
style = style.padding(v, h);
}
if let Some((v, h)) = self.margin {
style = style.margin(v, h);
}
if let Some(border) = self.border {
style = style.border(border);
}
if let Some(width) = self.width {
style = style.width(width);
}
style = style.align(self.align);
if let Some(fg) = self.foreground {
style = style.foreground(fg);
}
if let Some(bg) = self.background {
style = style.background(bg);
}
if self.bold {
style = style.bold();
}
if self.italic {
style = style.italic();
}
if self.underline {
style = style.underline();
}
if self.strikethrough {
style = style.strikethrough();
}
if self.dim {
style = style.dim();
}
style.render(&self.text)
} else {
let mut styled = style(&self.text);
if let Some(fg) = self.foreground {
styled = styled.fg(fg);
}
if let Some(bg) = self.background {
styled = styled.bg(bg);
}
if self.bold {
styled = styled.bold();
}
if self.italic {
styled = styled.italic();
}
if self.underline {
styled = styled.underline();
}
if self.strikethrough {
styled = styled.strikethrough();
}
if self.dim {
styled = styled.dim();
}
styled.to_string()
}
}
pub fn print(self) {
println!("{}", self.render());
}
}