Skip to content

Commit

Permalink
Merge pull request #3590 from tertsdiepraam/ls-fix-double-quoting
Browse files Browse the repository at this point in the history
ls: fix double quoting when color is enabled
  • Loading branch information
sylvestre authored Jun 5, 2022
2 parents 68cc931 + 626ed43 commit 10eee9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand Down

0 comments on commit 10eee9c

Please sign in to comment.