lsc.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. use std::iter::Peekable;
  2. use std::ops::FnMut;
  3. use nu_ansi_term::Color::*;
  4. use nu_ansi_term::{Color as Colour, Style};
  5. // Parsing the LS_COLORS environment variable into a map of names to Style values.
  6. //
  7. // This is sitting around undocumented at the moment because it’s a feature
  8. // that should really be unnecessary! exa highlights its output by creating a
  9. // theme of one Style value per part of the interface that can be coloured,
  10. // then reading styles from that theme. The LS_COLORS variable, on the other
  11. // hand, can contain arbitrary characters that ls is supposed to add to the
  12. // output, without needing to know what they actually do. This puts exa in the
  13. // annoying position of having to parse the ANSI escape codes _back_ into
  14. // Style values before it’s able to use them. Doing this has a lot of
  15. // downsides: if a new terminal feature is added with its own code, exa won’t
  16. // be able to use this without explicit support for parsing the feature, while
  17. // ls would not even need to know it existed. And there are some edge cases in
  18. // ANSI codes, where terminals would accept codes exa is strict about it. It’s
  19. // just not worth doing, and there should really be a way to just use slices
  20. // of the LS_COLORS string without having to parse them.
  21. pub struct LSColors<'var>(pub &'var str);
  22. impl<'var> LSColors<'var> {
  23. pub fn each_pair<C>(&mut self, mut callback: C)
  24. where
  25. C: FnMut(Pair<'var>),
  26. {
  27. for next in self.0.split(':') {
  28. let bits = next.split('=').take(3).collect::<Vec<_>>();
  29. if bits.len() == 2 && !bits[0].is_empty() && !bits[1].is_empty() {
  30. callback(Pair {
  31. key: bits[0],
  32. value: bits[1],
  33. });
  34. }
  35. }
  36. }
  37. }
  38. fn parse_into_high_colour<'a, I>(iter: &mut Peekable<I>) -> Option<Colour>
  39. where
  40. I: Iterator<Item = &'a str>,
  41. {
  42. match iter.peek() {
  43. Some(&"5") => {
  44. let _ = iter.next();
  45. if let Some(byte) = iter.next() {
  46. if let Ok(num) = byte.parse() {
  47. return Some(Fixed(num));
  48. }
  49. }
  50. }
  51. Some(&"2") => {
  52. let _ = iter.next();
  53. if let Some(hexes) = iter.next() {
  54. // Some terminals support R:G:B instead of R;G;B
  55. // but this clashes with splitting on ‘:’ in each_pair above.
  56. /*if hexes.contains(':') {
  57. let rgb = hexes.splitn(3, ':').collect::<Vec<_>>();
  58. if rgb.len() != 3 {
  59. return None;
  60. }
  61. else if let (Ok(r), Ok(g), Ok(b)) = (rgb[0].parse(), rgb[1].parse(), rgb[2].parse()) {
  62. return Some(RGB(r, g, b));
  63. }
  64. }*/
  65. if let (Some(r), Some(g), Some(b)) = (
  66. hexes.parse().ok(),
  67. iter.next().and_then(|s| s.parse().ok()),
  68. iter.next().and_then(|s| s.parse().ok()),
  69. ) {
  70. return Some(Rgb(r, g, b));
  71. }
  72. }
  73. }
  74. _ => {}
  75. }
  76. None
  77. }
  78. pub struct Pair<'var> {
  79. pub key: &'var str,
  80. pub value: &'var str,
  81. }
  82. impl<'var> Pair<'var> {
  83. pub fn to_style(&self) -> Style {
  84. let mut style = Style::default();
  85. let mut iter = self.value.split(';').peekable();
  86. while let Some(num) = iter.next() {
  87. match num.trim_start_matches('0') {
  88. // Bold and italic
  89. "1" => style = style.bold(),
  90. "2" => style = style.dimmed(),
  91. "3" => style = style.italic(),
  92. "4" => style = style.underline(),
  93. "5" => style = style.blink(),
  94. // 6 is supposedly a faster blink
  95. "7" => style = style.reverse(),
  96. "8" => style = style.hidden(),
  97. "9" => style = style.strikethrough(),
  98. // Foreground colours
  99. "30" => style = style.fg(Black),
  100. "31" => style = style.fg(Red),
  101. "32" => style = style.fg(Green),
  102. "33" => style = style.fg(Yellow),
  103. "34" => style = style.fg(Blue),
  104. "35" => style = style.fg(Purple),
  105. "36" => style = style.fg(Cyan),
  106. "37" => style = style.fg(White),
  107. // Bright foreground colours
  108. "90" => style = style.fg(DarkGray),
  109. "91" => style = style.fg(LightRed),
  110. "92" => style = style.fg(LightGreen),
  111. "93" => style = style.fg(LightYellow),
  112. "94" => style = style.fg(LightBlue),
  113. "95" => style = style.fg(LightPurple),
  114. "96" => style = style.fg(LightCyan),
  115. "97" => style = style.fg(LightGray),
  116. "38" => {
  117. if let Some(c) = parse_into_high_colour(&mut iter) {
  118. style = style.fg(c);
  119. }
  120. }
  121. // Background colours
  122. "40" => style = style.on(Black),
  123. "41" => style = style.on(Red),
  124. "42" => style = style.on(Green),
  125. "43" => style = style.on(Yellow),
  126. "44" => style = style.on(Blue),
  127. "45" => style = style.on(Purple),
  128. "46" => style = style.on(Cyan),
  129. "47" => style = style.on(White),
  130. // Bright background colours
  131. "100" => style = style.on(DarkGray),
  132. "101" => style = style.on(LightRed),
  133. "102" => style = style.on(LightGreen),
  134. "103" => style = style.on(LightYellow),
  135. "104" => style = style.on(LightBlue),
  136. "105" => style = style.on(LightPurple),
  137. "106" => style = style.on(LightCyan),
  138. "107" => style = style.on(LightGray),
  139. "48" => {
  140. if let Some(c) = parse_into_high_colour(&mut iter) {
  141. style = style.on(c);
  142. }
  143. }
  144. _ => { /* ignore the error and do nothing */ }
  145. }
  146. }
  147. style
  148. }
  149. }
  150. #[cfg(test)]
  151. mod ansi_test {
  152. use super::*;
  153. use nu_ansi_term::Style;
  154. macro_rules! test {
  155. ($name:ident: $input:expr => $result:expr) => {
  156. #[test]
  157. fn $name() {
  158. assert_eq!(
  159. Pair {
  160. key: "",
  161. value: $input
  162. }
  163. .to_style(),
  164. $result
  165. );
  166. }
  167. };
  168. }
  169. // Styles
  170. test!(bold: "1" => Style::default().bold());
  171. test!(bold2: "01" => Style::default().bold());
  172. test!(under: "4" => Style::default().underline());
  173. test!(unde2: "04" => Style::default().underline());
  174. test!(both: "1;4" => Style::default().bold().underline());
  175. test!(both2: "01;04" => Style::default().bold().underline());
  176. test!(fg: "31" => Red.normal());
  177. test!(bg: "43" => Style::default().on(Yellow));
  178. test!(bfg: "31;43" => Red.on(Yellow));
  179. test!(bfg2: "0031;0043" => Red.on(Yellow));
  180. test!(all: "43;31;1;4" => Red.on(Yellow).bold().underline());
  181. test!(again: "1;1;1;1;1" => Style::default().bold());
  182. // Failure cases
  183. test!(empty: "" => Style::default());
  184. test!(semis: ";;;;;;" => Style::default());
  185. test!(nines: "99999999" => Style::default());
  186. test!(word: "GREEN" => Style::default());
  187. // Higher colours
  188. test!(hifg: "38;5;149" => Fixed(149).normal());
  189. test!(hibg: "48;5;1" => Style::default().on(Fixed(1)));
  190. test!(hibo: "48;5;1;1" => Style::default().on(Fixed(1)).bold());
  191. test!(hiund: "4;48;5;1" => Style::default().on(Fixed(1)).underline());
  192. test!(rgb: "38;2;255;100;0" => Style::default().fg(Rgb(255, 100, 0)));
  193. test!(rgbi: "38;2;255;100;0;3" => Style::default().fg(Rgb(255, 100, 0)).italic());
  194. test!(rgbbg: "48;2;255;100;0" => Style::default().on(Rgb(255, 100, 0)));
  195. test!(rgbbi: "48;2;255;100;0;3" => Style::default().on(Rgb(255, 100, 0)).italic());
  196. test!(fgbg: "38;5;121;48;5;212" => Fixed(121).on(Fixed(212)));
  197. test!(bgfg: "48;5;121;38;5;212" => Fixed(212).on(Fixed(121)));
  198. test!(toohi: "48;5;999" => Style::default());
  199. }
  200. #[cfg(test)]
  201. mod test {
  202. use super::*;
  203. macro_rules! test {
  204. ($name:ident: $input:expr => $result:expr) => {
  205. #[test]
  206. fn $name() {
  207. let mut lscs = Vec::new();
  208. LSColors($input).each_pair(|p| lscs.push((p.key.clone(), p.to_style())));
  209. assert_eq!(lscs, $result.to_vec());
  210. }
  211. };
  212. }
  213. // Bad parses
  214. test!(empty: "" => []);
  215. test!(jibber: "blah" => []);
  216. test!(equals: "=" => []);
  217. test!(starts: "=di" => []);
  218. test!(ends: "id=" => []);
  219. // Foreground colours
  220. test!(green: "cb=32" => [ ("cb", Green.normal()) ]);
  221. test!(red: "di=31" => [ ("di", Red.normal()) ]);
  222. test!(blue: "la=34" => [ ("la", Blue.normal()) ]);
  223. // Background colours
  224. test!(yellow: "do=43" => [ ("do", Style::default().on(Yellow)) ]);
  225. test!(purple: "re=45" => [ ("re", Style::default().on(Purple)) ]);
  226. test!(cyan: "mi=46" => [ ("mi", Style::default().on(Cyan)) ]);
  227. // Bold and underline
  228. test!(bold: "fa=1" => [ ("fa", Style::default().bold()) ]);
  229. test!(under: "so=4" => [ ("so", Style::default().underline()) ]);
  230. test!(both: "la=1;4" => [ ("la", Style::default().bold().underline()) ]);
  231. // More and many
  232. test!(more: "me=43;21;55;34:yu=1;4;1" => [ ("me", Blue.on(Yellow)), ("yu", Style::default().bold().underline()) ]);
  233. test!(many: "red=31:green=32:blue=34" => [ ("red", Red.normal()), ("green", Green.normal()), ("blue", Blue.normal()) ]);
  234. }