Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/prompts/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -260,6 +273,7 @@ impl<'a> Confirm<'a> {
show_default: true,
wait_for_newline: false,
theme,
show_hint: true,
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/theme/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/theme/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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<bool>) -> Result {
self.write_formatted_prompt(|this, buf| {
this.theme.format_confirm_prompt_selection(buf, prompt, sel)
Expand Down