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

nl: support --join-blank-lines over multiple files

This commit is contained in:
Daniel Hofstetter 2023-09-19 10:47:20 +02:00
parent 1107fadca9
commit 1a30a1b8b6
2 changed files with 28 additions and 4 deletions

View file

@ -303,15 +303,14 @@ pub fn uu_app() -> Command {
// nl implements the main functionality for an individual buffer. // nl implements the main functionality for an individual buffer.
fn nl<T: Read>(reader: &mut BufReader<T>, stats: &mut Stats, settings: &Settings) -> UResult<()> { fn nl<T: Read>(reader: &mut BufReader<T>, stats: &mut Stats, settings: &Settings) -> UResult<()> {
let mut current_numbering_style = &settings.body_numbering; let mut current_numbering_style = &settings.body_numbering;
let mut consecutive_empty_lines = stats.consecutive_empty_lines;
for line in reader.lines() { for line in reader.lines() {
let line = line.map_err_context(|| "could not read line".to_string())?; let line = line.map_err_context(|| "could not read line".to_string())?;
if line.is_empty() { if line.is_empty() {
consecutive_empty_lines += 1; stats.consecutive_empty_lines += 1;
} else { } else {
consecutive_empty_lines = 0; stats.consecutive_empty_lines = 0;
}; };
// FIXME section delimiters are hardcoded and settings.section_delimiter is ignored // FIXME section delimiters are hardcoded and settings.section_delimiter is ignored
@ -336,7 +335,7 @@ fn nl<T: Read>(reader: &mut BufReader<T>, stats: &mut Stats, settings: &Settings
// for numbering, and only number the last one // for numbering, and only number the last one
NumberingStyle::All NumberingStyle::All
if line.is_empty() if line.is_empty()
&& consecutive_empty_lines % settings.join_blank_lines != 0 => && stats.consecutive_empty_lines % settings.join_blank_lines != 0 =>
{ {
false false
} }

View file

@ -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] #[test]
fn test_join_blank_lines_zero() { fn test_join_blank_lines_zero() {
for arg in ["-l0", "--join-blank-lines=0"] { for arg in ["-l0", "--join-blank-lines=0"] {