diff --git a/src/dialogs.rs b/src/dialogs.rs deleted file mode 100644 index dbf6a73..0000000 --- a/src/dialogs.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! Pretty (and colored) dialog for asking [Y/n] for the end user. -//! -//! Example: -//! "Do you want to overwrite 'archive.tar.gz'? [Y/n]" - -use std::{ - borrow::Cow, - io::{self, Write}, -}; - -use crate::utils::colors; - -/// Confirmation dialog for end user with [Y/n] question. -/// -/// If the placeholder is found in the prompt text, it will be replaced to form the final message. -pub struct Confirmation<'a> { - /// The message to be displayed with the placeholder text in it. - /// e.g.: "Do you want to overwrite 'FILE'?" - pub prompt: &'a str, - - /// The placeholder text that will be replaced in the `ask` function: - /// e.g.: Some("FILE") - pub placeholder: Option<&'a str>, -} - -impl<'a> Confirmation<'a> { - /// Creates a new Confirmation. - pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { - Self { prompt, placeholder: pattern } - } - - /// Creates user message and receives a boolean input to be used on the program - pub fn ask(&self, substitute: Option<&'a str>) -> crate::Result { - let message = match (self.placeholder, substitute) { - (None, _) => Cow::Borrowed(self.prompt), - (Some(_), None) => unreachable!("dev error, should be reported, we checked this won't happen"), - (Some(placeholder), Some(subs)) => Cow::Owned(self.prompt.replace(placeholder, subs)), - }; - - // Ask the same question to end while no valid answers are given - loop { - print!("{} [{}Y{}/{}n{}] ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET); - io::stdout().flush()?; - - let mut answer = String::new(); - io::stdin().read_line(&mut answer)?; - - answer.make_ascii_lowercase(); - match answer.trim() { - "" | "y" | "yes" => return Ok(true), - "n" | "no" => return Ok(false), - _ => continue, // Try again - } - } - } -} diff --git a/src/lib.rs b/src/lib.rs index 137775e..5644bf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,6 @@ pub mod macros; pub mod archive; pub mod cli; pub mod commands; -pub mod dialogs; pub mod error; pub mod extension; pub mod list; diff --git a/src/utils/question.rs b/src/utils/question.rs index 0cf779e..01bff85 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -1,13 +1,20 @@ //! Utils related to asking [Y/n] questions to the user. +//! +//! Example: +//! "Do you want to overwrite 'archive.tar.gz'? [Y/n]" -use std::path::Path; +use std::{ + borrow::Cow, + io::{self, Write}, + path::Path, +}; use fs_err as fs; use super::{strip_cur_dir, to_utf}; use crate::{ - dialogs::Confirmation, error::{Error, Result}, + utils::colors, }; #[derive(Debug, PartialEq, Clone, Copy)] @@ -69,3 +76,48 @@ pub fn user_wants_to_continue_decompressing(path: &Path, question_policy: Questi } } } + +/// Confirmation dialog for end user with [Y/n] question. +/// +/// If the placeholder is found in the prompt text, it will be replaced to form the final message. +pub struct Confirmation<'a> { + /// The message to be displayed with the placeholder text in it. + /// e.g.: "Do you want to overwrite 'FILE'?" + pub prompt: &'a str, + + /// The placeholder text that will be replaced in the `ask` function: + /// e.g.: Some("FILE") + pub placeholder: Option<&'a str>, +} + +impl<'a> Confirmation<'a> { + /// Creates a new Confirmation. + pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { + Self { prompt, placeholder: pattern } + } + + /// Creates user message and receives a boolean input to be used on the program + pub fn ask(&self, substitute: Option<&'a str>) -> crate::Result { + let message = match (self.placeholder, substitute) { + (None, _) => Cow::Borrowed(self.prompt), + (Some(_), None) => unreachable!("dev error, should be reported, we checked this won't happen"), + (Some(placeholder), Some(subs)) => Cow::Owned(self.prompt.replace(placeholder, subs)), + }; + + // Ask the same question to end while no valid answers are given + loop { + print!("{} [{}Y{}/{}n{}] ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET); + io::stdout().flush()?; + + let mut answer = String::new(); + io::stdin().read_line(&mut answer)?; + + answer.make_ascii_lowercase(); + match answer.trim() { + "" | "y" | "yes" => return Ok(true), + "n" | "no" => return Ok(false), + _ => continue, // Try again + } + } + } +}