mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
ls: default to one-line output if stdout is not a tty
This commit is contained in:
parent
988cc49d4a
commit
0af244ac42
2 changed files with 35 additions and 24 deletions
|
@ -260,16 +260,20 @@ impl Config {
|
||||||
// below should never happen as clap already restricts the values.
|
// below should never happen as clap already restricts the values.
|
||||||
_ => unreachable!("Invalid field for --format"),
|
_ => unreachable!("Invalid field for --format"),
|
||||||
},
|
},
|
||||||
options::FORMAT,
|
Some(options::FORMAT),
|
||||||
)
|
)
|
||||||
} else if options.is_present(options::format::LONG) {
|
} else if options.is_present(options::format::LONG) {
|
||||||
(Format::Long, options::format::LONG)
|
(Format::Long, Some(options::format::LONG))
|
||||||
} else if options.is_present(options::format::ACROSS) {
|
} else if options.is_present(options::format::ACROSS) {
|
||||||
(Format::Across, options::format::ACROSS)
|
(Format::Across, Some(options::format::ACROSS))
|
||||||
} else if options.is_present(options::format::COMMAS) {
|
} else if options.is_present(options::format::COMMAS) {
|
||||||
(Format::Commas, options::format::COMMAS)
|
(Format::Commas, Some(options::format::COMMAS))
|
||||||
|
} else if options.is_present(options::format::COLUMNS) {
|
||||||
|
(Format::Columns, Some(options::format::COLUMNS))
|
||||||
|
} else if atty::is(atty::Stream::Stdout) {
|
||||||
|
(Format::Columns, None)
|
||||||
} else {
|
} else {
|
||||||
(Format::Columns, options::format::COLUMNS)
|
(Format::OneLine, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
// The -o, -n and -g options are tricky. They cannot override with each
|
// The -o, -n and -g options are tricky. They cannot override with each
|
||||||
|
@ -288,9 +292,8 @@ impl Config {
|
||||||
// options, but manually whether they have an index that's greater than
|
// options, but manually whether they have an index that's greater than
|
||||||
// the other format options. If so, we set the appropriate format.
|
// the other format options. If so, we set the appropriate format.
|
||||||
if format != Format::Long {
|
if format != Format::Long {
|
||||||
let idx = options
|
let idx = opt
|
||||||
.indices_of(opt)
|
.and_then(|opt| options.indices_of(opt).map(|x| x.max().unwrap()))
|
||||||
.map(|x| x.max().unwrap())
|
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
if [
|
if [
|
||||||
options::format::LONG_NO_OWNER,
|
options::format::LONG_NO_OWNER,
|
||||||
|
|
|
@ -128,6 +128,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.args(&option.split(' ').collect::<Vec<_>>())
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
|
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
|
||||||
}
|
}
|
||||||
|
@ -136,6 +137,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.args(&option.split(' ').collect::<Vec<_>>())
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.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");
|
||||||
}
|
}
|
||||||
|
@ -144,6 +146,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.args(&option.split(' ').collect::<Vec<_>>())
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.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");
|
||||||
}
|
}
|
||||||
|
@ -152,6 +155,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.args(&option.split(' ').collect::<Vec<_>>())
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
|
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
|
||||||
}
|
}
|
||||||
|
@ -159,6 +163,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-w=bad")
|
.arg("-w=bad")
|
||||||
|
.arg("-C")
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_contains("invalid line width");
|
.stderr_contains("invalid line width");
|
||||||
|
|
||||||
|
@ -166,6 +171,7 @@ fn test_ls_width() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.args(&option.split(' ').collect::<Vec<_>>())
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
.fails()
|
.fails()
|
||||||
.stderr_only("ls: invalid line width: '1a'");
|
.stderr_only("ls: invalid line width: '1a'");
|
||||||
}
|
}
|
||||||
|
@ -183,7 +189,7 @@ fn test_ls_columns() {
|
||||||
// Columns is the default
|
// Columns is the default
|
||||||
let result = scene.ucmd().succeeds();
|
let result = scene.ucmd().succeeds();
|
||||||
|
|
||||||
result.stdout_only("test-columns-1 test-columns-2 test-columns-3 test-columns-4\n");
|
result.stdout_only("test-columns-1\ntest-columns-2\ntest-columns-3\ntest-columns-4\n");
|
||||||
|
|
||||||
for option in &["-C", "--format=columns"] {
|
for option in &["-C", "--format=columns"] {
|
||||||
let result = scene.ucmd().arg(option).succeeds();
|
let result = scene.ucmd().arg(option).succeeds();
|
||||||
|
@ -215,13 +221,14 @@ fn test_ls_columns() {
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.env("COLUMNS", "garbage")
|
.env("COLUMNS", "garbage")
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is("test-columns-1 test-columns-2 test-columns-3 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'");
|
.stderr_is("ls: ignoring invalid width in environment variable COLUMNS: 'garbage'");
|
||||||
}
|
}
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-w0")
|
.arg("-Cw0")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only("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");
|
||||||
scene
|
scene
|
||||||
|
@ -591,7 +598,7 @@ fn test_ls_sort_name() {
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("--sort=name")
|
.arg("--sort=name")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is("test-1 test-2 test-3\n");
|
.stdout_is("test-1\ntest-2\ntest-3\n");
|
||||||
|
|
||||||
let scene_dot = TestScenario::new(util_name!());
|
let scene_dot = TestScenario::new(util_name!());
|
||||||
let at = &scene_dot.fixtures;
|
let at = &scene_dot.fixtures;
|
||||||
|
@ -605,7 +612,7 @@ fn test_ls_sort_name() {
|
||||||
.arg("--sort=name")
|
.arg("--sort=name")
|
||||||
.arg("-A")
|
.arg("-A")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is(".a .b a b\n");
|
.stdout_is(".a\n.b\na\nb\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -626,16 +633,16 @@ fn test_ls_order_size() {
|
||||||
scene.ucmd().arg("-al").succeeds();
|
scene.ucmd().arg("-al").succeeds();
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-S").succeeds();
|
let result = scene.ucmd().arg("-S").succeeds();
|
||||||
result.stdout_only("test-4 test-3 test-2 test-1\n");
|
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-S").arg("-r").succeeds();
|
let result = scene.ucmd().arg("-S").arg("-r").succeeds();
|
||||||
result.stdout_only("test-1 test-2 test-3 test-4\n");
|
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--sort=size").succeeds();
|
let result = scene.ucmd().arg("--sort=size").succeeds();
|
||||||
result.stdout_only("test-4 test-3 test-2 test-1\n");
|
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--sort=size").arg("-r").succeeds();
|
let result = scene.ucmd().arg("--sort=size").arg("-r").succeeds();
|
||||||
result.stdout_only("test-1 test-2 test-3 test-4\n");
|
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -793,16 +800,16 @@ fn test_ls_order_time() {
|
||||||
|
|
||||||
// ctime was changed at write, so the order is 4 3 2 1
|
// ctime was changed at write, so the order is 4 3 2 1
|
||||||
let result = scene.ucmd().arg("-t").succeeds();
|
let result = scene.ucmd().arg("-t").succeeds();
|
||||||
result.stdout_only("test-4 test-3 test-2 test-1\n");
|
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--sort=time").succeeds();
|
let result = scene.ucmd().arg("--sort=time").succeeds();
|
||||||
result.stdout_only("test-4 test-3 test-2 test-1\n");
|
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("-tr").succeeds();
|
let result = scene.ucmd().arg("-tr").succeeds();
|
||||||
result.stdout_only("test-1 test-2 test-3 test-4\n");
|
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
|
||||||
|
|
||||||
let result = scene.ucmd().arg("--sort=time").arg("-r").succeeds();
|
let result = scene.ucmd().arg("--sort=time").arg("-r").succeeds();
|
||||||
result.stdout_only("test-1 test-2 test-3 test-4\n");
|
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
|
||||||
|
|
||||||
// 3 was accessed last in the read
|
// 3 was accessed last in the read
|
||||||
// So the order should be 2 3 4 1
|
// So the order should be 2 3 4 1
|
||||||
|
@ -813,11 +820,11 @@ fn test_ls_order_time() {
|
||||||
|
|
||||||
// It seems to be dependent on the platform whether the access time is actually set
|
// It seems to be dependent on the platform whether the access time is actually set
|
||||||
if file3_access > file4_access {
|
if file3_access > file4_access {
|
||||||
result.stdout_only("test-3 test-4 test-2 test-1\n");
|
result.stdout_only("test-3\ntest-4\ntest-2\ntest-1\n");
|
||||||
} else {
|
} else {
|
||||||
// Access time does not seem to be set on Windows and some other
|
// Access time does not seem to be set on Windows and some other
|
||||||
// systems so the order is 4 3 2 1
|
// systems so the order is 4 3 2 1
|
||||||
result.stdout_only("test-4 test-3 test-2 test-1\n");
|
result.stdout_only("test-4\ntest-3\ntest-2\ntest-1\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -826,7 +833,7 @@ fn test_ls_order_time() {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
let result = scene.ucmd().arg("-tc").succeeds();
|
let result = scene.ucmd().arg("-tc").succeeds();
|
||||||
result.stdout_only("test-2 test-4 test-3 test-1\n");
|
result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -970,6 +977,7 @@ fn test_ls_color() {
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("--color")
|
.arg("--color")
|
||||||
.arg("-w=15")
|
.arg("-w=15")
|
||||||
|
.arg("-C")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_only(format!(
|
.stdout_only(format!(
|
||||||
"{} test-color\nb {}\n",
|
"{} test-color\nb {}\n",
|
||||||
|
@ -1988,7 +1996,7 @@ fn test_ls_path() {
|
||||||
};
|
};
|
||||||
scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout);
|
scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout);
|
||||||
|
|
||||||
let expected_stdout = format!("{} {}\n", path, file1);
|
let expected_stdout = format!("{}\n{}\n", path, file1);
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg(file1)
|
.arg(file1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue