colours.rs 3.0 KB
Newer Older
B
Ben S 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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),
}

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 => ~"",
                    };
                    let bo: ~str = if bold { ~"1;" } else { ~"" };
                    let un: ~str = 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 })
    }
}