pub struct PasswordBuilder<'a> { /* private fields */ }Expand description
The builder for an password prompt.
How it looks if you set a mask:

How it looks if you do not set a mask:

See the various methods for more details on each available option.
§Examples
use reequestty::Question;
let password = Question::password("password")
.message("What is your password?")
.mask('*')
.build();Implementations§
Source§impl<'a> PasswordBuilder<'a>
impl<'a> PasswordBuilder<'a>
Sourcepub fn message<M>(self, message: M) -> Self
pub fn message<M>(self, message: M) -> Self
The message to display when the prompt is rendered in the terminal.
It can be either a String or a FnOnce that returns a String. If it is a
function, it is passed all the previous Answers, and will be called right before the
question is prompted to the user.
If it is not given, the message defaults to “<name>: “.
§Examples
use reequestty::Question;
let password = Question::password("password")
.message("What is your password?")
.build();Sourcepub fn when<W>(self, when: W) -> Self
pub fn when<W>(self, when: W) -> Self
Whether to ask the question (true) or not (false).
It can be either a bool or a FnOnce that returns a bool. If it is a
function, it is passed all the previous Answers, and will be called right before the
question is prompted to the user.
If it is not given, it defaults to true.
§Examples
use reequestty::{Answers, Question};
let password = Question::password("password")
.when(|previous_answers: &Answers| match previous_answers.get("anonymous") {
Some(ans) => !ans.as_bool().unwrap(),
None => true,
})
.build();Sourcepub fn ask_if_answered(self, ask_if_answered: bool) -> Self
pub fn ask_if_answered(self, ask_if_answered: bool) -> Self
Prompt the question even if it is answered.
By default if an answer with the given name already exists, the question will be
skipped. This can be overridden by setting ask_if_answered is set to true.
If this is not given, it defaults to false.
If you need to dynamically decide whether the question should be asked, use when.
§Examples
use reequestty::Question;
let password = Question::password("password")
.ask_if_answered(true)
.build();Sourcepub fn on_esc<T>(self, on_esc: T) -> Self
pub fn on_esc<T>(self, on_esc: T) -> Self
Configure what to do when the user presses the Esc key.
It can be either a OnEsc or a FnOnce that returns a OnEsc. If it is a
function, it is passed all the previous Answers, and will be called right before the
question is prompted to the user.
If it is not given, it defaults to OnEsc::Ignore.
§Examples
use reequestty::{Question, OnEsc};
let password = Question::password("password")
.on_esc(OnEsc::Terminate)
.build();Sourcepub fn mask(self, mask: char) -> Self
pub fn mask(self, mask: char) -> Self
Set a mask to print instead of the characters
Each character when printed to the terminal will be replaced by the given mask. If a mask is not provided, then input will be hidden.
§Examples
use reequestty::Question;
let password = Question::password("password")
.mask('*')
.build();Sourcepub fn filter<F>(self, filter: F) -> Self
pub fn filter<F>(self, filter: F) -> Self
Function to change the final submitted value before it is displayed to the user and
added to the Answers.
It is a FnOnce that is given the answer and the previous Answers, and should
return the new answer.
This will be called after the answer has been validated.
§Examples
use reequestty::Question;
let password = Question::password("password")
.filter(|password, previous_answers| encrypt(password))
.build();Sourcepub fn validate<F>(self, filter: F) -> Self
pub fn validate<F>(self, filter: F) -> Self
Function to validate the submitted value before it’s returned.
It is a FnMut that is given the answer and the previous Answers, and should
return Ok(()) if the given answer is valid. If it is invalid, it should return an
Err with the error message to display to the user.
This will be called when the user presses the Enter key.
§Examples
use reequestty::Question;
let password = Question::password("password")
.validate(|password, previous_answers| if password.chars().count() >= 5 {
Ok(())
} else {
Err("Your password must be at least 5 characters long".to_owned())
})
.build();Sourcepub fn validate_on_key<F>(self, filter: F) -> Self
pub fn validate_on_key<F>(self, filter: F) -> Self
Function to validate the value on every key press. If the validation fails, the text is displayed in red.
It is a FnMut that is given the answer and the previous Answers, and should
return true if it is valid.
This will be called after every change in state. Note that this validation is purely
cosmetic. If the user presses Enter, this function is not called. Instead, the one
supplied to validate (if any) is the only validation that can
prevent a user submission. This is required since final validation needs to return an
error message to show the user.
Note, this method will have no visual effect if the password is hidden instead of masked.
§Examples
use reequestty::Question;
let password = Question::password("password")
.validate_on_key(|password, previous_answers| password.chars().count() >= 5)
// Still required as this is the final validation and validate_on_key is purely cosmetic
.validate(|password, previous_answers| if password.chars().count() >= 5 {
Ok(())
} else {
Err("Your password must be at least 5 characters long".to_owned())
})
.build();Sourcepub fn transform<F>(self, transform: F) -> Self
pub fn transform<F>(self, transform: F) -> Self
Change the way the answer looks when displayed to the user.
It is a FnOnce that is given the answer, previous Answers and the Backend to
display the answer on. After the transform is called, a new line is also added.
It will only be called once the user finishes answering the question.
§Examples
use reequestty::Question;
use reequestty::prompt::style::Color;
let password = Question::password("password")
.transform(|password, previous_answers, backend| {
backend.set_fg(Color::Cyan)?;
for _ in password.chars() {
write!(backend, "*")?;
}
backend.set_fg(Color::Reset)
})
.build();Trait Implementations§
Source§impl<'a> Debug for PasswordBuilder<'a>
impl<'a> Debug for PasswordBuilder<'a>
Source§impl<'a> From<PasswordBuilder<'a>> for Question<'a>
impl<'a> From<PasswordBuilder<'a>> for Question<'a>
Source§fn from(builder: PasswordBuilder<'a>) -> Self
fn from(builder: PasswordBuilder<'a>) -> Self
Consumes the builder returning a Question