Skip to content

Commit

Permalink
nl: support --join-blank-lines over multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Sep 19, 2023
1 parent 1107fad commit 1a30a1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/uu/nl/src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,14 @@ pub fn uu_app() -> Command {
// nl implements the main functionality for an individual buffer.
fn nl<T: Read>(reader: &mut BufReader<T>, 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
Expand All @@ -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
NumberingStyle::All
if line.is_empty()
&& consecutive_empty_lines % settings.join_blank_lines != 0 =>
&& stats.consecutive_empty_lines % settings.join_blank_lines != 0 =>
{
false
}
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"] {
Expand Down

0 comments on commit 1a30a1b

Please sign in to comment.