#![crate_name = "ansi_term"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![desc = "A rust library for ANSI terminal colours and styles (bold, underline)"]
#![feature(phase)] extern crate regex;
#[phase(plugin)] extern crate regex_macros;
use Colour::{Black, Red, Green, Yellow, Blue, Purple, Cyan, White, Fixed};
use Style::{Plain, Foreground, Styled};
use std::str::SendStr;
pub trait Paint {
fn paint(&self, input: &str) -> String;
fn underline(&self) -> Style;
fn bold(&self) -> Style;
fn on(&self, background: Colour) -> Style;
}
pub enum Colour {
Black, Red, Green, Yellow, Blue, Purple, Cyan, White, Fixed(u8),
}
impl Colour {
fn foreground_code(&self) -> SendStr {
match *self {
Black => "30".into_maybe_owned(),
Red => "31".into_maybe_owned(),
Green => "32".into_maybe_owned(),
Yellow => "33".into_maybe_owned(),
Blue => "34".into_maybe_owned(),
Purple => "35".into_maybe_owned(),
Cyan => "36".into_maybe_owned(),
White => "37".into_maybe_owned(),
Fixed(num) => format!("38;5;{}", num).into_maybe_owned(),
}
}
fn background_code(&self) -> SendStr {
match *self {
Black => "40".into_maybe_owned(),
Red => "41".into_maybe_owned(),
Green => "42".into_maybe_owned(),
Yellow => "43".into_maybe_owned(),
Blue => "44".into_maybe_owned(),
Purple => "45".into_maybe_owned(),
Cyan => "46".into_maybe_owned(),
White => "47".into_maybe_owned(),
Fixed(num) => format!("48;5;{}", num).into_maybe_owned(),
}
}
pub fn normal(&self) -> Style {
Styled { foreground: *self, background: None, bold: false, underline: false }
}
}
impl Paint for Colour {
fn paint(&self, input: &str) -> String {
let re = format!("\x1B[{}m{}\x1B[0m", self.foreground_code(), input);
return re.to_string();
}
fn underline(&self) -> Style {
Styled { foreground: *self, background: None, bold: false, underline: true }
}
fn bold(&self) -> Style {
Styled { foreground: *self, background: None, bold: true, underline: false }
}
fn on(&self, background: Colour) -> Style {
Styled { foreground: *self, background: Some(background), bold: false, underline: false }
}
}
pub enum Style {
Plain,
Foreground(Colour),
Styled { foreground: Colour, background: Option<Colour>, bold: bool, underline: bool, },
}
impl Paint for Style {
fn paint(&self, input: &str) -> String {
match *self {
Plain => input.to_string(),
Foreground(c) => c.paint(input),
Styled { foreground, background, bold, underline } => {
let bg = match background {
Some(c) => format!("{};", c.background_code()).into_maybe_owned(),
None => "".into_maybe_owned(),
};
let bo = if bold { "1;" } else { "" };
let un = if underline { "4;" } else { "" };
let painted = format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground.foreground_code(), input.to_string());
return painted.to_string();
}
}
}
fn bold(&self) -> Style {
match *self {
Plain => Styled { foreground: White, background: None, bold: true, underline: false },
Foreground(c) => Styled { foreground: c, background: None, bold: true, underline: false },
Styled { foreground, background, bold: _, underline } => Styled { foreground: foreground, background: background, bold: true, underline: underline },
}
}
fn underline(&self) -> Style {
match *self {
Plain => Styled { foreground: White, background: None, bold: false, underline: true },
Foreground(c) => Styled { foreground: c, background: None, bold: false, underline: true },
Styled { foreground, background, bold, underline: _ } => Styled { foreground: foreground, background: background, bold: bold, underline: true },
}
}
fn on(&self, background: Colour) -> Style {
match *self {
Plain => Styled { foreground: White,background: Some(background), bold: false, underline: false },
Foreground(c) => Styled { foreground: c, background: Some(background), bold: false, underline: false },
Styled { foreground, background: _, bold, underline } => Styled { foreground: foreground, background: Some(background), bold: bold, underline: underline },
}
}
}
pub fn strip_formatting(input: String) -> String {
let re = regex!("\x1B\\[.+?m");
re.replace_all(input.as_slice(), "").to_string()
}
#[cfg(test)]
mod tests {
use super::strip_formatting;
use super::Style::Plain;
use super::Colour::{Black, Red, Green, Yellow, Blue, Purple, Cyan, White, Fixed};
use super::Paint;
#[test]
fn test_plain() {
let hi = Plain.paint("text/plain");
assert!(hi == "text/plain".to_string());
}
#[test]
fn test_red() {
let hi = Red.paint("hi");
assert!(hi == "\x1B[31mhi\x1B[0m".to_string());
}
#[test]
fn test_black() {
let hi = Black.normal().paint("hi");
assert!(hi == "\x1B[30mhi\x1B[0m".to_string());
}
#[test]
fn test_yellow_bold() {
let hi = Yellow.bold().paint("hi");
assert!(hi == "\x1B[1;33mhi\x1B[0m".to_string());
}
#[test]
fn test_yellow_bold_2() {
let hi = Yellow.normal().bold().paint("hi");
assert!(hi == "\x1B[1;33mhi\x1B[0m".to_string());
}
#[test]
fn test_blue_underline() {
let hi = Blue.underline().paint("hi");
assert!(hi == "\x1B[4;34mhi\x1B[0m".to_string());
}
#[test]
fn test_green_bold_underline() {
let hi = Green.bold().underline().paint("hi");
assert!(hi == "\x1B[1;4;32mhi\x1B[0m".to_string());
}
#[test]
fn test_green_bold_underline_2() {
let hi = Green.underline().bold().paint("hi");
assert!(hi == "\x1B[1;4;32mhi\x1B[0m".to_string());
}
#[test]
fn test_purple_on_white() {
let hi = Purple.on(White).paint("hi");
assert!(hi == "\x1B[47;35mhi\x1B[0m".to_string());
}
#[test]
fn test_purple_on_white_2() {
let hi = Purple.normal().on(White).paint("hi");
assert!(hi == "\x1B[47;35mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_bold_on_white() {
let hi = Cyan.bold().on(White).paint("hi");
assert!(hi == "\x1B[1;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_underline_on_white() {
let hi = Cyan.underline().on(White).paint("hi");
assert!(hi == "\x1B[4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_bold_underline_on_white() {
let hi = Cyan.bold().underline().on(White).paint("hi");
assert!(hi == "\x1B[1;4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_cyan_underline_bold_on_white() {
let hi = Cyan.underline().bold().on(White).paint("hi");
assert!(hi == "\x1B[1;4;47;36mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed() {
let hi = Fixed(100).paint("hi");
assert!(hi == "\x1B[38;5;100mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed_on_purple() {
let hi = Fixed(100).on(Purple).paint("hi");
assert!(hi == "\x1B[45;38;5;100mhi\x1B[0m".to_string());
}
#[test]
fn test_fixed_on_fixed() {
let hi = Fixed(100).on(Fixed(200)).paint("hi");
assert!(hi == "\x1B[48;5;200;38;5;100mhi\x1B[0m".to_string());
}
#[test]
fn test_strip_formatting() {
let hi = strip_formatting(Blue.paint("hi"));
assert!(hi == "hi".to_string());
}
#[test]
fn test_strip_formatting_2() {
let hi = strip_formatting(Blue.on(Fixed(230)).bold().paint("hi"));
assert!(hi == "hi".to_string());
}
}