pub struct Input<'a, T> { /* private fields */ }
Expand description
Renders an input prompt.
Example usage
use dialoguer::Input;
let input : String = Input::new()
.with_prompt("Tea or coffee?")
.with_initial_text("Yes")
.default("No".into())
.interact_text()?;
It can also be used with turbofish notation:
let input = Input::<String>::new()
.interact_text()?;
Implementations
sourceimpl<T> Input<'_, T>
impl<T> Input<'_, T>
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 input prompt.
sourcepub fn report(&mut self, val: bool) -> &mut Self
pub fn report(&mut self, val: bool) -> &mut 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>>(&mut self, val: S) -> &mut Self
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Self
Sets initial text that user can accept or erase.
sourcepub fn default(&mut self, value: T) -> &mut Self
pub fn default(&mut self, value: T) -> &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 allow_empty(&mut self, val: bool) -> &mut Self
pub fn allow_empty(&mut self, val: bool) -> &mut 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(&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 behaviour is to append default
to the prompt to tell the
user what is the default value.
This method does not affect existance of default value, only its display in the prompt!
sourceimpl<'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.
sourceimpl<'a, T> Input<'a, T> where
T: 'a,
impl<'a, T> Input<'a, T> where
T: 'a,
sourcepub fn validate_with<V>(&mut self, validator: V) -> &mut Self where
V: Validator<T> + 'a,
V::Err: ToString,
pub fn validate_with<V>(&mut self, validator: V) -> &mut Self where
V: Validator<T> + 'a,
V::Err: ToString,
Registers a validator.
Example
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();
sourceimpl<T> Input<'_, T> where
T: Clone + ToString + FromStr,
<T as FromStr>::Err: Debug + ToString,
impl<T> Input<'_, T> where
T: Clone + ToString + FromStr,
<T as FromStr>::Err: Debug + ToString,
sourcepub fn interact_text(&mut self) -> Result<T>
pub fn interact_text(&mut self) -> Result<T>
sourcepub fn interact_text_on(&mut self, term: &Term) -> Result<T>
pub fn interact_text_on(&mut self, term: &Term) -> Result<T>
Like interact_text
but allows a specific terminal to be set.
sourceimpl<T> Input<'_, T> where
T: Clone + ToString + FromStr,
<T as FromStr>::Err: ToString,
impl<T> Input<'_, T> where
T: Clone + ToString + FromStr,
<T as FromStr>::Err: ToString,
sourcepub fn interact(&mut self) -> Result<T>
pub fn interact(&mut 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(&mut self, term: &Term) -> Result<T>
pub fn interact_on(&mut self, term: &Term) -> Result<T>
Like interact
but allows a specific terminal to be set.
Trait Implementations
Auto Trait Implementations
impl<'a, T> !RefUnwindSafe for Input<'a, T>
impl<'a, T> !Send for Input<'a, T>
impl<'a, T> !Sync for Input<'a, T>
impl<'a, T> Unpin for Input<'a, T> where
T: Unpin,
impl<'a, T> !UnwindSafe for Input<'a, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more