input/
input.rs

1use dialoguer::{theme::ColorfulTheme, Input};
2
3fn main() {
4    let input: String = Input::with_theme(&ColorfulTheme::default())
5        .with_prompt("Your name")
6        .interact_text()
7        .unwrap();
8
9    println!("Hello {}!", input);
10
11    let mail: String = Input::with_theme(&ColorfulTheme::default())
12        .with_prompt("Your email")
13        .validate_with({
14            let mut force = None;
15            move |input: &String| -> Result<(), &str> {
16                if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17                    Ok(())
18                } else {
19                    force = Some(input.clone());
20                    Err("This is not a mail address; type the same value again to force use")
21                }
22            }
23        })
24        .interact_text()
25        .unwrap();
26
27    println!("Email: {}", mail);
28
29    let mail: String = Input::with_theme(&ColorfulTheme::default())
30        .with_prompt("Your planet")
31        .default("Earth".to_string())
32        .interact_text()
33        .unwrap();
34
35    println!("Planet: {}", mail);
36
37    let mail: String = Input::with_theme(&ColorfulTheme::default())
38        .with_prompt("Your galaxy")
39        .with_initial_text("Milky Way".to_string())
40        .interact_text()
41        .unwrap();
42
43    println!("Galaxy: {}", mail);
44}