exa/colours.rs
Ben S a0582132e5 Have each row use the same column widths
This involves putting the entire output into a table before anything
is actually printed, in order to determine what the width of each
column should be. This should make it appear to output slower, as the
first line can only be printed after every file has been examined, but
it's still fast to me.
2014-05-22 13:22:41 +01:00

90 lines
3.1 KiB
Rust

pub enum Colour {
Black = 30, Red = 31, Green = 32, Yellow = 33, Blue = 34, Purple = 35, Cyan = 36, White = 37,
}
pub enum Style {
Plain,
Foreground(Colour),
Style(StyleStruct),
}
pub struct StyleStruct {
foreground: Colour,
background: Option<Colour>,
bold: bool,
underline: bool,
}
impl Style {
pub fn paint(&self, input: ~str) -> ~str {
match *self {
Plain => input,
Foreground(c) => c.paint(input),
Style(s) => match s {
StyleStruct { foreground, background, bold, underline } => {
let bg: ~str = match background {
Some(c) => format!("{};", c as int + 10),
None => "".to_owned(),
};
let bo = if bold { "1;" } else { "" };
let un = if underline { "4;" } else { "" };
format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input)
}
}
}
}
}
impl Style {
pub fn bold(&self) -> Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: None, bold: true, underline: false }),
Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: true, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: true, underline: false }),
}
}
pub fn underline(&self) -> Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: None, bold: false, underline: true }),
Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: false, underline: true }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: false, underline: true }),
}
}
pub fn on(&self, background: Colour) -> Style {
match *self {
Plain => Style(StyleStruct { foreground: White, background: Some(background), bold: false, underline: false }),
Foreground(c) => Style(StyleStruct { foreground: c, background: Some(background), bold: false, underline: false }),
Style(st) => Style(StyleStruct { foreground: st.foreground, background: Some(background), bold: false, underline: false }),
}
}
}
impl Colour {
pub fn paint(&self, input: &str) -> ~str {
format!("\x1B[{}m{}\x1B[0m", *self as int, input)
}
pub fn underline(&self) -> Style {
Style(StyleStruct { foreground: *self, background: None, bold: false, underline: true })
}
pub fn bold(&self) -> Style {
Style(StyleStruct { foreground: *self, background: None, bold: true, underline: false })
}
pub fn normal(&self) -> Style {
Style(StyleStruct { foreground: *self, background: None, bold: false, underline: false })
}
pub fn on(&self, background: Colour) -> Style {
Style(StyleStruct { foreground: *self, background: Some(background), bold: false, underline: false })
}
}
pub fn strip_formatting(input: &~str) -> ~str {
let re = regex!("\x1B\\[.+?m");
re.replace_all(*input, "").to_owned()
}