mod.rs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. use ansi_term::Style;
  2. use crate::fs::File;
  3. use crate::output::file_name::Colours as FileNameColours;
  4. use crate::output::render;
  5. mod ui_styles;
  6. pub use self::ui_styles::UiStyles;
  7. pub use self::ui_styles::Size as SizeColours;
  8. mod lsc;
  9. pub use self::lsc::LSColors;
  10. mod default_theme;
  11. #[derive(PartialEq, Eq, Debug)]
  12. pub struct Options {
  13. pub use_colours: UseColours,
  14. pub colour_scale: ColourScale,
  15. pub definitions: Definitions,
  16. }
  17. /// Under what circumstances we should display coloured, rather than plain,
  18. /// output to the terminal.
  19. ///
  20. /// By default, we want to display the colours when stdout can display them.
  21. /// Turning them on when output is going to, say, a pipe, would make programs
  22. /// such as `grep` or `more` not work properly. So the `Automatic` mode does
  23. /// this check and only displays colours when they can be truly appreciated.
  24. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  25. pub enum UseColours {
  26. /// Display them even when output isn’t going to a terminal.
  27. Always,
  28. /// Display them when output is going to a terminal, but not otherwise.
  29. Automatic,
  30. /// Never display them, even when output is going to a terminal.
  31. Never,
  32. }
  33. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  34. pub enum ColourScale {
  35. Fixed,
  36. Gradient,
  37. }
  38. #[derive(PartialEq, Eq, Debug, Default)]
  39. pub struct Definitions {
  40. pub ls: Option<String>,
  41. pub exa: Option<String>,
  42. }
  43. pub struct Theme {
  44. pub ui: UiStyles,
  45. pub exts: Box<dyn FileColours>,
  46. }
  47. impl Options {
  48. #[allow(trivial_casts)] // the `as Box<_>` stuff below warns about this for some reason
  49. pub fn to_theme(&self, isatty: bool) -> Theme {
  50. use crate::info::filetype::FileExtensions;
  51. if self.use_colours == UseColours::Never || (self.use_colours == UseColours::Automatic && ! isatty) {
  52. let ui = UiStyles::plain();
  53. let exts = Box::new(NoFileColours);
  54. return Theme { ui, exts };
  55. }
  56. // Parse the environment variables into colours and extension mappings
  57. let mut ui = UiStyles::default_theme(self.colour_scale);
  58. let (exts, use_default_filetypes) = self.definitions.parse_color_vars(&mut ui);
  59. // Use between 0 and 2 file name highlighters
  60. let exts = match (exts.is_non_empty(), use_default_filetypes) {
  61. (false, false) => Box::new(NoFileColours) as Box<_>,
  62. (false, true) => Box::new(FileExtensions) as Box<_>,
  63. ( true, false) => Box::new(exts) as Box<_>,
  64. ( true, true) => Box::new((exts, FileExtensions)) as Box<_>,
  65. };
  66. Theme { ui, exts }
  67. }
  68. }
  69. impl Definitions {
  70. /// Parse the environment variables into `LS_COLORS` pairs, putting file glob
  71. /// colours into the `ExtensionMappings` that gets returned, and using the
  72. /// two-character UI codes to modify the mutable `Colours`.
  73. ///
  74. /// Also returns if the `EXA_COLORS` variable should reset the existing file
  75. /// type mappings or not. The `reset` code needs to be the first one.
  76. fn parse_color_vars(&self, colours: &mut UiStyles) -> (ExtensionMappings, bool) {
  77. use log::*;
  78. let mut exts = ExtensionMappings::default();
  79. if let Some(lsc) = &self.ls {
  80. LSColors(lsc).each_pair(|pair| {
  81. if ! colours.set_ls(&pair) {
  82. match glob::Pattern::new(pair.key) {
  83. Ok(pat) => {
  84. exts.add(pat, pair.to_style());
  85. }
  86. Err(e) => {
  87. warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e);
  88. }
  89. }
  90. }
  91. });
  92. }
  93. let mut use_default_filetypes = true;
  94. if let Some(exa) = &self.exa {
  95. // Is this hacky? Yes.
  96. if exa == "reset" || exa.starts_with("reset:") {
  97. use_default_filetypes = false;
  98. }
  99. LSColors(exa).each_pair(|pair| {
  100. if ! colours.set_ls(&pair) && ! colours.set_exa(&pair) {
  101. match glob::Pattern::new(pair.key) {
  102. Ok(pat) => {
  103. exts.add(pat, pair.to_style());
  104. }
  105. Err(e) => {
  106. warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e);
  107. }
  108. }
  109. };
  110. });
  111. }
  112. (exts, use_default_filetypes)
  113. }
  114. }
  115. pub trait FileColours: std::marker::Sync {
  116. fn colour_file(&self, file: &File<'_>) -> Option<Style>;
  117. }
  118. #[derive(PartialEq, Debug)]
  119. struct NoFileColours;
  120. impl FileColours for NoFileColours {
  121. fn colour_file(&self, _file: &File<'_>) -> Option<Style> {
  122. None
  123. }
  124. }
  125. // When getting the colour of a file from a *pair* of colourisers, try the
  126. // first one then try the second one. This lets the user provide their own
  127. // file type associations, while falling back to the default set if not set
  128. // explicitly.
  129. impl<A, B> FileColours for (A, B)
  130. where A: FileColours,
  131. B: FileColours,
  132. {
  133. fn colour_file(&self, file: &File<'_>) -> Option<Style> {
  134. self.0.colour_file(file)
  135. .or_else(|| self.1.colour_file(file))
  136. }
  137. }
  138. #[derive(PartialEq, Debug, Default)]
  139. struct ExtensionMappings {
  140. mappings: Vec<(glob::Pattern, Style)>,
  141. }
  142. // Loop through backwards so that colours specified later in the list override
  143. // colours specified earlier, like we do with options and strict mode
  144. impl FileColours for ExtensionMappings {
  145. fn colour_file(&self, file: &File<'_>) -> Option<Style> {
  146. self.mappings.iter().rev()
  147. .find(|t| t.0.matches(&file.name))
  148. .map (|t| t.1)
  149. }
  150. }
  151. impl ExtensionMappings {
  152. fn is_non_empty(&self) -> bool {
  153. ! self.mappings.is_empty()
  154. }
  155. fn add(&mut self, pattern: glob::Pattern, style: Style) {
  156. self.mappings.push((pattern, style));
  157. }
  158. }
  159. impl render::BlocksColours for Theme {
  160. fn block_count(&self) -> Style { self.ui.blocks }
  161. fn no_blocks(&self) -> Style { self.ui.punctuation }
  162. }
  163. impl render::FiletypeColours for Theme {
  164. fn normal(&self) -> Style { self.ui.filekinds.normal }
  165. fn directory(&self) -> Style { self.ui.filekinds.directory }
  166. fn pipe(&self) -> Style { self.ui.filekinds.pipe }
  167. fn symlink(&self) -> Style { self.ui.filekinds.symlink }
  168. fn block_device(&self) -> Style { self.ui.filekinds.block_device }
  169. fn char_device(&self) -> Style { self.ui.filekinds.char_device }
  170. fn socket(&self) -> Style { self.ui.filekinds.socket }
  171. fn special(&self) -> Style { self.ui.filekinds.special }
  172. }
  173. impl render::GitColours for Theme {
  174. fn not_modified(&self) -> Style { self.ui.punctuation }
  175. #[allow(clippy::new_ret_no_self)]
  176. fn new(&self) -> Style { self.ui.git.new }
  177. fn modified(&self) -> Style { self.ui.git.modified }
  178. fn deleted(&self) -> Style { self.ui.git.deleted }
  179. fn renamed(&self) -> Style { self.ui.git.renamed }
  180. fn type_change(&self) -> Style { self.ui.git.typechange }
  181. fn ignored(&self) -> Style { self.ui.git.ignored }
  182. fn conflicted(&self) -> Style { self.ui.git.conflicted }
  183. }
  184. #[cfg(unix)]
  185. impl render::GroupColours for Theme {
  186. fn yours(&self) -> Style { self.ui.users.group_yours }
  187. fn not_yours(&self) -> Style { self.ui.users.group_not_yours }
  188. fn no_group(&self) -> Style { self.ui.punctuation }
  189. }
  190. impl render::LinksColours for Theme {
  191. fn normal(&self) -> Style { self.ui.links.normal }
  192. fn multi_link_file(&self) -> Style { self.ui.links.multi_link_file }
  193. }
  194. impl render::PermissionsColours for Theme {
  195. fn dash(&self) -> Style { self.ui.punctuation }
  196. fn user_read(&self) -> Style { self.ui.perms.user_read }
  197. fn user_write(&self) -> Style { self.ui.perms.user_write }
  198. fn user_execute_file(&self) -> Style { self.ui.perms.user_execute_file }
  199. fn user_execute_other(&self) -> Style { self.ui.perms.user_execute_other }
  200. fn group_read(&self) -> Style { self.ui.perms.group_read }
  201. fn group_write(&self) -> Style { self.ui.perms.group_write }
  202. fn group_execute(&self) -> Style { self.ui.perms.group_execute }
  203. fn other_read(&self) -> Style { self.ui.perms.other_read }
  204. fn other_write(&self) -> Style { self.ui.perms.other_write }
  205. fn other_execute(&self) -> Style { self.ui.perms.other_execute }
  206. fn special_user_file(&self) -> Style { self.ui.perms.special_user_file }
  207. fn special_other(&self) -> Style { self.ui.perms.special_other }
  208. fn attribute(&self) -> Style { self.ui.perms.attribute }
  209. }
  210. impl render::SizeColours for Theme {
  211. fn size(&self, prefix: Option<number_prefix::Prefix>) -> Style {
  212. use number_prefix::Prefix::*;
  213. match prefix {
  214. Some(Kilo | Kibi) => self.ui.size.number_kilo,
  215. Some(Mega | Mebi) => self.ui.size.number_mega,
  216. Some(Giga | Gibi) => self.ui.size.number_giga,
  217. Some(_) => self.ui.size.number_huge,
  218. None => self.ui.size.number_byte,
  219. }
  220. }
  221. fn unit(&self, prefix: Option<number_prefix::Prefix>) -> Style {
  222. use number_prefix::Prefix::*;
  223. match prefix {
  224. Some(Kilo | Kibi) => self.ui.size.unit_kilo,
  225. Some(Mega | Mebi) => self.ui.size.unit_mega,
  226. Some(Giga | Gibi) => self.ui.size.unit_giga,
  227. Some(_) => self.ui.size.unit_huge,
  228. None => self.ui.size.unit_byte,
  229. }
  230. }
  231. fn no_size(&self) -> Style { self.ui.punctuation }
  232. fn major(&self) -> Style { self.ui.size.major }
  233. fn comma(&self) -> Style { self.ui.punctuation }
  234. fn minor(&self) -> Style { self.ui.size.minor }
  235. }
  236. #[cfg(unix)]
  237. impl render::UserColours for Theme {
  238. fn you(&self) -> Style { self.ui.users.user_you }
  239. fn someone_else(&self) -> Style { self.ui.users.user_someone_else }
  240. fn no_user(&self) -> Style { self.ui.punctuation }
  241. }
  242. impl FileNameColours for Theme {
  243. fn normal_arrow(&self) -> Style { self.ui.punctuation }
  244. fn broken_symlink(&self) -> Style { self.ui.broken_symlink }
  245. fn broken_filename(&self) -> Style { apply_overlay(self.ui.broken_symlink, self.ui.broken_path_overlay) }
  246. fn broken_control_char(&self) -> Style { apply_overlay(self.ui.control_char, self.ui.broken_path_overlay) }
  247. fn control_char(&self) -> Style { self.ui.control_char }
  248. fn symlink_path(&self) -> Style { self.ui.symlink_path }
  249. fn executable_file(&self) -> Style { self.ui.filekinds.executable }
  250. fn colour_file(&self, file: &File<'_>) -> Style {
  251. self.exts.colour_file(file).unwrap_or(self.ui.filekinds.normal)
  252. }
  253. }
  254. impl render::SecurityCtxColours for Theme {
  255. fn none(&self) -> Style { self.ui.security_context.none }
  256. fn selinux_colon(&self) -> Style { self.ui.security_context.selinux.colon }
  257. fn selinux_user(&self) -> Style { self.ui.security_context.selinux.user }
  258. fn selinux_role(&self) -> Style { self.ui.security_context.selinux.role }
  259. fn selinux_type(&self) -> Style { self.ui.security_context.selinux.typ }
  260. fn selinux_range(&self) -> Style { self.ui.security_context.selinux.range }
  261. }
  262. /// Some of the styles are **overlays**: although they have the same attribute
  263. /// set as regular styles (foreground and background colours, bold, underline,
  264. /// etc), they’re intended to be used to *amend* existing styles.
  265. ///
  266. /// For example, the target path of a broken symlink is displayed in a red,
  267. /// underlined style by default. Paths can contain control characters, so
  268. /// these control characters need to be underlined too, otherwise it looks
  269. /// weird. So instead of having four separate configurable styles for “link
  270. /// path”, “broken link path”, “control character” and “broken control
  271. /// character”, there are styles for “link path”, “control character”, and
  272. /// “broken link overlay”, the latter of which is just set to override the
  273. /// underline attribute on the other two.
  274. fn apply_overlay(mut base: Style, overlay: Style) -> Style {
  275. if let Some(fg) = overlay.foreground { base.foreground = Some(fg); }
  276. if let Some(bg) = overlay.background { base.background = Some(bg); }
  277. if overlay.is_bold { base.is_bold = true; }
  278. if overlay.is_dimmed { base.is_dimmed = true; }
  279. if overlay.is_italic { base.is_italic = true; }
  280. if overlay.is_underline { base.is_underline = true; }
  281. if overlay.is_blink { base.is_blink = true; }
  282. if overlay.is_reverse { base.is_reverse = true; }
  283. if overlay.is_hidden { base.is_hidden = true; }
  284. if overlay.is_strikethrough { base.is_strikethrough = true; }
  285. base
  286. }
  287. // TODO: move this function to the ansi_term crate
  288. #[cfg(test)]
  289. mod customs_test {
  290. use super::*;
  291. use crate::theme::ui_styles::UiStyles;
  292. use ansi_term::Colour::*;
  293. macro_rules! test {
  294. ($name:ident: ls $ls:expr, exa $exa:expr => colours $expected:ident -> $process_expected:expr) => {
  295. #[test]
  296. fn $name() {
  297. let mut $expected = UiStyles::default();
  298. $process_expected();
  299. let definitions = Definitions {
  300. ls: Some($ls.into()),
  301. exa: Some($exa.into()),
  302. };
  303. let mut result = UiStyles::default();
  304. let (_exts, _reset) = definitions.parse_color_vars(&mut result);
  305. assert_eq!($expected, result);
  306. }
  307. };
  308. ($name:ident: ls $ls:expr, exa $exa:expr => exts $mappings:expr) => {
  309. #[test]
  310. fn $name() {
  311. let mappings: Vec<(glob::Pattern, Style)>
  312. = $mappings.iter()
  313. .map(|t| (glob::Pattern::new(t.0).unwrap(), t.1))
  314. .collect();
  315. let definitions = Definitions {
  316. ls: Some($ls.into()),
  317. exa: Some($exa.into()),
  318. };
  319. let (result, _reset) = definitions.parse_color_vars(&mut UiStyles::default());
  320. assert_eq!(ExtensionMappings { mappings }, result);
  321. }
  322. };
  323. ($name:ident: ls $ls:expr, exa $exa:expr => colours $expected:ident -> $process_expected:expr, exts $mappings:expr) => {
  324. #[test]
  325. fn $name() {
  326. let mut $expected = UiStyles::colourful(false);
  327. $process_expected();
  328. let mappings: Vec<(glob::Pattern, Style)>
  329. = $mappings.into_iter()
  330. .map(|t| (glob::Pattern::new(t.0).unwrap(), t.1))
  331. .collect();
  332. let definitions = Definitions {
  333. ls: Some($ls.into()),
  334. exa: Some($exa.into()),
  335. };
  336. let mut meh = UiStyles::colourful(false);
  337. let (result, _reset) = definitions.parse_color_vars(&vars, &mut meh);
  338. assert_eq!(ExtensionMappings { mappings }, result);
  339. assert_eq!($expected, meh);
  340. }
  341. };
  342. }
  343. // LS_COLORS can affect all of these colours:
  344. test!(ls_di: ls "di=31", exa "" => colours c -> { c.filekinds.directory = Red.normal(); });
  345. test!(ls_ex: ls "ex=32", exa "" => colours c -> { c.filekinds.executable = Green.normal(); });
  346. test!(ls_fi: ls "fi=33", exa "" => colours c -> { c.filekinds.normal = Yellow.normal(); });
  347. test!(ls_pi: ls "pi=34", exa "" => colours c -> { c.filekinds.pipe = Blue.normal(); });
  348. test!(ls_so: ls "so=35", exa "" => colours c -> { c.filekinds.socket = Purple.normal(); });
  349. test!(ls_bd: ls "bd=36", exa "" => colours c -> { c.filekinds.block_device = Cyan.normal(); });
  350. test!(ls_cd: ls "cd=35", exa "" => colours c -> { c.filekinds.char_device = Purple.normal(); });
  351. test!(ls_ln: ls "ln=34", exa "" => colours c -> { c.filekinds.symlink = Blue.normal(); });
  352. test!(ls_or: ls "or=33", exa "" => colours c -> { c.broken_symlink = Yellow.normal(); });
  353. // EXA_COLORS can affect all those colours too:
  354. test!(exa_di: ls "", exa "di=32" => colours c -> { c.filekinds.directory = Green.normal(); });
  355. test!(exa_ex: ls "", exa "ex=33" => colours c -> { c.filekinds.executable = Yellow.normal(); });
  356. test!(exa_fi: ls "", exa "fi=34" => colours c -> { c.filekinds.normal = Blue.normal(); });
  357. test!(exa_pi: ls "", exa "pi=35" => colours c -> { c.filekinds.pipe = Purple.normal(); });
  358. test!(exa_so: ls "", exa "so=36" => colours c -> { c.filekinds.socket = Cyan.normal(); });
  359. test!(exa_bd: ls "", exa "bd=35" => colours c -> { c.filekinds.block_device = Purple.normal(); });
  360. test!(exa_cd: ls "", exa "cd=34" => colours c -> { c.filekinds.char_device = Blue.normal(); });
  361. test!(exa_ln: ls "", exa "ln=33" => colours c -> { c.filekinds.symlink = Yellow.normal(); });
  362. test!(exa_or: ls "", exa "or=32" => colours c -> { c.broken_symlink = Green.normal(); });
  363. // EXA_COLORS will even override options from LS_COLORS:
  364. test!(ls_exa_di: ls "di=31", exa "di=32" => colours c -> { c.filekinds.directory = Green.normal(); });
  365. test!(ls_exa_ex: ls "ex=32", exa "ex=33" => colours c -> { c.filekinds.executable = Yellow.normal(); });
  366. test!(ls_exa_fi: ls "fi=33", exa "fi=34" => colours c -> { c.filekinds.normal = Blue.normal(); });
  367. // But more importantly, EXA_COLORS has its own, special list of colours:
  368. test!(exa_ur: ls "", exa "ur=38;5;100" => colours c -> { c.perms.user_read = Fixed(100).normal(); });
  369. test!(exa_uw: ls "", exa "uw=38;5;101" => colours c -> { c.perms.user_write = Fixed(101).normal(); });
  370. test!(exa_ux: ls "", exa "ux=38;5;102" => colours c -> { c.perms.user_execute_file = Fixed(102).normal(); });
  371. test!(exa_ue: ls "", exa "ue=38;5;103" => colours c -> { c.perms.user_execute_other = Fixed(103).normal(); });
  372. test!(exa_gr: ls "", exa "gr=38;5;104" => colours c -> { c.perms.group_read = Fixed(104).normal(); });
  373. test!(exa_gw: ls "", exa "gw=38;5;105" => colours c -> { c.perms.group_write = Fixed(105).normal(); });
  374. test!(exa_gx: ls "", exa "gx=38;5;106" => colours c -> { c.perms.group_execute = Fixed(106).normal(); });
  375. test!(exa_tr: ls "", exa "tr=38;5;107" => colours c -> { c.perms.other_read = Fixed(107).normal(); });
  376. test!(exa_tw: ls "", exa "tw=38;5;108" => colours c -> { c.perms.other_write = Fixed(108).normal(); });
  377. test!(exa_tx: ls "", exa "tx=38;5;109" => colours c -> { c.perms.other_execute = Fixed(109).normal(); });
  378. test!(exa_su: ls "", exa "su=38;5;110" => colours c -> { c.perms.special_user_file = Fixed(110).normal(); });
  379. test!(exa_sf: ls "", exa "sf=38;5;111" => colours c -> { c.perms.special_other = Fixed(111).normal(); });
  380. test!(exa_xa: ls "", exa "xa=38;5;112" => colours c -> { c.perms.attribute = Fixed(112).normal(); });
  381. test!(exa_sn: ls "", exa "sn=38;5;113" => colours c -> {
  382. c.size.number_byte = Fixed(113).normal();
  383. c.size.number_kilo = Fixed(113).normal();
  384. c.size.number_mega = Fixed(113).normal();
  385. c.size.number_giga = Fixed(113).normal();
  386. c.size.number_huge = Fixed(113).normal();
  387. });
  388. test!(exa_sb: ls "", exa "sb=38;5;114" => colours c -> {
  389. c.size.unit_byte = Fixed(114).normal();
  390. c.size.unit_kilo = Fixed(114).normal();
  391. c.size.unit_mega = Fixed(114).normal();
  392. c.size.unit_giga = Fixed(114).normal();
  393. c.size.unit_huge = Fixed(114).normal();
  394. });
  395. test!(exa_nb: ls "", exa "nb=38;5;115" => colours c -> { c.size.number_byte = Fixed(115).normal(); });
  396. test!(exa_nk: ls "", exa "nk=38;5;116" => colours c -> { c.size.number_kilo = Fixed(116).normal(); });
  397. test!(exa_nm: ls "", exa "nm=38;5;117" => colours c -> { c.size.number_mega = Fixed(117).normal(); });
  398. test!(exa_ng: ls "", exa "ng=38;5;118" => colours c -> { c.size.number_giga = Fixed(118).normal(); });
  399. test!(exa_nh: ls "", exa "nh=38;5;119" => colours c -> { c.size.number_huge = Fixed(119).normal(); });
  400. test!(exa_ub: ls "", exa "ub=38;5;115" => colours c -> { c.size.unit_byte = Fixed(115).normal(); });
  401. test!(exa_uk: ls "", exa "uk=38;5;116" => colours c -> { c.size.unit_kilo = Fixed(116).normal(); });
  402. test!(exa_um: ls "", exa "um=38;5;117" => colours c -> { c.size.unit_mega = Fixed(117).normal(); });
  403. test!(exa_ug: ls "", exa "ug=38;5;118" => colours c -> { c.size.unit_giga = Fixed(118).normal(); });
  404. test!(exa_uh: ls "", exa "uh=38;5;119" => colours c -> { c.size.unit_huge = Fixed(119).normal(); });
  405. test!(exa_df: ls "", exa "df=38;5;115" => colours c -> { c.size.major = Fixed(115).normal(); });
  406. test!(exa_ds: ls "", exa "ds=38;5;116" => colours c -> { c.size.minor = Fixed(116).normal(); });
  407. test!(exa_uu: ls "", exa "uu=38;5;117" => colours c -> { c.users.user_you = Fixed(117).normal(); });
  408. test!(exa_un: ls "", exa "un=38;5;118" => colours c -> { c.users.user_someone_else = Fixed(118).normal(); });
  409. test!(exa_gu: ls "", exa "gu=38;5;119" => colours c -> { c.users.group_yours = Fixed(119).normal(); });
  410. test!(exa_gn: ls "", exa "gn=38;5;120" => colours c -> { c.users.group_not_yours = Fixed(120).normal(); });
  411. test!(exa_lc: ls "", exa "lc=38;5;121" => colours c -> { c.links.normal = Fixed(121).normal(); });
  412. test!(exa_lm: ls "", exa "lm=38;5;122" => colours c -> { c.links.multi_link_file = Fixed(122).normal(); });
  413. test!(exa_ga: ls "", exa "ga=38;5;123" => colours c -> { c.git.new = Fixed(123).normal(); });
  414. test!(exa_gm: ls "", exa "gm=38;5;124" => colours c -> { c.git.modified = Fixed(124).normal(); });
  415. test!(exa_gd: ls "", exa "gd=38;5;125" => colours c -> { c.git.deleted = Fixed(125).normal(); });
  416. test!(exa_gv: ls "", exa "gv=38;5;126" => colours c -> { c.git.renamed = Fixed(126).normal(); });
  417. test!(exa_gt: ls "", exa "gt=38;5;127" => colours c -> { c.git.typechange = Fixed(127).normal(); });
  418. test!(exa_xx: ls "", exa "xx=38;5;128" => colours c -> { c.punctuation = Fixed(128).normal(); });
  419. test!(exa_da: ls "", exa "da=38;5;129" => colours c -> { c.date = Fixed(129).normal(); });
  420. test!(exa_in: ls "", exa "in=38;5;130" => colours c -> { c.inode = Fixed(130).normal(); });
  421. test!(exa_bl: ls "", exa "bl=38;5;131" => colours c -> { c.blocks = Fixed(131).normal(); });
  422. test!(exa_hd: ls "", exa "hd=38;5;132" => colours c -> { c.header = Fixed(132).normal(); });
  423. test!(exa_lp: ls "", exa "lp=38;5;133" => colours c -> { c.symlink_path = Fixed(133).normal(); });
  424. test!(exa_cc: ls "", exa "cc=38;5;134" => colours c -> { c.control_char = Fixed(134).normal(); });
  425. test!(exa_bo: ls "", exa "bO=4" => colours c -> { c.broken_path_overlay = Style::default().underline(); });
  426. // All the while, LS_COLORS treats them as filenames:
  427. test!(ls_uu: ls "uu=38;5;117", exa "" => exts [ ("uu", Fixed(117).normal()) ]);
  428. test!(ls_un: ls "un=38;5;118", exa "" => exts [ ("un", Fixed(118).normal()) ]);
  429. test!(ls_gu: ls "gu=38;5;119", exa "" => exts [ ("gu", Fixed(119).normal()) ]);
  430. test!(ls_gn: ls "gn=38;5;120", exa "" => exts [ ("gn", Fixed(120).normal()) ]);
  431. // Just like all other keys:
  432. test!(ls_txt: ls "*.txt=31", exa "" => exts [ ("*.txt", Red.normal()) ]);
  433. test!(ls_mp3: ls "*.mp3=38;5;135", exa "" => exts [ ("*.mp3", Fixed(135).normal()) ]);
  434. test!(ls_mak: ls "Makefile=1;32;4", exa "" => exts [ ("Makefile", Green.bold().underline()) ]);
  435. test!(exa_txt: ls "", exa "*.zip=31" => exts [ ("*.zip", Red.normal()) ]);
  436. test!(exa_mp3: ls "", exa "lev.*=38;5;153" => exts [ ("lev.*", Fixed(153).normal()) ]);
  437. test!(exa_mak: ls "", exa "Cargo.toml=4;32;1" => exts [ ("Cargo.toml", Green.bold().underline()) ]);
  438. // Testing whether a glob from EXA_COLORS overrides a glob from LS_COLORS
  439. // can’t be tested here, because they’ll both be added to the same vec
  440. // Values get separated by colons:
  441. test!(ls_multi: ls "*.txt=31:*.rtf=32", exa "" => exts [ ("*.txt", Red.normal()), ("*.rtf", Green.normal()) ]);
  442. test!(exa_multi: ls "", exa "*.tmp=37:*.log=37" => exts [ ("*.tmp", White.normal()), ("*.log", White.normal()) ]);
  443. test!(ls_five: ls "1*1=31:2*2=32:3*3=1;33:4*4=34;1:5*5=35;4", exa "" => exts [
  444. ("1*1", Red.normal()), ("2*2", Green.normal()), ("3*3", Yellow.bold()), ("4*4", Blue.bold()), ("5*5", Purple.underline())
  445. ]);
  446. // Finally, colours get applied right-to-left:
  447. test!(ls_overwrite: ls "pi=31:pi=32:pi=33", exa "" => colours c -> { c.filekinds.pipe = Yellow.normal(); });
  448. test!(exa_overwrite: ls "", exa "da=36:da=35:da=34" => colours c -> { c.date = Blue.normal(); });
  449. }