forked from Orchid/orchid
Preparation for sharing
- rustfmt - clippy - comments - README
This commit is contained in:
13
src/cli.rs
13
src/cli.rs
@@ -1,19 +1,22 @@
|
||||
use std::{fmt::Display, io::{stdin, BufRead, stdout, Write}};
|
||||
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>
|
||||
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}
|
||||
if input.is_empty() {
|
||||
return default;
|
||||
}
|
||||
match try_cast(input) {
|
||||
Ok(t) => return t,
|
||||
Err(e) => println!("Error: {e}")
|
||||
Err(e) => println!("Error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user