Repository navigation
fix(ls): respect line_ending in display_grid when using --zero with column formats #15505
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(), | ||
|
|
@@ -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(), | ||
|
|
@@ -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 | ||
|
|
@@ -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> = { | ||
|
|
@@ -547,7 +550,6 @@ fn display_grid( | |
| tab_size, | ||
| }, | ||
| }; | ||
|
|
||
| let grid = Grid::new( | ||
| names, | ||
| GridOptions { | ||
|
|
@@ -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> { | ||
| 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" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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(()) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also test with a smaller |
||
| .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"]) | ||
|
|
@@ -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"]) | ||
|
|
||
There was a problem hiding this comment.
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