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 {
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;
let _ = write!(out, ",\n{}", name.contents);
} else {
@ -1492,22 +1493,37 @@ fn display_grid(
direction: Direction,
out: &mut BufWriter<Stdout>,
) {
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);
if width == 0 {
// If the width is 0 we print one single line
let mut printed_something = false;
for name in names {
if printed_something {
let _ = write!(out, " ");
}
printed_something = true;
let _ = write!(out, "{}", name.contents);
}
// Width is too small for the grid, so we fit it in one column
None => {
let _ = write!(out, "{}", grid.fit_into_columns(1));
if printed_something {
let _ = writeln!(out);
}
} 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");
}
for option in &[
"-w 25",
"-w=25",
"--width=25",
"--width 25",
"-w 0",
"-w=0",
"--width=0",
"--width 0",
] {
for option in &["-w 25", "-w=25", "--width=25", "--width 25"] {
scene
.ucmd()
.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");
}
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
.ucmd()
.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");
}
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
.ucmd()
.env("COLUMNS", "40")
.arg(option)
.env("COLUMNS", "garbage")
.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
.ucmd()
.env("COLUMNS", "garbage")
.arg("-w0")
.succeeds()
.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'");
.stdout_only("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n");
scene
.ucmd()
.arg("-mw0")
.succeeds()
.stdout_only("test-columns-1, test-columns-2, test-columns-3, test-columns-4\n");
}
#[test]