mirror of
https://github.com/theryangeary/choose
synced 2024-11-26 21:00:17 +00:00
Unify enumerating lines in print_choice
This commit is contained in:
parent
18dcf845fb
commit
5a5a77e88f
1 changed files with 10 additions and 12 deletions
22
src/main.rs
22
src/main.rs
|
@ -16,21 +16,23 @@ enum Choice {
|
|||
|
||||
impl Choice {
|
||||
fn print_choice(&self, line: &String, opt: &Opt) {
|
||||
let words: Vec<&str> = line.split_whitespace().collect();
|
||||
let words = line.split_whitespace().into_iter().enumerate();
|
||||
|
||||
match self {
|
||||
Choice::Field(i) => {
|
||||
if *i < words.len().try_into().unwrap() {
|
||||
print!("{} ", words[*i as usize]);
|
||||
}
|
||||
},
|
||||
print!(
|
||||
"{} ",
|
||||
words
|
||||
.filter(|x| x.0 == *i as usize)
|
||||
.map(|x| x.1)
|
||||
.collect::<String>()
|
||||
);
|
||||
}
|
||||
Choice::FieldRange(r) => match r {
|
||||
(None, None) => print!("{}", words.into_iter().collect::<String>()),
|
||||
(None, None) => print!("{}", words.map(|x| x.1).collect::<String>()),
|
||||
(Some(start), None) => print!(
|
||||
"{} ",
|
||||
words
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|x| x.0 >= (*start).try_into().unwrap())
|
||||
.map(|x| x.1)
|
||||
.collect::<Vec<&str>>()
|
||||
|
@ -45,8 +47,6 @@ impl Choice {
|
|||
print!(
|
||||
"{} ",
|
||||
words
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|x| x.0 < e)
|
||||
.map(|x| x.1)
|
||||
.collect::<Vec<&str>>()
|
||||
|
@ -62,8 +62,6 @@ impl Choice {
|
|||
print!(
|
||||
"{} ",
|
||||
words
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap())
|
||||
.map(|x| x.1)
|
||||
.collect::<Vec<&str>>()
|
||||
|
|
Loading…
Reference in a new issue