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
40 changes: 37 additions & 3 deletions src/uu/ls/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ fn display_items_non_long(
&mut state.out,
quoted,
config.tab_size,
config.line_ending,
),
Format::Across => display_grid(
names.into_iter(),
Expand All @@ -418,6 +419,7 @@ fn display_items_non_long(
&mut state.out,
quoted,
config.tab_size,
config.line_ending,
),
Format::Commas => display_commas_format(
names.into_iter(),
Expand Down Expand Up @@ -497,6 +499,7 @@ fn display_grid(
out: &mut BufWriter<Stdout>,
quoted: bool,
tab_size: usize,
line_ending: LineEnding,
) -> UResult<()> {
if width == 0 {
// If the width is 0 we print one single line
Expand All @@ -509,7 +512,7 @@ fn display_grid(
write_os_str(out, &name.displayed)?;
}
if printed_something {
writeln!(out)?;
write!(out, "{line_ending}")?;
}
} else {
let names: Vec<String> = {
Expand Down Expand Up @@ -547,7 +550,6 @@ fn display_grid(
tab_size,
},
};

let grid = Grid::new(
names,
GridOptions {
Expand All @@ -556,7 +558,39 @@ fn display_grid(
width: width as usize,
},
);
write!(out, "{grid}")?;
match line_ending {
LineEnding::Newline => write!(out, "{grid}")?,
LineEnding::Nul => {
struct GridNulWriter<'a, W> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please move this struct out of the function? it makes display_grid hard to read

out: &'a mut W,
io_error: Option<std::io::Error>,
}
impl<W: Write> FmtWrite for GridNulWriter<'_, W> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
let res = if s == "\n" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this relies on term_grid writing the row end as a separate "\n" chunk, so a file named just \n would become \0 too, no?
could you please add a test with such a file?

self.out.write_all(b"\0")
} else {
self.out.write_all(s.as_bytes())
};
if let Err(e) = res {
self.io_error = Some(e);
return Err(std::fmt::Error);
}
Ok(())
}
}
let mut writer = GridNulWriter {
out,
io_error: None,
};
if let Err(e) = write!(writer, "{grid}") {
if let Some(io_err) = writer.io_error {
return Err(io_err.into());
}
return Err(std::io::Error::other(e).into());
}
}
}
}
Ok(())
}
Expand Down
40 changes: 40 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,26 @@ fn test_ls_zero() {
.succeeds()
.stdout_only("0-test-zero, 2-test-zero, 3-test-zero\x00");

for opt in ["-C", "-x"] {
scene
.ucmd()
.args(&["--zero", opt, "-w", "80"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also test with a smaller -w to get several rows, so we check every line ends with \0

.succeeds()
.stdout_only("0-test-zero 2-test-zero 3-test-zero\x00");
}

scene
.ucmd()
.args(&["--zero", "-C", "-w", "30"])
.succeeds()
.stdout_only("0-test-zero 3-test-zero\x002-test-zero\x00");

scene
.ucmd()
.args(&["--zero", "-x", "-w", "30"])
.succeeds()
.stdout_only("0-test-zero 2-test-zero\x003-test-zero\x00");

scene
.ucmd()
.args(&["--zero", "--hide-control-chars"])
Expand Down Expand Up @@ -1142,6 +1162,26 @@ fn test_ls_zero() {
.succeeds()
.stdout_only("0-test-zero, 1\ntest-zero, 2-test-zero, 3-test-zero\x00");

for opt in ["-C", "-x"] {
scene
.ucmd()
.args(&["--zero", opt, "-w", "80"])
.succeeds()
.stdout_only("0-test-zero 1\ntest-zero\t 2-test-zero 3-test-zero\x00");
}

scene
.ucmd()
.args(&["--zero", "-C", "-w", "30"])
.succeeds()
.stdout_only("0-test-zero 2-test-zero\x001\ntest-zero 3-test-zero\x00");

scene
.ucmd()
.args(&["--zero", "-x", "-w", "30"])
.succeeds()
.stdout_only("0-test-zero 1\ntest-zero\x002-test-zero 3-test-zero\x00");

scene
.ucmd()
.args(&["--zero", "--hide-control-chars"])
Expand Down
Loading