Enable stylization by filename

This commit is contained in:
sharkdp 2017-06-01 23:08:02 +02:00
parent c6209471a2
commit 961169aff9
2 changed files with 20 additions and 8 deletions

View file

@ -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()));

View file

@ -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"));
}