summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2020-08-13 17:05:28 -0400
committerRyan Geary <rtgnj42@gmail.com>2020-08-13 17:43:16 -0400
commit76461e0a1dfdfe5d034a3f55d1aa75bc66698913 (patch)
tree914ff57c122627f4d27bd14c9d272e81558f632f
parent5981201700314b2010e69143ed075cbae6489012 (diff)
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.
-rw-r--r--src/choice.rs53
-rw-r--r--src/main.rs10
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<W: WriteReceiver>(&self, line: &String, config: &Config, handle: &mut W) {
+ pub fn print_choice<W: WriteReceiver>(&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<W: WriteReceiver>(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),