pub struct Input<'a, T> { /* private fields */ }
Expand description
Renders an input prompt.
§Example
use dialoguer::Input;
fn main() {
let name: String = Input::new()
.with_prompt("Your name?")
.interact_text()
.unwrap();
println!("Your name is: {}", name);
}
It can also be used with turbofish notation:
use dialoguer::Input;
fn main() {
let name = Input::<String>::new()
.with_prompt("Your name?")
.interact_text()
.unwrap();
println!("Your name is: {}", name);
}
Implementations§
Source§impl<T> Input<'_, T>
impl<T> Input<'_, T>
Sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
Sets the input prompt.
Sourcepub fn with_post_completion_text<S: Into<String>>(
self,
post_completion_text: S,
) -> Self
pub fn with_post_completion_text<S: Into<String>>( self, post_completion_text: S, ) -> Self
Changes the prompt text to the post completion text after input is complete
Sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report the input value after interaction.
The default is to report the input value.
Sourcepub fn with_initial_text<S: Into<String>>(self, val: S) -> Self
pub fn with_initial_text<S: Into<String>>(self, val: S) -> Self
Sets initial text that user can accept or erase.
Sourcepub fn default(self, value: T) -> Self
pub fn default(self, value: T) -> 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 allow_empty(self, val: bool) -> Self
pub fn allow_empty(self, val: bool) -> Self
Enables or disables an empty input
By default, if there is no default value set for the input, the user must input a non-empty string.
Sourcepub fn show_default(self, val: bool) -> Self
pub fn show_default(self, val: bool) -> Self
Disables or enables the default value display.
The default behaviour is to append default
to the prompt to tell the
user what is the default value.
This method does not affect existence of default value, only its display in the prompt!
Source§impl<'a, T> Input<'a, T>
impl<'a, T> Input<'a, T>
Sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates an input prompt with a specific theme.
§Example
use dialoguer::{theme::ColorfulTheme, Input};
fn main() {
let name: String = Input::with_theme(&ColorfulTheme::default())
.interact()
.unwrap();
}
Source§impl<'a, T> Input<'a, T>where
T: 'a,
impl<'a, T> Input<'a, T>where
T: 'a,
Sourcepub fn validate_with<V>(self, validator: V) -> Self
pub fn validate_with<V>(self, validator: V) -> Self
Registers a validator.
§Example
use dialoguer::Input;
fn main() {
let mail: String = Input::new()
.with_prompt("Enter email")
.validate_with(|input: &String| -> Result<(), &str> {
if input.contains('@') {
Ok(())
} else {
Err("This is not a mail address")
}
})
.interact()
.unwrap();
}
Source§impl<T> Input<'_, T>
impl<T> Input<'_, T>
Sourcepub fn interact_text(self) -> Result<T>
pub fn interact_text(self) -> Result<T>
Sourcepub fn interact_text_on(self, term: &Term) -> Result<T>
pub fn interact_text_on(self, term: &Term) -> Result<T>
Like interact_text
but allows a specific terminal to be set.
Sourcepub fn interact(self) -> Result<T>
pub fn interact(self) -> Result<T>
Enables user interaction and returns the result.
Allows any characters as input, including e.g arrow keys.
Some of the keys might have undesired behavior.
For more limited version, see interact_text
.
If the user confirms the result is true
, false
otherwise.
The dialog is rendered on stderr.
Sourcepub fn interact_on(self, term: &Term) -> Result<T>
pub fn interact_on(self, term: &Term) -> Result<T>
Like interact
but allows a specific terminal to be set.