Struct inquire::Password[][src]

pub struct Password<'a> {
    pub message: &'a str,
    pub help_message: Option<&'a str>,
    pub formatter: StringFormatter<'a>,
    pub display_mode: PasswordDisplayMode,
    pub enable_display_toggle: bool,
    pub validators: Vec<StringValidator<'a>>,
    pub render_config: RenderConfig,
}
Expand description

Prompt meant for secretive text inputs.

By default, the password prompt behaves like a standard one you’d see in common CLI applications: the user has no UI indicators about the state of the current input. They do not know how many characters they typed, or which character they typed, with no option to display the current text input.

However, you can still customize these and other behaviors if you wish:

  • Standard display mode: Set the display mode of the text input among hidden, masked and full via the PasswordDisplayMode enum.
    • Hidden: default behavior, no UI indicators.
    • Masked: behaves like a normal text input, except that all characters of the input are masked to a special character, which is '*' by default but can be customized via RenderConfig.
    • Full: behaves like a normal text input, no modifications.
  • Toggle display mode: By enabling this feature by calling the with_display_toggle_enabled(), you allow the user to toggle between the standard display mode set and the full display mode.
    • If you have set the standard display mode to hidden (which is also the default) or masked, the user can press Ctrl+R to change the display mode to Full, and Ctrl+R again to change it back to the standard one.
    • Obviously, if you have set the standard display mode to Full, pressing Ctrl+R won’t cause any changes.
  • Help message: Message displayed at the line below the prompt.
  • Formatter: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
    • By default, it prints eight asterisk characters: ********.
  • Validators: Custom validators to make sure a given submitted input pass the specified requirements, e.g. not allowing empty inputs or requiring special characters.
    • No validators are on by default.

Remember that for CLI applications it is standard to not allow use any display modes other than Hidden and to not allow the user to see the text input in any way. Use the customization options at your discretion.

Example

 use inquire::{validator::StringValidator, Password, PasswordDisplayMode};

 let validator: StringValidator = &|input| if input.chars().count() < 10 {
     Err(String::from("Keys must have at least 10 characters."))
 } else {
     Ok(())
 };

 let name = Password::new("Encryption Key:")
     .with_display_toggle_enabled()
     .with_display_mode(PasswordDisplayMode::Hidden)
     .with_validator(validator)
     .with_formatter(&|_| String::from("Input received"))
     .with_help_message("It is recommended to generate a new one only for this purpose")
     .prompt();

 match name {
     Ok(_) => println!("This doesn't look like a key."),
     Err(_) => println!("An error happened when asking for your key, try again later."),
 }

Fields

message: &'a str

Message to be presented to the user.

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.

display_mode: PasswordDisplayMode

How the password input is displayed to the user.

enable_display_toggle: bool

Whether to allow the user to toggle the display of the current password input by pressing the Ctrl+R hotkey.

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.

render_config: RenderConfig

RenderConfig to apply to the rendered interface.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.

Implementations

Default formatter, set to always display "********" regardless of input length.

Default validators added to the Password prompt, none.

Default help message.

Default value for the allow display toggle variable.

Default password display mode.

Creates a Password with the provided message and default options.

Sets the help message of the prompt.

Sets the flag to enable display toggling.

Sets the standard display mode for the prompt.

Sets the formatter.

Adds a validator to the collection of validators. You might want to use this feature in case you need to limit the user to specific choices, such as requiring special characters in the password.

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.

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.

Sets the provided color theme to this prompt.

Note: The default render config considers if the NO_COLOR environment variable is set to decide whether to render the colored config or the empty one.

When overriding the config in a prompt, NO_COLOR is no longer considered and your config is treated as the only source of truth. If you want to customize colors and still suport NO_COLOR, you will have to do this on your end.

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

This method is intended for flows where the user skipping/cancelling the prompt - by pressing ESC - is considered normal behavior. In this case, it does not return Err(InquireError::OperationCanceled), but Ok(None).

Meanwhile, if the user does submit an answer, the method wraps the return type with Some.

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.