From 76461e0a1dfdfe5d034a3f55d1aa75bc66698913 Mon Sep 17 00:00:00 2001 From: Ryan Geary Date: Thu, 13 Aug 2020 17:05:28 -0400 Subject: 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. --- src/choice.rs | 53 ++++++++++++++++++++++++++--------------------------- src/main.rs | 10 +++++++++- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/choice.rs b/src/choice.rs index 9df4580..2bd0194 100644 --- a/src/choice.rs +++ b/src/choice.rs @@ -35,10 +35,9 @@ impl Choice { } } - pub fn print_choice(&self, line: &String, config: &Config, handle: &mut W) { + pub fn print_choice(&self, line: &str, config: &Config, handle: &mut W) { if config.opt.character_wise { - let line_chars = line[0..line.len() - 1].chars(); - self.print_choice_generic(line_chars, config, handle); + self.print_choice_generic(line.chars(), config, handle); } else { let line_iter = config .separator @@ -507,42 +506,42 @@ mod tests { #[test] fn print_0_to_2_character_wise() { - test_fn(vec!["choose", "0:2", "-c"], "abcd\n", "abc"); + test_fn(vec!["choose", "0:2", "-c"], "abcd", "abc"); } #[test] fn print_2_to_end_character_wise() { - test_fn(vec!["choose", "2:", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "2:", "-c"], "abcd", "cd"); } #[test] fn print_start_to_2_character_wise() { - test_fn(vec!["choose", ":2", "-c"], "abcd\n", "abc"); + test_fn(vec!["choose", ":2", "-c"], "abcd", "abc"); } #[test] fn print_0_to_2_character_wise_exclusive() { - test_fn(vec!["choose", "0:2", "-c", "-x"], "abcd\n", "ab"); + test_fn(vec!["choose", "0:2", "-c", "-x"], "abcd", "ab"); } #[test] fn print_0_to_2_character_wise_with_output_delimeter() { - test_fn(vec!["choose", "0:2", "-c", "-o", ":"], "abcd\n", "a:b:c"); + test_fn(vec!["choose", "0:2", "-c", "-o", ":"], "abcd", "a:b:c"); } #[test] fn print_after_end_character_wise() { - test_fn(vec!["choose", "0:9", "-c"], "abcd\n", "abcd"); + test_fn(vec!["choose", "0:9", "-c"], "abcd", "abcd"); } #[test] fn print_2_to_0_character_wise() { - test_fn(vec!["choose", "2:0", "-c"], "abcd\n", "cba"); + test_fn(vec!["choose", "2:0", "-c"], "abcd", "cba"); } #[test] fn print_neg_2_to_end_character_wise() { - test_fn(vec!["choose", "-2:", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "-2:", "-c"], "abcd", "cd"); } #[test] @@ -780,42 +779,42 @@ mod tests { #[test] fn print_0_to_2_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "0..=2", "-c"], "abcd\n", "abc"); + test_fn(vec!["choose", "0..=2", "-c"], "abcd", "abc"); } #[test] fn print_2_to_end_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "2..=", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "2..=", "-c"], "abcd", "cd"); } #[test] fn print_start_to_2_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "..=2", "-c"], "abcd\n", "abc"); + test_fn(vec!["choose", "..=2", "-c"], "abcd", "abc"); } #[test] fn print_0_to_2_character_wise_exclusive_rust_syntax_inclusive() { - test_fn(vec!["choose", "0..=2", "-c", "-x"], "abcd\n", "abc"); + test_fn(vec!["choose", "0..=2", "-c", "-x"], "abcd", "abc"); } #[test] fn print_0_to_2_character_wise_with_output_delimeter_rust_syntax_inclusive() { - test_fn(vec!["choose", "0..=2", "-c", "-o", ":"], "abcd\n", "a:b:c"); + test_fn(vec!["choose", "0..=2", "-c", "-o", ":"], "abcd", "a:b:c"); } #[test] fn print_after_end_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "0..=9", "-c"], "abcd\n", "abcd"); + test_fn(vec!["choose", "0..=9", "-c"], "abcd", "abcd"); } #[test] fn print_2_to_0_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "2..=0", "-c"], "abcd\n", "cba"); + test_fn(vec!["choose", "2..=0", "-c"], "abcd", "cba"); } #[test] fn print_neg_2_to_end_character_wise_rust_syntax_inclusive() { - test_fn(vec!["choose", "-2..=", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "-2..=", "-c"], "abcd", "cd"); } #[test] @@ -1019,42 +1018,42 @@ mod tests { #[test] fn print_0_to_2_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "0..2", "-c"], "abcd\n", "ab"); + test_fn(vec!["choose", "0..2", "-c"], "abcd", "ab"); } #[test] fn print_2_to_end_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "2..", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "2..", "-c"], "abcd", "cd"); } #[test] fn print_start_to_2_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "..2", "-c"], "abcd\n", "ab"); + test_fn(vec!["choose", "..2", "-c"], "abcd", "ab"); } #[test] fn print_0_to_2_character_wise_exclusive_rust_syntax_exclusive() { - test_fn(vec!["choose", "0..2", "-c", "-x"], "abcd\n", "ab"); + test_fn(vec!["choose", "0..2", "-c", "-x"], "abcd", "ab"); } #[test] fn print_0_to_2_character_wise_with_output_delimeter_rust_syntax_exclusive() { - test_fn(vec!["choose", "0..2", "-c", "-o", ":"], "abcd\n", "a:b"); + test_fn(vec!["choose", "0..2", "-c", "-o", ":"], "abcd", "a:b"); } #[test] fn print_after_end_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "0..9", "-c"], "abcd\n", "abcd"); + test_fn(vec!["choose", "0..9", "-c"], "abcd", "abcd"); } #[test] fn print_2_to_0_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "2..0", "-c"], "abcd\n", "ba"); + test_fn(vec!["choose", "2..0", "-c"], "abcd", "ba"); } #[test] fn print_neg_2_to_end_character_wise_rust_syntax_exclusive() { - test_fn(vec!["choose", "-2..", "-c"], "abcd\n", "cd"); + test_fn(vec!["choose", "-2..", "-c"], "abcd", "cd"); } #[test] diff --git a/src/main.rs b/src/main.rs index 6146621..5672889 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,13 +52,21 @@ fn main_generic(opt: Opt, handle: &mut W) { while let Some(line) = reader.read_line(&mut buffer) { match line { Ok(l) => { + let l = if config.opt.character_wise || config.opt.field_separator.is_some() { + &l[0..l.len() - 1] + } else { + &l + }; + let choice_iter = &mut config.opt.choices.iter().peekable(); + while let Some(choice) = choice_iter.next() { - choice.print_choice(&l, &config, handle); + choice.print_choice(l, &config, handle); if choice_iter.peek().is_some() { handle.write_separator(&config); } } + match handle.write(b"\n") { Ok(_) => (), Err(e) => eprintln!("Failed to write to output: {}", e), -- cgit v1.2.3