Struct inquire::Text[][src]

pub struct Text<'a> {
    pub message: &'a str,
    pub default: Option<&'a str>,
    pub help_message: Option<&'a str>,
    pub formatter: StringFormatter<'a>,
    pub validators: Vec<StringValidator<'a>>,
    pub page_size: usize,
    pub suggester: Option<Suggester<'a>>,
}
Expand description

Standard text prompt that returns the user string input.

This is the standard the standard kind of prompt you would expect from a library like this one. It displays a message to the user, prompting them to type something back. The user’s input is then stored in a String and returned to the prompt caller.

Configuration options

  • Prompt message: Main message when prompting the user for input, "What is your name?" in the example below.
  • Help message: Message displayed at the line below the prompt.
  • Default value: Default value returned when the user submits an empty response.
  • Validators: Custom validators to the user’s input, displaying an error message if the input does not pass the requirements.
  • Formatter: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
  • Suggester: Custom function that returns a list of input suggestions based on the current text input. See more on “Autocomplete” below.

Default behaviors

Default behaviors for each one of Text configuration options:

  • The input formatter just echoes back the given input.
  • No validators are called, accepting any sort of input including empty ones.
  • No default values or help messages.
  • No auto-completion features set-up.
  • Prompt messages are always required when instantiating via new().

Autocomplete

With Text inputs, it is also possible to set-up an auto-completion system to provide a better UX when necessary.

You can set-up a custom Suggester function, which receives the current input as the only argument and should return a vector of strings, the suggested values.

The user is then able to select one of them by moving up and down the list, possibly further modifying a selected suggestion.

Example

use inquire::Text;

let name = Text::new("What is your name?").prompt();

match name {
    Ok(name) => println!("Hello {}", name),
    Err(_) => println!("An error happened when asking for your name, try again later."),
}

Fields

message: &'a str

Message to be presented to the user.

default: Option<&'a str>

Default value, returned when the user input is empty.

help_message: Option<&'a str>

Help message to be presented to the user.

formatter: StringFormatter<'a>

Function that formats the user input and presents it to the user as the final rendering of the prompt.

validators: Vec<StringValidator<'a>>

Collection of validators to apply to the user input.

Validators are executed in the order they are stored, stopping at and displaying to the user only the first validation error that might appear.

The possible error is displayed to the user one line above the prompt.

page_size: usize

Page size of the suggestions displayed to the user, when applicable.

suggester: Option<Suggester<'a>>

Function that provides a list of suggestions to the user based on the current input.

Implementations

Default formatter, set to DEFAULT_STRING_FORMATTER

Default page size, equal to the global default page size config::DEFAULT_PAGE_SIZE

Default validators added to the Text prompt, none.

Default help message.

Creates a Text with the provided message and default options.

Sets the help message of the prompt.

Sets the default input.

Sets the suggester.

Sets the formatter.

Sets the page size

Adds a validator to the collection of validators. You might want to use this feature in case you need to require certain features from the user’s answer, such as defining a limit of characters.

Validators are executed in the order they are stored, stopping at and displaying to the user only the first validation error that might appear.

The possible error is displayed to the user one line above the prompt.

Adds the validators to the collection of validators in the order they are given. You might want to use this feature in case you need to require certain features from the user’s answer, such as defining a limit of characters.

Validators are executed in the order they are stored, stopping at and displaying to the user only the first validation error that might appear.

The possible error is displayed to the user one line above the prompt.

Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.