File: multiselect.rs

package info (click to toggle)
rust-inquire 0.9.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,956 kB
  • sloc: makefile: 2; sh: 1
file content (44 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download | duplicates (2)
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"),
    }
}