Struct dialoguer::Input

source ·
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>

source

pub fn new() -> Self

Creates an input prompt with default theme.

source

pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self

Sets the input prompt.

source

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

source

pub fn report(self, val: bool) -> Self

Indicates whether to report the input value after interaction.

The default is to report the input value.

source

pub fn with_initial_text<S: Into<String>>(self, val: S) -> Self

Sets initial text that user can accept or erase.

source

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.

source

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.

source

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>

source

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,

source

pub fn validate_with<V>(self, validator: V) -> Self
where V: InputValidator<T> + 'a, V::Err: ToString,

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>
where T: Clone + ToString + FromStr, <T as FromStr>::Err: ToString,

source

pub fn interact_text(self) -> Result<T>

Enables the user to enter a printable ascii sequence and returns the result.

Its difference from interact is that it only allows ascii characters for string, while interact allows virtually any character to be used e.g arrow keys.

The dialog is rendered on stderr.

source

pub fn interact_text_on(self, term: &Term) -> Result<T>

Like interact_text but allows a specific terminal to be set.

source

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.

source

pub fn interact_on(self, term: &Term) -> Result<T>

Like interact but allows a specific terminal to be set.

Trait Implementations§

source§

impl<'a, T: Clone> Clone for Input<'a, T>

source§

fn clone(&self) -> Input<'a, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Default for Input<'static, T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Input<'a, T>
where T: Freeze,

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.