From 626ed43e05d7d167024229971c728b50ce61f7c4 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 2 Jun 2022 23:44:18 +0200 Subject: [PATCH] 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'". --- src/uu/ls/src/ls.rs | 31 +++++++++++++------------------ tests/by-util/test_ls.rs | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 52a5a114526..2ebd2fb973d 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -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, } } diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index ba95fa6a1a6..db2c3d445f2 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -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!());