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
26 changes: 24 additions & 2 deletions src/uu/ls/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ 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
&& !os_str_starts_with_skip_ansi(&n, b"'")
&& !os_str_starts_with_skip_ansi(&n, b"\"")
{
buf.push(b' ');
}
buf.extend(n.as_encoded_bytes());
Expand Down Expand Up @@ -968,7 +971,8 @@ 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 && !os_str_starts_with_skip_ansi(&item_display.displayed, b"'");

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

fn os_str_starts_with_skip_ansi(haystack: &OsStr, needle: &[u8]) -> bool {
let bytes = os_str_as_bytes_lossy(haystack);
let mut i = 0;
while i < bytes.len() && bytes[i] == b'\x1b' {
i += 1;
if i < bytes.len() && bytes[i] == b'[' {
i += 1;
while i < bytes.len() && bytes[i] != b'm' {
i += 1;
}
if i < bytes.len() {
i += 1; // skip 'm'
}
}
}
bytes[i..].starts_with(needle)
}

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

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

at.mkdir("aa directory");
at.mkdir("bb directory");
at.touch("cc file");

let result = scene
.ucmd()
.arg("--color=always")
.arg("--quoting-style=shell-escape-always")
.arg("-T0")
.arg("--format=column")
.succeeds();

let stdout = result.stdout_str();
for line in stdout.lines() {
assert!(
!line.starts_with(' '),
"Line should not start with a space: {line:?}"
);
}
}

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