use std::fmt;
use rawcmd_utils::{ANSI_PAIR};
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
text: String,
color: Option<usize>,
bg: Option<usize>,
style: Option<usize>,
}
impl Style {
pub fn new(txt: &str) -> Self {
Self {
text: txt.to_string(),
color: None,
bg: None,
style: None,
}
}
pub fn bold(mut self) -> Self {
self.style = Some(1);
self
}
pub fn dim(mut self) -> Self {
self.style = Some(2);
self
}
pub fn italic(mut self) -> Self {
self.style = Some(3);
self
}
pub fn underline(mut self) -> Self {
self.style = Some(4);
self
}
pub fn blink(mut self) -> Self {
self.style = Some(5);
self
}
pub fn inverse(mut self) -> Self {
self.style = Some(6);
self
}
pub fn hidden(mut self) -> Self {
self.style = Some(7);
self
}
pub fn black(mut self) -> Self {
self.color = Some(8);
self
}
pub fn red(mut self) -> Self {
self.color = Some(9);
self
}
pub fn green(mut self) -> Self {
self.color = Some(10);
self
}
pub fn yellow(mut self) -> Self {
self.color = Some(11);
self
}
pub fn blue(mut self) -> Self {
self.color = Some(12);
self
}
pub fn magenta(mut self) -> Self {
self.color = Some(13);
self
}
pub fn cyan(mut self) -> Self {
self.color = Some(14);
self
}
pub fn white(mut self) -> Self {
self.color = Some(15);
self
}
pub fn bgblack(mut self) -> Self {
self.bg = Some(16);
self
}
pub fn bgred(mut self) -> Self {
self.bg = Some(17);
self
}
pub fn bggreen(mut self) -> Self {
self.bg = Some(18);
self
}
pub fn bgyellow(mut self) -> Self {
self.bg = Some(19);
self
}
pub fn bgblue(mut self) -> Self {
self.bg = Some(20);
self
}
pub fn bgmagenta(mut self) -> Self {
self.bg = Some(21);
self
}
pub fn bgcyan(mut self) -> Self {
self.bg = Some(22);
self
}
pub fn bgwhite(mut self) -> Self {
self.bg = Some(23);
self
}
pub fn clean(mut self) -> Self {
self.color = None;
self.bg = None;
self.style = None;
self
}
}
impl fmt::Display for Style {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut txt = self.text.to_string();
if self.color.is_some() {
let index = self.color.unwrap();
txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
}
if self.bg.is_some() {
let index = self.bg.unwrap();
txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
}
if self.style.is_some() {
let index = self.style.unwrap();
txt = format!("{}{}{}", ANSI_PAIR[index][0], txt, ANSI_PAIR[index][1]);
}
write!(f, "{}", txt)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn applies_bold_style() {
assert_eq!(
Style::new("foo").bold().to_string(),
format!("{}{}{}", ANSI_PAIR[1][0], "foo", ANSI_PAIR[1][1]),
);
}
#[test]
fn applies_dim_style() {
assert_eq!(
Style::new("foo").dim().to_string(),
format!("{}{}{}", ANSI_PAIR[2][0], "foo", ANSI_PAIR[2][1]),
);
}
#[test]
fn applies_italic_style() {
assert_eq!(
Style::new("foo").italic().to_string(),
format!("{}{}{}", ANSI_PAIR[3][0], "foo", ANSI_PAIR[3][1]),
);
}
#[test]
fn applies_underline_style() {
assert_eq!(
Style::new("foo").underline().to_string(),
format!("{}{}{}", ANSI_PAIR[4][0], "foo", ANSI_PAIR[4][1]),
);
}
#[test]
fn applies_blink_style() {
assert_eq!(
Style::new("foo").blink().to_string(),
format!("{}{}{}", ANSI_PAIR[5][0], "foo", ANSI_PAIR[5][1]),
);
}
#[test]
fn applies_inverse_style() {
assert_eq!(
Style::new("foo").inverse().to_string(),
format!("{}{}{}", ANSI_PAIR[6][0], "foo", ANSI_PAIR[6][1]),
);
}
#[test]
fn applies_hidden_style() {
assert_eq!(
Style::new("foo").hidden().to_string(),
format!("{}{}{}", ANSI_PAIR[7][0], "foo", ANSI_PAIR[7][1]),
);
}
#[test]
fn applies_black_color() {
assert_eq!(
Style::new("foo").black().to_string(),
format!("{}{}{}", ANSI_PAIR[8][0], "foo", ANSI_PAIR[8][1]),
);
}
#[test]
fn applies_red_color() {
assert_eq!(
Style::new("foo").red().to_string(),
format!("{}{}{}", ANSI_PAIR[9][0], "foo", ANSI_PAIR[9][1]),
);
}
#[test]
fn applies_green_color() {
assert_eq!(
Style::new("foo").green().to_string(),
format!("{}{}{}", ANSI_PAIR[10][0], "foo", ANSI_PAIR[10][1]),
);
}
#[test]
fn applies_yellow_color() {
assert_eq!(
Style::new("foo").yellow().to_string(),
format!("{}{}{}", ANSI_PAIR[11][0], "foo", ANSI_PAIR[11][1]),
);
}
#[test]
fn applies_blue_color() {
assert_eq!(
Style::new("foo").blue().to_string(),
format!("{}{}{}", ANSI_PAIR[12][0], "foo", ANSI_PAIR[12][1]),
);
}
#[test]
fn applies_magenta_color() {
assert_eq!(
Style::new("foo").magenta().to_string(),
format!("{}{}{}", ANSI_PAIR[13][0], "foo", ANSI_PAIR[13][1]),
);
}
#[test]
fn applies_cyan_color() {
assert_eq!(
Style::new("foo").cyan().to_string(),
format!("{}{}{}", ANSI_PAIR[14][0], "foo", ANSI_PAIR[14][1]),
);
}
#[test]
fn applies_white_color() {
assert_eq!(
Style::new("foo").white().to_string(),
format!("{}{}{}", ANSI_PAIR[15][0], "foo", ANSI_PAIR[15][1]),
);
}
#[test]
fn applies_black_background() {
assert_eq!(
Style::new("foo").bgblack().to_string(),
format!("{}{}{}", ANSI_PAIR[16][0], "foo", ANSI_PAIR[16][1]),
);
}
#[test]
fn applies_red_background() {
assert_eq!(
Style::new("foo").bgred().to_string(),
format!("{}{}{}", ANSI_PAIR[17][0], "foo", ANSI_PAIR[17][1]),
);
}
#[test]
fn applies_green_background() {
assert_eq!(
Style::new("foo").bggreen().to_string(),
format!("{}{}{}", ANSI_PAIR[18][0], "foo", ANSI_PAIR[18][1]),
);
}
#[test]
fn applies_yellow_background() {
assert_eq!(
Style::new("foo").bgyellow().to_string(),
format!("{}{}{}", ANSI_PAIR[19][0], "foo", ANSI_PAIR[19][1]),
);
}
#[test]
fn applies_blue_background() {
assert_eq!(
Style::new("foo").bgblue().to_string(),
format!("{}{}{}", ANSI_PAIR[20][0], "foo", ANSI_PAIR[20][1]),
);
}
#[test]
fn applies_magenta_background() {
assert_eq!(
Style::new("foo").bgmagenta().to_string(),
format!("{}{}{}", ANSI_PAIR[21][0], "foo", ANSI_PAIR[21][1]),
);
}
#[test]
fn applies_cyan_background() {
assert_eq!(
Style::new("foo").bgcyan().to_string(),
format!("{}{}{}", ANSI_PAIR[22][0], "foo", ANSI_PAIR[22][1]),
);
}
#[test]
fn applies_white_background() {
assert_eq!(
Style::new("foo").bgwhite().to_string(),
format!("{}{}{}", ANSI_PAIR[23][0], "foo", ANSI_PAIR[23][1]),
);
}
#[test]
fn applyes_color_background_and_style() {
assert_eq!(
Style::new("foo").blink().red().bgwhite().to_string(),
format!("{}{}{}{}{}{}{}", ANSI_PAIR[5][0], ANSI_PAIR[23][0], ANSI_PAIR[9][0], "foo", ANSI_PAIR[9][1], ANSI_PAIR[23][1], ANSI_PAIR[5][1]),
);
}
#[test]
fn cleans_styles() {
assert_eq!(
Style::new("foo").red().clean().to_string(),
"foo",
);
}
}