colours.rs 3.0 KB

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