extern crate ansi_term;
use self::ansi_term::Style;
use self::ansi_term::{Colour,ANSIString};
use self::ansi_term::Colour::{Red};
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Purple,
Cyan,
White,
}
pub trait Styled {
fn bold(&self) -> ansi_term::ANSIString;
fn underline(&self) -> ansi_term::ANSIString;
fn paint(&self, color: Color) -> ansi_term::ANSIString;
}
impl<T: AsRef<str>> Styled for T {
fn bold(&self) -> ansi_term::ANSIString {
Style::default().bold().paint(&self.as_ref())
}
fn underline(&self) -> ansi_term::ANSIString {
Style::default().underline().paint(&self.as_ref())
}
fn paint(&self, color: Color) -> ansi_term::ANSIString {
match color {
Color::Black => { Colour::Black.paint(&self.as_ref()) },
Color::Red => { Colour::Red.paint(&self.as_ref()) },
Color::Green => { Colour::Green.paint(&self.as_ref()) },
Color::Yellow => { Colour::Yellow.paint(&self.as_ref()) },
Color::Blue => { Colour::Blue.paint(&self.as_ref()) },
Color::Purple => { Colour::Purple.paint(&self.as_ref()) },
Color::Cyan => { Colour::Cyan.paint(&self.as_ref()) },
Color::White => { Colour::White.paint(&self.as_ref()) },
}
}
}
#[test]
fn test_styled_bold() {
assert_eq!(String::from("Styled string test").bold().to_string(),
Style::default().bold().paint("Styled string test").to_string());
}
#[test]
fn test_styled_underline() {
assert_eq!(String::from("Styled string test").underline().to_string(),
Style::default().underline().paint("Styled string test").to_string());
}
#[test]
fn test_styled_paint() {
assert_eq!(String::from("Styled string test").paint(Color::Yellow).to_string(),
Colour::Yellow.paint("Styled string test").to_string());
}