use std::io::{self, Write};
#[derive(Debug)]
pub struct Prompt;
impl Prompt {
pub fn confirm(prompt: &str, default: bool) -> io::Result<bool> {
let yes_no = if default { "[Y/n]" } else { "[y/N]" };
let full_prompt = format!("{} {} ", prompt, yes_no);
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(full_prompt.as_bytes())?;
handle.flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim().to_lowercase();
Ok(match input.as_str() {
"y" | "yes" => true,
"n" | "no" => false,
"" => default,
_ => false,
})
}
pub fn input(prompt: &str) -> io::Result<String> {
let full_prompt = format!("{} ", prompt);
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(full_prompt.as_bytes())?;
handle.flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
Ok(input.trim().to_string())
}
pub fn password(prompt: &str) -> io::Result<String> {
let full_prompt = format!("{} ", prompt);
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(full_prompt.as_bytes())?;
handle.flush()?;
let password = crate::os::read_password()?;
println!();
Ok(password)
}
pub fn text(prompt: &str) -> io::Result<String> {
Self::input(prompt)
}
}