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
use std::{io, io::prelude::*};
#[derive(Debug, Clone, Default)]
pub struct Input<'a> {
msg: Option<&'a str>,
err_msg: Option<&'a str>,
}
impl<'a> Input<'a> {
pub const fn new() -> Self { Input { msg: None, err_msg: None } }
pub fn with_msg(&mut self, msg: &'a str) -> &mut Self {
self.msg = Some(msg);
self
}
pub fn with_err_msg(&mut self, err_msg: &'a str) -> &mut Self {
self.err_msg = Some(err_msg);
self
}
fn get_input(&self) -> Option<String> {
if let Some(msg) = self.msg {
print!("{}", msg);
io::stdout().lock().flush().unwrap();
}
let mut line = String::new();
match io::stdin().lock().read_line(&mut line) {
Ok(_) => {},
Err(err) => {
if let Some(err_msg) = self.err_msg {
eprintln!("{}: {}", err_msg, err);
} else {
eprintln!("{}", err);
}
return None;
},
};
Some(line)
}
pub fn get(&self) -> Option<String> {
match self.get_input() {
Some(input) => Some(input.trim().to_string()),
None => None,
}
}
pub fn is_affirmative(&self) -> bool {
if let Some(input) = self.get_input() {
let input = input.trim().to_uppercase();
input == "Y" || input == "YES" || input == "1"
} else {
false
}
}
}