colours.rs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. pub 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) -> StrBuf {
  17. match *self {
  18. Plain => input.to_strbuf(),
  19. Foreground(c) => c.paint(input),
  20. Style(s) => match s {
  21. StyleStruct { foreground, background, bold, underline } => {
  22. let bg = match background {
  23. Some(c) => format!("{};", c as int + 10),
  24. None => "".to_strbuf()
  25. };
  26. let bo = if bold { "1;" } else { "" };
  27. let un = if underline { "4;" } else { "" };
  28. let re = format!("\x1B[{}{}{}{}m{}\x1B[0m", bo, un, bg, foreground as int, input.to_strbuf());
  29. return re.to_owned();
  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) -> StrBuf {
  60. let re = format!("\x1B[{}m{}\x1B[0m", *self as int, input);
  61. return re.to_owned();
  62. }
  63. pub fn underline(&self) -> Style {
  64. Style(StyleStruct { foreground: *self, background: None, bold: false, underline: true })
  65. }
  66. pub fn bold(&self) -> Style {
  67. Style(StyleStruct { foreground: *self, background: None, bold: true, underline: false })
  68. }
  69. pub fn normal(&self) -> Style {
  70. Style(StyleStruct { foreground: *self, background: None, bold: false, underline: false })
  71. }
  72. pub fn on(&self, background: Colour) -> Style {
  73. Style(StyleStruct { foreground: *self, background: Some(background), bold: false, underline: false })
  74. }
  75. }
  76. pub fn strip_formatting(input: &StrBuf) -> StrBuf {
  77. let re = regex!("\x1B\\[.+?m");
  78. re.replace_all(input.as_slice(), "").to_owned()
  79. }