colours.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. pub enum Colour {
  2. Black = 30, Red = 31, Green = 32, Yellow = 33, Blue = 34, Purple = 35, Cyan = 36, White = 37,
  3. }
  4. pub enum Style {
  5. Plain,
  6. Foreground(Colour),
  7. Style(StyleStruct),
  8. }
  9. struct StyleStruct {
  10. foreground: Colour,
  11. background: Option<Colour>,
  12. bold: bool,
  13. underline: bool,
  14. }
  15. impl Style {
  16. pub fn paint(&self, input: ~str) -> ~str {
  17. match *self {
  18. Plain => input,
  19. Foreground(c) => c.paint(input),
  20. Style(s) => match s {
  21. StyleStruct { foreground, background, bold, underline } => {
  22. let bg: ~str = match background {
  23. Some(c) => format!("{};", c as int + 10),
  24. None => ~"",
  25. };
  26. let bo: ~str = if bold { ~"1;" } else { ~"" };
  27. let un: ~str = if underline { ~"4;" } else { ~"" };
  28. format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input)
  29. }
  30. }
  31. }
  32. }
  33. }
  34. impl Style {
  35. pub fn bold(&self) -> Style {
  36. match *self {
  37. Plain => Style(StyleStruct { foreground: White, background: None, bold: true, underline: false }),
  38. Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: true, underline: false }),
  39. Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: true, underline: false }),
  40. }
  41. }
  42. pub fn underline(&self) -> Style {
  43. match *self {
  44. Plain => Style(StyleStruct { foreground: White, background: None, bold: false, underline: true }),
  45. Foreground(c) => Style(StyleStruct { foreground: c, background: None, bold: false, underline: true }),
  46. Style(st) => Style(StyleStruct { foreground: st.foreground, background: st.background, bold: false, underline: true }),
  47. }
  48. }
  49. pub fn on(&self, background: Colour) -> Style {
  50. match *self {
  51. Plain => Style(StyleStruct { foreground: White, background: Some(background), bold: false, underline: false }),
  52. Foreground(c) => Style(StyleStruct { foreground: c, background: Some(background), bold: false, underline: false }),
  53. Style(st) => Style(StyleStruct { foreground: st.foreground, background: Some(background), bold: false, underline: false }),
  54. }
  55. }
  56. }
  57. impl Colour {
  58. pub fn paint(&self, input: &str) -> ~str {
  59. format!("\x1B[{}m{}\x1B[0m", *self as int, input)
  60. }
  61. pub fn underline(&self) -> Style {
  62. Style(StyleStruct { foreground: *self, background: None, bold: false, underline: true })
  63. }
  64. pub fn bold(&self) -> Style {
  65. Style(StyleStruct { foreground: *self, background: None, bold: true, underline: false })
  66. }
  67. pub fn normal(&self) -> Style {
  68. Style(StyleStruct { foreground: *self, background: None, bold: false, underline: false })
  69. }
  70. pub fn on(&self, background: Colour) -> Style {
  71. Style(StyleStruct { foreground: *self, background: Some(background), bold: false, underline: false })
  72. }
  73. }