Skip to content
Open
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
24 changes: 22 additions & 2 deletions src/uu/ls/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn display_grid(
// ```
// FIXME: the Grid crate only supports &str, so can't display raw bytes
buf.clear();
if quoted && !os_str_starts_with(&n, b"'") && !os_str_starts_with(&n, b"\"") {
if quoted && !displayed_name_starts_with_quote(&n) {
buf.push(b' ');
}
buf.extend(n.as_encoded_bytes());
Expand Down Expand Up @@ -968,7 +968,7 @@ fn display_item_long(
LazyCell::new(|| ansi_width(&String::from_utf8_lossy(&state.display_buf))),
);

let needs_space = quoted && !os_str_starts_with(&item_display.displayed, b"'");
let needs_space = quoted && !displayed_name_starts_with_quote(&item_display.displayed);

if config.dired {
let mut dired_name_len = item_display.dired_name_len;
Expand Down Expand Up @@ -1340,6 +1340,26 @@ fn os_str_starts_with(haystack: &OsStr, needle: &[u8]) -> bool {
os_str_as_bytes_lossy(haystack).starts_with(needle)
}

fn displayed_name_starts_with_quote(name: &OsStr) -> bool {
let bytes = os_str_as_bytes_lossy(name);
let bytes = strip_leading_ansi_sequences(&bytes);
bytes.starts_with(b"'") || bytes.starts_with(b"\"")
}

fn strip_leading_ansi_sequences(mut bytes: &[u8]) -> &[u8] {
while bytes.starts_with(b"\x1b[") {
let Some(end) = bytes[2..]
.iter()
.position(|&b| (0x40..=0x7e).contains(&b))
.map(|end| end + 2)
else {
break;
};
bytes = &bytes[end + 1..];
}
bytes
}

fn write_os_str<W: Write>(writer: &mut W, string: &OsStr) -> std::io::Result<()> {
writer.write_all(&os_str_as_bytes_lossy(string))
}
43 changes: 43 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3997,6 +3997,49 @@ fn test_ls_align_unquoted() {
}
}

#[test]
fn test_ls_color_does_not_make_quoted_names_align_as_unquoted() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir("dir one");
at.touch("file one");

let args = ["-C", "-T0", "-w=80", "--quoting-style=shell-escape-always"];

let plain = scene
.ucmd()
.args(&args)
.arg("--color=never")
.succeeds()
.stdout_move_str();
let colored = scene
.ucmd()
.args(&args)
.arg("--color=always")
.succeeds()
.stdout_move_str();

let ansi_re = Regex::new(r"\x1b\[[0-9;]*[A-Za-z]").unwrap();
assert_eq!(ansi_re.replace_all(&colored, ""), plain);

let long_args = ["-l", "--quoting-style=shell-escape-always"];
let plain = scene
.ucmd()
.args(&long_args)
.arg("--color=never")
.succeeds()
.stdout_move_str();
let colored = scene
.ucmd()
.args(&long_args)
.arg("--color=always")
.succeeds()
.stdout_move_str();

assert_eq!(ansi_re.replace_all(&colored, ""), plain);
}

#[test]
fn test_ls_align_unquoted_multiline() {
let scene = TestScenario::new(util_name!());
Expand Down
Loading