Better error message for MissingArgumentsForDecompression
[ouch.git] / src / dialogs.rs
blob2902a41423b804bc0dff0a0f59e193226f22ed7e
1 use std::io::{self, Write};
3 use colored::Colorize;
5 pub struct Confirmation<'a> {
6     pub prompt: &'a str,
7     pub placeholder: Option<&'a str>,
10 #[derive(Debug)]
11 pub struct Error;
13 impl<'a> Confirmation<'a> {
14     pub fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
15         Self {
16             prompt,
17             placeholder: pattern,
18         }
19     }
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),
26         };
28         loop {
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() {
37                 return Ok(true);
38             }
40             match trimmed_answer.to_ascii_lowercase().as_ref() {
41                 "y" | "yes" => return Ok(true),
42                 "n" | "no" => return Ok(false),
43                 _ => {}
44             }
45         }
46     }