mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #5306 from cakebaker/nl_fix_output_order_if_stdin_and_files_are_mixed
nl: fix output order if stdin and files are mixed
This commit is contained in:
commit
4ca792d2ff
2 changed files with 20 additions and 13 deletions
|
@ -168,7 +168,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut read_stdin = false;
|
|
||||||
let files: Vec<String> = match matches.get_many::<String>(options::FILE) {
|
let files: Vec<String> = match matches.get_many::<String>(options::FILE) {
|
||||||
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
|
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
|
||||||
None => vec!["-".to_owned()],
|
None => vec!["-".to_owned()],
|
||||||
|
@ -178,21 +177,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
for file in &files {
|
for file in &files {
|
||||||
if file == "-" {
|
if file == "-" {
|
||||||
// If both file names and '-' are specified, we choose to treat first all
|
let mut buffer = BufReader::new(stdin());
|
||||||
// regular files, and then read from stdin last.
|
nl(&mut buffer, &mut stats, &settings)?;
|
||||||
read_stdin = true;
|
} else {
|
||||||
continue;
|
let path = Path::new(file);
|
||||||
|
let reader = File::open(path).map_err_context(|| file.to_string())?;
|
||||||
|
let mut buffer = BufReader::new(reader);
|
||||||
|
nl(&mut buffer, &mut stats, &settings)?;
|
||||||
}
|
}
|
||||||
let path = Path::new(file);
|
|
||||||
let reader = File::open(path).map_err_context(|| file.to_string())?;
|
|
||||||
let mut buffer = BufReader::new(reader);
|
|
||||||
nl(&mut buffer, &mut stats, &settings)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if read_stdin {
|
|
||||||
let mut buffer = BufReader::new(stdin());
|
|
||||||
nl(&mut buffer, &mut stats, &settings)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -349,6 +349,19 @@ fn test_default_body_numbering_multiple_files() {
|
||||||
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
|
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_default_body_numbering_multiple_files_and_stdin() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
||||||
|
at.write("a.txt", "a");
|
||||||
|
at.write("c.txt", "c");
|
||||||
|
|
||||||
|
ucmd.args(&["a.txt", "-", "c.txt"])
|
||||||
|
.pipe_in("b")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_body_numbering_all_lines_without_delimiter() {
|
fn test_body_numbering_all_lines_without_delimiter() {
|
||||||
for arg in ["-ba", "--body-numbering=a"] {
|
for arg in ["-ba", "--body-numbering=a"] {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue