Merge pull request #3590 from tertsdiepraam/ls-fix-double-quoting

ls: fix double quoting when color is enabled
This commit is contained in:
Sylvestre Ledru 2022-06-05 09:30:01 +02:00 committed by GitHub
commit 10eee9cfca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View file

@ -2505,9 +2505,12 @@ fn display_file_name(
let mut width = name.width();
if let Some(ls_colors) = &config.color {
if let Ok(metadata) = path.p_buf.symlink_metadata() {
name = color_name(ls_colors, &path.p_buf, &name, &metadata, config);
}
name = color_name(
name,
&path.p_buf,
path.p_buf.symlink_metadata().ok().as_ref(),
ls_colors,
);
}
if config.format != Format::Long && !more_info.is_empty() {
@ -2588,11 +2591,10 @@ fn display_file_name(
};
name.push_str(&color_name(
ls_colors,
escape_name(target.as_os_str(), &config.quoting_style),
&target_data.p_buf,
&target.to_string_lossy(),
&target_metadata,
config,
Some(&target_metadata),
ls_colors,
));
}
} else {
@ -2623,19 +2625,12 @@ fn display_file_name(
}
}
fn color_name(
ls_colors: &LsColors,
path: &Path,
name: &str,
md: &Metadata,
config: &Config,
) -> String {
match ls_colors.style_for_path_with_metadata(path, Some(md)) {
fn color_name(name: String, path: &Path, md: Option<&Metadata>, ls_colors: &LsColors) -> String {
match ls_colors.style_for_path_with_metadata(path, md) {
Some(style) => {
let p = escape_name(OsStr::new(&name), &config.quoting_style);
return style.to_ansi_term_style().paint(p).to_string();
return style.to_ansi_term_style().paint(name).to_string();
}
None => escape_name(OsStr::new(&name), &config.quoting_style),
None => name,
}
}

View file

@ -2324,6 +2324,20 @@ fn test_ls_quoting_style() {
}
}
#[test]
fn test_ls_quoting_and_color() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("one two");
scene
.ucmd()
.arg("--color")
.arg("one two")
.succeeds()
.stdout_only("'one two'\n");
}
#[test]
fn test_ls_ignore_hide() {
let scene = TestScenario::new(util_name!());