forked from Orchid/orchid
23 lines
543 B
Rust
23 lines
543 B
Rust
use std::fmt::Display;
|
|
use std::io::{stdin, stdout, BufRead, Write};
|
|
|
|
pub fn prompt<T: Display, E: Display>(
|
|
prompt: &str,
|
|
default: T,
|
|
mut try_cast: impl FnMut(String) -> Result<T, E>,
|
|
) -> T {
|
|
loop {
|
|
print!("{prompt} ({default}): ");
|
|
stdout().lock().flush().unwrap();
|
|
let mut input = String::with_capacity(100);
|
|
stdin().lock().read_line(&mut input).unwrap();
|
|
if input.is_empty() {
|
|
return default;
|
|
}
|
|
match try_cast(input) {
|
|
Ok(t) => return t,
|
|
Err(e) => println!("Error: {e}"),
|
|
}
|
|
}
|
|
}
|