Skip to content

Commit 76461e0

Browse files
committed
Fix blank line getting printed with field_separator
This changes the onus of removing trailing newlines when it matters to the caller to print_choice.
1 parent 5981201 commit 76461e0

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/choice.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ impl Choice {
3535
}
3636
}
3737

38-
pub fn print_choice<W: WriteReceiver>(&self, line: &String, config: &Config, handle: &mut W) {
38+
pub fn print_choice<W: WriteReceiver>(&self, line: &str, config: &Config, handle: &mut W) {
3939
if config.opt.character_wise {
40-
let line_chars = line[0..line.len() - 1].chars();
41-
self.print_choice_generic(line_chars, config, handle);
40+
self.print_choice_generic(line.chars(), config, handle);
4241
} else {
4342
let line_iter = config
4443
.separator
@@ -507,42 +506,42 @@ mod tests {
507506

508507
#[test]
509508
fn print_0_to_2_character_wise() {
510-
test_fn(vec!["choose", "0:2", "-c"], "abcd\n", "abc");
509+
test_fn(vec!["choose", "0:2", "-c"], "abcd", "abc");
511510
}
512511

513512
#[test]
514513
fn print_2_to_end_character_wise() {
515-
test_fn(vec!["choose", "2:", "-c"], "abcd\n", "cd");
514+
test_fn(vec!["choose", "2:", "-c"], "abcd", "cd");
516515
}
517516

518517
#[test]
519518
fn print_start_to_2_character_wise() {
520-
test_fn(vec!["choose", ":2", "-c"], "abcd\n", "abc");
519+
test_fn(vec!["choose", ":2", "-c"], "abcd", "abc");
521520
}
522521

523522
#[test]
524523
fn print_0_to_2_character_wise_exclusive() {
525-
test_fn(vec!["choose", "0:2", "-c", "-x"], "abcd\n", "ab");
524+
test_fn(vec!["choose", "0:2", "-c", "-x"], "abcd", "ab");
526525
}
527526

528527
#[test]
529528
fn print_0_to_2_character_wise_with_output_delimeter() {
530-
test_fn(vec!["choose", "0:2", "-c", "-o", ":"], "abcd\n", "a:b:c");
529+
test_fn(vec!["choose", "0:2", "-c", "-o", ":"], "abcd", "a:b:c");
531530
}
532531

533532
#[test]
534533
fn print_after_end_character_wise() {
535-
test_fn(vec!["choose", "0:9", "-c"], "abcd\n", "abcd");
534+
test_fn(vec!["choose", "0:9", "-c"], "abcd", "abcd");
536535
}
537536

538537
#[test]
539538
fn print_2_to_0_character_wise() {
540-
test_fn(vec!["choose", "2:0", "-c"], "abcd\n", "cba");
539+
test_fn(vec!["choose", "2:0", "-c"], "abcd", "cba");
541540
}
542541

543542
#[test]
544543
fn print_neg_2_to_end_character_wise() {
545-
test_fn(vec!["choose", "-2:", "-c"], "abcd\n", "cd");
544+
test_fn(vec!["choose", "-2:", "-c"], "abcd", "cd");
546545
}
547546

548547
#[test]
@@ -780,42 +779,42 @@ mod tests {
780779

781780
#[test]
782781
fn print_0_to_2_character_wise_rust_syntax_inclusive() {
783-
test_fn(vec!["choose", "0..=2", "-c"], "abcd\n", "abc");
782+
test_fn(vec!["choose", "0..=2", "-c"], "abcd", "abc");
784783
}
785784

786785
#[test]
787786
fn print_2_to_end_character_wise_rust_syntax_inclusive() {
788-
test_fn(vec!["choose", "2..=", "-c"], "abcd\n", "cd");
787+
test_fn(vec!["choose", "2..=", "-c"], "abcd", "cd");
789788
}
790789

791790
#[test]
792791
fn print_start_to_2_character_wise_rust_syntax_inclusive() {
793-
test_fn(vec!["choose", "..=2", "-c"], "abcd\n", "abc");
792+
test_fn(vec!["choose", "..=2", "-c"], "abcd", "abc");
794793
}
795794

796795
#[test]
797796
fn print_0_to_2_character_wise_exclusive_rust_syntax_inclusive() {
798-
test_fn(vec!["choose", "0..=2", "-c", "-x"], "abcd\n", "abc");
797+
test_fn(vec!["choose", "0..=2", "-c", "-x"], "abcd", "abc");
799798
}
800799

801800
#[test]
802801
fn print_0_to_2_character_wise_with_output_delimeter_rust_syntax_inclusive() {
803-
test_fn(vec!["choose", "0..=2", "-c", "-o", ":"], "abcd\n", "a:b:c");
802+
test_fn(vec!["choose", "0..=2", "-c", "-o", ":"], "abcd", "a:b:c");
804803
}
805804

806805
#[test]
807806
fn print_after_end_character_wise_rust_syntax_inclusive() {
808-
test_fn(vec!["choose", "0..=9", "-c"], "abcd\n", "abcd");
807+
test_fn(vec!["choose", "0..=9", "-c"], "abcd", "abcd");
809808
}
810809

811810
#[test]
812811
fn print_2_to_0_character_wise_rust_syntax_inclusive() {
813-
test_fn(vec!["choose", "2..=0", "-c"], "abcd\n", "cba");
812+
test_fn(vec!["choose", "2..=0", "-c"], "abcd", "cba");
814813
}
815814

816815
#[test]
817816
fn print_neg_2_to_end_character_wise_rust_syntax_inclusive() {
818-
test_fn(vec!["choose", "-2..=", "-c"], "abcd\n", "cd");
817+
test_fn(vec!["choose", "-2..=", "-c"], "abcd", "cd");
819818
}
820819

821820
#[test]
@@ -1019,42 +1018,42 @@ mod tests {
10191018

10201019
#[test]
10211020
fn print_0_to_2_character_wise_rust_syntax_exclusive() {
1022-
test_fn(vec!["choose", "0..2", "-c"], "abcd\n", "ab");
1021+
test_fn(vec!["choose", "0..2", "-c"], "abcd", "ab");
10231022
}
10241023

10251024
#[test]
10261025
fn print_2_to_end_character_wise_rust_syntax_exclusive() {
1027-
test_fn(vec!["choose", "2..", "-c"], "abcd\n", "cd");
1026+
test_fn(vec!["choose", "2..", "-c"], "abcd", "cd");
10281027
}
10291028

10301029
#[test]
10311030
fn print_start_to_2_character_wise_rust_syntax_exclusive() {
1032-
test_fn(vec!["choose", "..2", "-c"], "abcd\n", "ab");
1031+
test_fn(vec!["choose", "..2", "-c"], "abcd", "ab");
10331032
}
10341033

10351034
#[test]
10361035
fn print_0_to_2_character_wise_exclusive_rust_syntax_exclusive() {
1037-
test_fn(vec!["choose", "0..2", "-c", "-x"], "abcd\n", "ab");
1036+
test_fn(vec!["choose", "0..2", "-c", "-x"], "abcd", "ab");
10381037
}
10391038

10401039
#[test]
10411040
fn print_0_to_2_character_wise_with_output_delimeter_rust_syntax_exclusive() {
1042-
test_fn(vec!["choose", "0..2", "-c", "-o", ":"], "abcd\n", "a:b");
1041+
test_fn(vec!["choose", "0..2", "-c", "-o", ":"], "abcd", "a:b");
10431042
}
10441043

10451044
#[test]
10461045
fn print_after_end_character_wise_rust_syntax_exclusive() {
1047-
test_fn(vec!["choose", "0..9", "-c"], "abcd\n", "abcd");
1046+
test_fn(vec!["choose", "0..9", "-c"], "abcd", "abcd");
10481047
}
10491048

10501049
#[test]
10511050
fn print_2_to_0_character_wise_rust_syntax_exclusive() {
1052-
test_fn(vec!["choose", "2..0", "-c"], "abcd\n", "ba");
1051+
test_fn(vec!["choose", "2..0", "-c"], "abcd", "ba");
10531052
}
10541053

10551054
#[test]
10561055
fn print_neg_2_to_end_character_wise_rust_syntax_exclusive() {
1057-
test_fn(vec!["choose", "-2..", "-c"], "abcd\n", "cd");
1056+
test_fn(vec!["choose", "-2..", "-c"], "abcd", "cd");
10581057
}
10591058

10601059
#[test]

src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,21 @@ fn main_generic<W: WriteReceiver>(opt: Opt, handle: &mut W) {
5252
while let Some(line) = reader.read_line(&mut buffer) {
5353
match line {
5454
Ok(l) => {
55+
let l = if config.opt.character_wise || config.opt.field_separator.is_some() {
56+
&l[0..l.len() - 1]
57+
} else {
58+
&l
59+
};
60+
5561
let choice_iter = &mut config.opt.choices.iter().peekable();
62+
5663
while let Some(choice) = choice_iter.next() {
57-
choice.print_choice(&l, &config, handle);
64+
choice.print_choice(l, &config, handle);
5865
if choice_iter.peek().is_some() {
5966
handle.write_separator(&config);
6067
}
6168
}
69+
6270
match handle.write(b"\n") {
6371
Ok(_) => (),
6472
Err(e) => eprintln!("Failed to write to output: {}", e),

0 commit comments

Comments
 (0)