forked from Lutetium-Vanadium/requestty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.rs
80 lines (68 loc) · 2.15 KB
/
input.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
use requestty::prompt::style::Stylize;
use requestty::Question;
fn map_err<E>(_: E) {}
fn parse_col(s: &str) -> Result<(u8, u8, u8), ()> {
if !s.is_ascii() {
return Err(());
}
let s = s.trim();
if s.starts_with('#') {
let (r, g, b, mult) = if s.len() == 4 {
(1..2, 2..3, 3..4, 17)
} else if s.len() == 7 {
(1..3, 3..5, 5..7, 1)
} else {
return Err(());
};
Ok((
mult * u8::from_str_radix(&s[r], 16).map_err(map_err)?,
mult * u8::from_str_radix(&s[g], 16).map_err(map_err)?,
mult * u8::from_str_radix(&s[b], 16).map_err(map_err)?,
))
} else if s.starts_with("rgb(") && s.ends_with(')') {
let mut s = &s[4..(s.len() - 1)];
let r;
let g;
if let Some(pos) = s.find(',') {
r = s[..pos].trim().parse().map_err(map_err)?;
s = &s[(pos + 1)..];
} else {
return Err(());
}
if let Some(pos) = s.find(',') {
g = s[..pos].trim().parse().map_err(map_err)?;
s = &s[(pos + 1)..];
} else {
return Err(());
}
let b = s.trim().parse().map_err(map_err)?;
Ok((r, g, b))
} else {
Err(())
}
}
fn main() {
let questions = vec![
Question::input("first_name")
.message("What's your first name")
.build(),
Question::input("last_name")
.message("What's your last name")
.default("Doe")
.build(),
Question::input("fav_color")
.message("What's your favourite colour")
.validate_on_key(|ans, _| parse_col(ans).is_ok())
.validate(|ans, _| match parse_col(ans) {
Ok(_) => Ok(()),
Err(_) => Err("Please provide a valid css colour".into()),
})
.transform(|ans, _, backend| {
let (r, g, b) = parse_col(ans).unwrap();
backend.write_styled(&ans.rgb(r, g, b))?;
writeln!(backend)
})
.build(),
];
println!("{:#?}", requestty::prompt(questions));
}