forked from mikaelmello/inquire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiselect.rs
44 lines (37 loc) · 1.14 KB
/
multiselect.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use inquire::{
formatter::MultiOptionFormatter, list_option::ListOption, validator::Validation, MultiSelect,
};
fn main() {
let options = vec![
"Banana",
"Apple",
"Strawberry",
"Grapes",
"Lemon",
"Tangerine",
"Watermelon",
"Orange",
"Pear",
"Avocado",
"Pineapple",
];
let validator = |a: &[ListOption<&&str>]| {
if a.len() < 2 {
return Ok(Validation::Invalid("This list is too small!".into()));
}
let x = a.iter().any(|o| *o.value == "Pineapple");
match x {
true => Ok(Validation::Valid),
false => Ok(Validation::Invalid("Remember to buy pineapples".into())),
}
};
let formatter: MultiOptionFormatter<&str> = &|a| format!("{} different fruits", a.len());
let ans = MultiSelect::new("Select the fruits for your shopping list:", options)
.with_validator(validator)
.with_formatter(formatter)
.prompt();
match ans {
Ok(_) => println!("I'll get right on it"),
Err(_) => println!("The shopping list could not be processed"),
}
}