1 use std::io::{self, Write};
5 pub struct Confirmation<'a> {
7 pub placeholder: Option<&'a str>,
13 impl<'a> Confirmation<'a> {
14 pub fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
21 pub fn ask(&self, substitute: Option<&'a str>) -> crate::Result<bool> {
22 let message = match (self.placeholder, substitute) {
23 (None, _) => self.prompt.into(),
24 (Some(_), None) => return Err(crate::Error::InternalError),
25 (Some(placeholder), Some(subs)) => self.prompt.replace(placeholder, subs),
29 print!("{} [{}/{}] ", message, "Y".bright_green(), "n".bright_red());
30 io::stdout().flush()?;
32 let mut answer = String::new();
33 io::stdin().read_line(&mut answer)?;
34 let trimmed_answer = answer.trim();
36 if trimmed_answer.is_empty() {
40 match trimmed_answer.to_ascii_lowercase().as_ref() {
41 "y" | "yes" => return Ok(true),
42 "n" | "no" => return Ok(false),