use std::io::{BufRead, Write};
use degenbot_cli_core::Prompter;
#[derive(Debug, Default, Clone, Copy)]
pub struct ConsolePrompter;
impl ConsolePrompter {
#[must_use]
pub const fn new() -> Self {
Self
}
}
impl Prompter for ConsolePrompter {
fn confirm(&self, message: &str, default: bool) -> bool {
let stdin = std::io::stdin();
let mut reader = stdin.lock();
let stdout = std::io::stdout();
let mut writer = stdout.lock();
confirm_with(&mut reader, &mut writer, message, default)
}
}
#[must_use]
pub fn confirm_with<R: BufRead, W: Write>(
reader: &mut R,
writer: &mut W,
message: &str,
default: bool,
) -> bool {
let suffix = if default { "[Y/n]" } else { "[y/N]" };
let _ = write!(writer, "{message} {suffix} ");
let _ = writer.flush();
let mut line = String::new();
match reader.read_line(&mut line) {
Ok(0) | Err(_) => false,
Ok(_) => match line.trim().to_ascii_lowercase().as_str() {
"" => default,
"y" | "yes" => true,
_ => false,
},
}
}