diff --git a/src/prompts/confirm.rs b/src/prompts/confirm.rs index 8ff126f..c3978b9 100644 --- a/src/prompts/confirm.rs +++ b/src/prompts/confirm.rs @@ -35,6 +35,7 @@ pub struct Confirm<'a> { show_default: bool, wait_for_newline: bool, theme: &'a dyn Theme, + show_hint: bool, } impl Default for Confirm<'static> { @@ -97,6 +98,14 @@ impl Confirm<'_> { self } + /// Disables or enables the hint display (e.g. `[y/n]`). + /// + /// The default is to show the hint. + pub fn show_hint(mut self, val: bool) -> Self { + self.show_hint = val; + self + } + /// Enables user interaction and returns the result. /// /// The dialog is rendered on stderr. @@ -163,7 +172,11 @@ impl Confirm<'_> { None }; - render.confirm_prompt(&self.prompt, default_if_show)?; + if self.show_hint { + render.confirm_prompt(&self.prompt, default_if_show)?; + } else { + render.confirm_prompt_no_hint(&self.prompt)?; + } term.hide_cursor()?; term.flush()?; @@ -260,6 +273,7 @@ impl<'a> Confirm<'a> { show_default: true, wait_for_newline: false, theme, + show_hint: true, } } } diff --git a/src/theme/colorful.rs b/src/theme/colorful.rs index 787ab7f..28ad8e9 100644 --- a/src/theme/colorful.rs +++ b/src/theme/colorful.rs @@ -173,6 +173,20 @@ impl Theme for ColorfulTheme { } } + /// Formats a confirm prompt without the hint ([y/n]). + fn format_confirm_prompt_no_hint(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result { + if !prompt.is_empty() { + write!( + f, + "{} {} {}", + &self.prompt_prefix, + self.prompt_style.apply_to(prompt), + &self.prompt_suffix + )?; + } + Ok(()) + } + /// Formats a confirm prompt after selection. fn format_confirm_prompt_selection( &self, diff --git a/src/theme/mod.rs b/src/theme/mod.rs index d22001c..2470364 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -45,6 +45,14 @@ pub trait Theme { Ok(()) } + /// Formats a confirm prompt without the hint ([y/n]). + fn format_confirm_prompt_no_hint(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result { + if !prompt.is_empty() { + write!(f, "{} ", prompt)?; + } + Ok(()) + } + /// Formats a confirm prompt after selection. fn format_confirm_prompt_selection( &self, diff --git a/src/theme/render.rs b/src/theme/render.rs index e6f3add..53ed833 100644 --- a/src/theme/render.rs +++ b/src/theme/render.rs @@ -91,6 +91,10 @@ impl<'a> TermThemeRenderer<'a> { self.write_formatted_str(|this, buf| this.theme.format_confirm_prompt(buf, prompt, default)) } + pub fn confirm_prompt_no_hint(&mut self, prompt: &str) -> Result { + self.write_formatted_str(|this, buf| this.theme.format_confirm_prompt_no_hint(buf, prompt)) + } + pub fn confirm_prompt_selection(&mut self, prompt: &str, sel: Option) -> Result { self.write_formatted_prompt(|this, buf| { this.theme.format_confirm_prompt_selection(buf, prompt, sel)