Skip to content

Commit

Permalink
Merge pull request #3317 from sylvestre/ls-quote
Browse files Browse the repository at this point in the history
ls: Add proper quotes on symlink with --quoting-style=shell-escape
  • Loading branch information
sylvestre authored Mar 27, 2022
2 parents e1f8893 + c79d146 commit c932236
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2478,7 +2478,7 @@ fn display_file_name(

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);
name = color_name(ls_colors, &path.p_buf, &name, &metadata, config);
}
}

Expand Down Expand Up @@ -2562,13 +2562,15 @@ fn display_file_name(
name.push_str(&color_name(
ls_colors,
&target_data.p_buf,
target.to_string_lossy().into_owned(),
&target.to_string_lossy(),
&target_metadata,
config,
));
}
} else {
// If no coloring is required, we just use target as is.
name.push_str(&target.to_string_lossy());
// Apply the right quoting
name.push_str(&escape_name(target.as_os_str(), &config.quoting_style));
}
}
}
Expand All @@ -2593,10 +2595,19 @@ fn display_file_name(
}
}

fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String {
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)) {
Some(style) => style.to_ansi_term_style().paint(name).to_string(),
None => name,
Some(style) => {
let p = escape_name(OsStr::new(&name), &config.quoting_style);
return style.to_ansi_term_style().paint(p).to_string();
}
None => escape_name(OsStr::new(&name), &config.quoting_style),
}
}

Expand Down
39 changes: 39 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,3 +2913,42 @@ fn test_ls_multiple_a_A() {
.stdout_does_not_contain(".")
.stdout_does_not_contain("..");
}

#[test]
fn test_ls_quoting() {
let scene = TestScenario::new(util_name!());

scene
.ccmd("ln")
.arg("-s")
.arg("'need quoting'")
.arg("symlink")
.succeeds();
scene
.ucmd()
.arg("-l")
.arg("--quoting-style=shell-escape")
.arg("symlink")
.succeeds()
.stdout_contains("\'need quoting\'");
}

#[test]
fn test_ls_quoting_color() {
let scene = TestScenario::new(util_name!());

scene
.ccmd("ln")
.arg("-s")
.arg("'need quoting'")
.arg("symlink")
.succeeds();
scene
.ucmd()
.arg("-l")
.arg("--quoting-style=shell-escape")
.arg("--color=auto")
.arg("symlink")
.succeeds()
.stdout_contains("\'need quoting\'");
}

0 comments on commit c932236

Please sign in to comment.