1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

ls: fix double quoting when color is enabled

When color was enabled the escape_name function was called twice which led to names like `hello world` being displayed as `"'hello world'".
This commit is contained in:
Terts Diepraam 2022-06-02 23:44:18 +02:00
parent 68cc9312c8
commit 626ed43e05
2 changed files with 27 additions and 18 deletions

View file

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