forked from Lutetium-Vanadium/requestty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt-module.rs
103 lines (97 loc) · 3.79 KB
/
prompt-module.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use requestty::{Choice, DefaultSeparator, Question, Separator};
fn main() -> requestty::Result<()> {
let phone_validator = regex::RegexBuilder::new(r"^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$")
.case_insensitive(true)
.build()
.unwrap();
let answers = requestty::Answers::default();
// the prompt module can also be created with the `requestty::prompt_module` macro
let mut module = requestty::PromptModule::new(vec![
Question::confirm("to_be_delivered")
.message("Is this for delivery?")
.default(false)
.build(),
Question::input("phone")
.message("What's your phone number?")
.validate(|value, _| {
if phone_validator.is_match(value) {
Ok(())
} else {
Err("Please enter a valid phone number".into())
}
})
.build(),
Question::select("size")
.message("What size do you need?")
.choice("Large")
.choice("Medium")
.choice("Small")
.build(),
Question::int("quantity")
.message("How many do you need?")
.validate(|ans, _| {
if ans > 0 {
Ok(())
} else {
Err("You need to order at least one pizza".into())
}
})
.build(),
Question::confirm("custom_toppings")
.message("Do you want to customize the toppings?")
.default(false)
.build(),
Question::expand("toppings")
.message("What about the toppings?")
.when(|answers: &requestty::Answers| !answers["custom_toppings"].as_bool().unwrap())
.choice('p', "Pepperoni and cheese")
.choice('a', "All dressed")
.choice('w', "Hawaiian")
.build(),
Question::multi_select("toppings")
.message("Select toppings")
.when(|answers: &requestty::Answers| answers["custom_toppings"].as_bool().unwrap())
.separator(" = The Meats = ")
.choices(vec!["Pepperoni", "Ham", "Ground Meat", "Bacon"])
.separator(" = The Cheeses = ")
.choice_with_default("Mozzarella", true)
.choice("Cheddar")
.choices(vec![
Choice("Parmesan".into()),
Separator(" = The usual = ".into()),
"Mushroom".into(),
"Tomato".into(),
Separator(" = The extras = ".into()),
"Pineapple".into(),
"Olives".into(),
"Extra cheese".into(),
DefaultSeparator,
])
.build(),
Question::raw_select("beverage")
.message("You also get a free 2L beverage")
.choice("Pepsi")
.choice("7up")
.choice("Coke")
.build(),
Question::input("comments")
.message("Any comments on your purchase experience?")
.default("Nope, all good!")
.build(),
Question::select("prize")
.message("For leaving a comment, you get a freebie")
.choices(vec!["cake", "fries"])
.when(|answers: &requestty::Answers| {
return answers["comments"].as_string().unwrap() != "Nope, all good!";
})
.build(),
])
// you can use answers from before
.with_answers(answers);
// you can also prompt a single question, and get a mutable reference to its answer
if module.prompt()?.unwrap().as_bool().unwrap() {
println!("Delivery is guaranteed to be under 40 minutes");
}
println!("{:#?}", module.prompt_all()?);
Ok(())
}