Skip to content

Commit

Permalink
Fix join in streamed --lines
Browse files Browse the repository at this point in the history
  • Loading branch information
riquito committed May 21, 2022
1 parent 9f2abaa commit 9c2f16f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/bin/tuc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,12 @@ fn read_and_cut_lines(
// we exhausted the use of that bound, move on
bounds_idx += 1;
add_newline_next = false;

// if opt.join and it was not the last matching bound
if opt.join && bounds_idx != opt.bounds.0.len() {
stdout.write_all(&[opt.eol as u8])?;
}

continue; // let's see if the next bound matches too
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ fn it_cuts_on_lines() {

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd
.args(&["--lines", "3,1"])
.write_stdin("a\nb\nc")
.assert();

assert.success().stdout("ca");

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd
.args(&["--lines", "2:3"])
.write_stdin("a\nb\nc")
Expand Down Expand Up @@ -199,3 +208,24 @@ fn it_join_fields() {

assert.success().stdout("a-c\n");
}

#[test]
fn it_join_lines() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd
.args(&["-l", "1,3", "-j"])
.write_stdin("a\nb\nc")
.assert();

assert.success().stdout("a\nc");

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();

let assert = cmd
.args(&["-l", "3,1", "-j"])
.write_stdin("a\nb\nc")
.assert();

assert.success().stdout("c\na");
}

0 comments on commit 9c2f16f

Please sign in to comment.