From 1a30a1b8b656afe0deb674a4b848f7b311a18c81 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 19 Sep 2023 10:47:20 +0200 Subject: [PATCH] nl: support --join-blank-lines over multiple files --- src/uu/nl/src/nl.rs | 7 +++---- tests/by-util/test_nl.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index 356882237..fef9c030a 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -303,15 +303,14 @@ pub fn uu_app() -> Command { // nl implements the main functionality for an individual buffer. fn nl(reader: &mut BufReader, stats: &mut Stats, settings: &Settings) -> UResult<()> { let mut current_numbering_style = &settings.body_numbering; - let mut consecutive_empty_lines = stats.consecutive_empty_lines; for line in reader.lines() { let line = line.map_err_context(|| "could not read line".to_string())?; if line.is_empty() { - consecutive_empty_lines += 1; + stats.consecutive_empty_lines += 1; } else { - consecutive_empty_lines = 0; + stats.consecutive_empty_lines = 0; }; // FIXME section delimiters are hardcoded and settings.section_delimiter is ignored @@ -336,7 +335,7 @@ fn nl(reader: &mut BufReader, stats: &mut Stats, settings: &Settings // for numbering, and only number the last one NumberingStyle::All if line.is_empty() - && consecutive_empty_lines % settings.join_blank_lines != 0 => + && stats.consecutive_empty_lines % settings.join_blank_lines != 0 => { false } diff --git a/tests/by-util/test_nl.rs b/tests/by-util/test_nl.rs index 336ab4c29..b008c61de 100644 --- a/tests/by-util/test_nl.rs +++ b/tests/by-util/test_nl.rs @@ -284,6 +284,31 @@ fn test_join_blank_lines() { } } +#[test] +fn test_join_blank_lines_multiple_files() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("a.txt", "\n\n"); + at.write("b.txt", "\n\n"); + at.write("c.txt", "\n\n"); + + for arg in ["-l3", "--join-blank-lines=3"] { + scene + .ucmd() + .args(&[arg, "--body-numbering=a", "a.txt", "b.txt", "c.txt"]) + .succeeds() + .stdout_is(concat!( + " \n", + " \n", + " 1\t\n", + " \n", + " \n", + " 2\t\n", + )); + } +} + #[test] fn test_join_blank_lines_zero() { for arg in ["-l0", "--join-blank-lines=0"] {