mirror of
https://github.com/sharkdp/fd.git
synced 2024-11-18 18:00:35 +01:00
Enable stylization by filename
This commit is contained in:
parent
c6209471a2
commit
961169aff9
2 changed files with 20 additions and 8 deletions
|
@ -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()));
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue