From 961169aff960be6d7b820fedc79ad52c6ce1af6d Mon Sep 17 00:00:00 2001 From: sharkdp Date: Thu, 1 Jun 2017 23:08:02 +0200 Subject: [PATCH] Enable stylization by filename --- src/bin/main.rs | 22 ++++++++++++++++------ src/lscolors/mod.rs | 6 ++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index e0d0271..50fc764 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -63,12 +63,22 @@ fn print_entry(path_root: &Path, path_entry: &Path, config: &FdOptions) { } else if component_path.is_dir() { config.ls_colors.directory } else { - // Loop up file extension - component_path.extension() - .and_then(|e| e.to_str()) - .and_then(|e| config.ls_colors.extensions.get(e)) - .map(|r| r.clone()) - .unwrap_or(Style::new()) + // Look up file name + let o_style = + component_path.file_name() + .and_then(|n| n.to_str()) + .and_then(|n| config.ls_colors.filenames.get(n)); + + match o_style { + Some(s) => s.clone(), + None => + // Look up file extension + component_path.extension() + .and_then(|e| e.to_str()) + .and_then(|e| config.ls_colors.extensions.get(e)) + .map(|r| r.clone()) + .unwrap_or(Style::new()) + } }; print!("{}", style.paint(comp_str.to_str().unwrap())); diff --git a/src/lscolors/mod.rs b/src/lscolors/mod.rs index b189ac9..34976a4 100644 --- a/src/lscolors/mod.rs +++ b/src/lscolors/mod.rs @@ -109,7 +109,7 @@ impl LsColors { } else if pattern.starts_with("*") { let filename = String::from(pattern).split_off(1); - self.extensions.insert(filename, style); + self.filenames.insert(filename, style); } else { // Unknown/corrupt pattern return; @@ -154,8 +154,10 @@ fn test_lscolors() { assert_eq!(LsColors::default(), LsColors::from_string(&String::new())); let result = LsColors::from_string( - &String::from("rs=0:di=03;34:ln=01;36:*.foo=01;31:")); + &String::from("rs=0:di=03;34:ln=01;36:*.foo=01;35:*README=33")); assert_eq!(Colour::Blue.italic(), result.directory); assert_eq!(Colour::Cyan.bold(), result.symlink); + assert_eq!(Some(&Colour::Purple.bold()), result.extensions.get("foo")); + assert_eq!(Some(&Colour::Yellow.normal()), result.filenames.get("README")); }