mirror of
https://github.com/theryangeary/choose
synced 2024-11-22 19:03:05 +00:00
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.
This commit is contained in:
parent
5981201700
commit
76461e0a1d
2 changed files with 35 additions and 28 deletions
|
@ -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]
|
||||
|
|
10
src/main.rs
10
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),
|
||||
|
|
Loading…
Reference in a new issue