1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

ls: print a single line when width is set to 0

This means that we treat a width=0 as infinite width.
This commit is contained in:
Michael Debertol 2021-08-11 15:53:39 +02:00
parent 13a62489c5
commit 988cc49d4a
2 changed files with 64 additions and 34 deletions

View file

@ -1440,7 +1440,8 @@ fn display_items(items: &[PathData], config: &Config, out: &mut BufWriter<Stdout
} }
for name in names { for name in names {
let name_width = name.width as u16; let name_width = name.width as u16;
if current_col + name_width + 1 > config.width { // If the width is 0 we print one single line
if config.width != 0 && current_col + name_width + 1 > config.width {
current_col = name_width + 2; current_col = name_width + 2;
let _ = write!(out, ",\n{}", name.contents); let _ = write!(out, ",\n{}", name.contents);
} else { } else {
@ -1492,22 +1493,37 @@ fn display_grid(
direction: Direction, direction: Direction,
out: &mut BufWriter<Stdout>, out: &mut BufWriter<Stdout>,
) { ) {
let mut grid = Grid::new(GridOptions { if width == 0 {
filling: Filling::Spaces(2), // If the width is 0 we print one single line
direction, let mut printed_something = false;
}); for name in names {
if printed_something {
for name in names { let _ = write!(out, " ");
grid.add(name); }
} printed_something = true;
let _ = write!(out, "{}", name.contents);
match grid.fit_into_width(width as usize) {
Some(output) => {
let _ = write!(out, "{}", output);
} }
// Width is too small for the grid, so we fit it in one column if printed_something {
None => { let _ = writeln!(out);
let _ = write!(out, "{}", grid.fit_into_columns(1)); }
} else {
let mut grid = Grid::new(GridOptions {
filling: Filling::Spaces(2),
direction,
});
for name in names {
grid.add(name);
}
match grid.fit_into_width(width as usize) {
Some(output) => {
let _ = write!(out, "{}", output);
}
// Width is too small for the grid, so we fit it in one column
None => {
let _ = write!(out, "{}", grid.fit_into_columns(1));
}
} }
} }
} }

View file

@ -140,16 +140,7 @@ fn test_ls_width() {
.stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n"); .stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n");
} }
for option in &[ for option in &["-w 25", "-w=25", "--width=25", "--width 25"] {
"-w 25",
"-w=25",
"--width=25",
"--width 25",
"-w 0",
"-w=0",
"--width=0",
"--width 0",
] {
scene scene
.ucmd() .ucmd()
.args(&option.split(' ').collect::<Vec<_>>()) .args(&option.split(' ').collect::<Vec<_>>())
@ -157,6 +148,14 @@ fn test_ls_width() {
.stdout_only("test-width-1\ntest-width-2\ntest-width-3\ntest-width-4\n"); .stdout_only("test-width-1\ntest-width-2\ntest-width-3\ntest-width-4\n");
} }
for option in &["-w 0", "-w=0", "--width=0", "--width 0"] {
scene
.ucmd()
.args(&option.split(' ').collect::<Vec<_>>())
.succeeds()
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
}
scene scene
.ucmd() .ucmd()
.arg("-w=bad") .arg("-w=bad")
@ -200,21 +199,36 @@ fn test_ls_columns() {
.stdout_only("test-columns-1 test-columns-3\ntest-columns-2 test-columns-4\n"); .stdout_only("test-columns-1 test-columns-3\ntest-columns-2 test-columns-4\n");
} }
for option in &["-C", "--format=columns"] { // On windows we are always able to get the terminal size, so we can't simulate falling back to the
// environment variable.
#[cfg(not(windows))]
{
for option in &["-C", "--format=columns"] {
scene
.ucmd()
.env("COLUMNS", "40")
.arg(option)
.succeeds()
.stdout_only("test-columns-1 test-columns-3\ntest-columns-2 test-columns-4\n");
}
scene scene
.ucmd() .ucmd()
.env("COLUMNS", "40") .env("COLUMNS", "garbage")
.arg(option)
.succeeds() .succeeds()
.stdout_only("test-columns-1 test-columns-3\ntest-columns-2 test-columns-4\n"); .stdout_is("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n")
.stderr_is("ls: ignoring invalid width in environment variable COLUMNS: 'garbage'");
} }
scene scene
.ucmd() .ucmd()
.env("COLUMNS", "garbage") .arg("-w0")
.succeeds() .succeeds()
.stdout_is("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n") .stdout_only("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n");
.stderr_is("ls: ignoring invalid width in environment variable COLUMNS: 'garbage'"); scene
.ucmd()
.arg("-mw0")
.succeeds()
.stdout_only("test-columns-1, test-columns-2, test-columns-3, test-columns-4\n");
} }
#[test] #[test]