diff --git a/src/choice.rs b/src/choice.rs index 7cf0980..e6dcf59 100644 --- a/src/choice.rs +++ b/src/choice.rs @@ -78,8 +78,6 @@ mod tests { //} } - - } use regex::Regex; @@ -125,6 +123,10 @@ pub enum Choice { impl Choice { pub fn print_choice(&self, line: &String, opt: &Opt) { + print!("{}", self.get_choice_slice(line, opt).join(" ")); + } + + fn get_choice_slice<'a>(&self, line: &'a String, opt: &Opt) -> Vec<&'a str> { let re = Regex::new(match &opt.field_separator { Some(s) => s, None => "[[:space:]]", @@ -142,39 +144,26 @@ impl Choice { .enumerate(); match self { - Choice::Field(i) => { - print!( - "{} ", - words - .filter(|x| x.0 == *i as usize) - .map(|x| x.1) - .collect::() - ); - } + Choice::Field(i) => words + .filter(|x| x.0 == *i as usize) + .map(|x| x.1) + .collect::>(), Choice::FieldRange(r) => match r { - (None, None) => print!("{}", words.map(|x| x.1).collect::()), - (Some(start), None) => print!( - "{} ", - words - .filter(|x| x.0 >= (*start).try_into().unwrap()) - .map(|x| x.1) - .collect::>() - .join(" ") - ), + (None, None) => words.map(|x| x.1).collect::>(), + (Some(start), None) => words + .filter(|x| x.0 >= (*start).try_into().unwrap()) + .map(|x| x.1) + .collect::>(), (None, Some(end)) => { let e: usize = if opt.inclusive { (end + 1).try_into().unwrap() } else { (*end).try_into().unwrap() }; - print!( - "{} ", - words - .filter(|x| x.0 < e) - .map(|x| x.1) - .collect::>() - .join(" ") - ) + words + .filter(|x| x.0 < e) + .map(|x| x.1) + .collect::>() } (Some(start), Some(end)) => { let e: usize = if opt.inclusive { @@ -182,17 +171,13 @@ impl Choice { } else { (*end).try_into().unwrap() }; - print!( - "{} ", - words - .filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap()) - .map(|x| x.1) - .collect::>() - .join(" ") - ) + words + .filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap()) + .map(|x| x.1) + .collect::>() } }, - }; + } } pub fn parse_choice(src: &str) -> Result {