Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: fix double quoting when color is enabled #3590

Merged
merged 1 commit into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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