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:
parent
1107fadca9
commit
1a30a1b8b6
2 changed files with 28 additions and 4 deletions
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"] {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue