Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Tools/langtool/src/util.rs
3658 views
1
use std::io::{self, Write};
2
3
pub fn ask_yes_no(question: &str) -> bool {
4
loop {
5
println!("{} (y/n): ", question);
6
7
let mut input = String::new();
8
io::stdin()
9
.read_line(&mut input)
10
.expect("Failed to read line");
11
12
match input.trim().to_lowercase().as_str() {
13
"y" | "yes" => return true,
14
"n" | "no" => return false,
15
_ => println!("Please enter 'y' or 'n'"),
16
}
17
}
18
}
19
20
pub fn ask_letter(question: &str, allowed_chars: &str) -> char {
21
loop {
22
println!("{question} ({allowed_chars}): ");
23
let _ = io::stdout().flush();
24
25
let mut input = String::new();
26
io::stdin()
27
.read_line(&mut input)
28
.expect("Failed to read line");
29
30
match input
31
.trim()
32
.to_lowercase()
33
.as_str()
34
.chars()
35
.into_iter()
36
.next()
37
{
38
Some(c) => {
39
if allowed_chars.contains(c) {
40
return c;
41
}
42
}
43
_ => println!("Please enter a character"),
44
}
45
}
46
}
47
48