Struct geekbar_dialoguer::Confirm
source · [−]pub struct Confirm<'a> { /* private fields */ }
Expand description
Renders a confirm prompt.
Example usage
use dialoguer::Confirm;
if Confirm::new().with_prompt("Do you want to continue?").interact()? {
println!("Looks like you want to continue");
} else {
println!("nevermind then :(");
}
Implementations
sourceimpl Confirm<'_>
impl Confirm<'_>
sourcepub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self
Sets the confirm prompt.
sourcepub fn report(&mut self, val: bool) -> &mut Self
pub fn report(&mut self, val: bool) -> &mut Self
Indicates whether or not to report the chosen selection after interaction.
The default is to report the chosen selection.
pub fn with_text(&mut self, text: &str) -> &mut Self
sourcepub fn wait_for_newline(&mut self, wait: bool) -> &mut Self
pub fn wait_for_newline(&mut self, wait: bool) -> &mut Self
Sets when to react to user input.
When false
(default), we check on each user keystroke immediately as
it is typed. Valid inputs can be one of ‘y’, ‘n’, or a newline to accept
the default.
When true
, the user must type their choice and hit the Enter key before
proceeding. Valid inputs can be “yes”, “no”, “y”, “n”, or an empty string
to accept the default.
sourcepub fn default(&mut self, val: bool) -> &mut Self
pub fn default(&mut self, val: bool) -> &mut Self
Sets a default.
Out of the box the prompt does not have a default and will continue to display until the user inputs something and hits enter. If a default is set the user can instead accept the default with enter.
sourcepub fn show_default(&mut self, val: bool) -> &mut Self
pub fn show_default(&mut self, val: bool) -> &mut Self
Disables or enables the default value display.
The default is to append the default value to the prompt to tell the user.
sourcepub fn interact(&self) -> Result<bool>
pub fn interact(&self) -> Result<bool>
Enables user interaction and returns the result.
The dialog is rendered on stderr.
Result contains bool
if user answered “yes” or “no” or default
(configured in default
if pushes enter.
This unlike interact_opt
does not allow to quit with ‘Esc’ or ‘q’.
sourcepub fn interact_opt(&self) -> Result<Option<bool>>
pub fn interact_opt(&self) -> Result<Option<bool>>
Enables user interaction and returns the result.
The dialog is rendered on stderr.
Result contains Some(bool)
if user answered “yes” or “no” or Some(default)
(configured in default
) if pushes enter,
or None
if user cancelled with ‘Esc’ or ‘q’.
sourcepub fn interact_on(&self, term: &Term) -> Result<bool>
pub fn interact_on(&self, term: &Term) -> Result<bool>
sourcepub fn interact_on_opt(&self, term: &Term) -> Result<Option<bool>>
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<bool>>
Like interact_opt
but allows a specific terminal to be set.
Examples
use dialoguer::Confirm;
use console::Term;
fn main() -> std::io::Result<()> {
let confirmation = Confirm::new()
.interact_on_opt(&Term::stdout())?;
match confirmation {
Some(answer) => println!("User answered {}", if answer { "yes" } else { "no " }),
None => println!("User did not answer")
}
Ok(())
}
sourceimpl<'a> Confirm<'a>
impl<'a> Confirm<'a>
sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates a confirm prompt with a specific theme.
Examples
use dialoguer::{
Confirm,
theme::ColorfulTheme
};
let proceed = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Do you wish to continue?")
.interact()?;