pub struct Input<'a> {
pub title: String,
pub description: String,
pub prompt: String,
pub placeholder: String,
pub suggestions: Option<&'a [&'a str]>,
pub inline: bool,
pub password: bool,
pub input: String,
pub theme: &'a Theme,
pub validation: Box<dyn InputValidator>,
/* private fields */
}Expand description
Single line text input
§Example
use demand::Input;
let input = Input::new("What's your name?")
.description("We'll use this to personalize your experience.")
.placeholder("Enter your name");
let name = match input.run() {
Ok(value) => value,
Err(e) => {
if e.kind() == std::io::ErrorKind::Interrupted {
println!("Input cancelled");
return;
} else {
panic!("Error: {}", e);
}
}
};Fields§
§title: StringThe title of the input
description: StringA description to display after the title
prompt: StringA prompt to display after the description
placeholder: StringA placeholder to display in the input
suggestions: Option<&'a [&'a str]>A list of suggestions to autocomplete from (legacy, prefer using autocomplete())
inline: boolShow the input inline
password: boolWhether to mask the input
input: StringInput entered by the user
theme: &'a ThemeColors/style of the input
validation: Box<dyn InputValidator>Validation function
Implementations§
Source§impl<'a> Input<'a>
impl<'a> Input<'a>
Sourcepub fn description(self, description: &str) -> Self
pub fn description(self, description: &str) -> Self
Sets the description of the input.
If the input is inline, it is displayed to the right of the title. Otherwise, it is displayed below the title.
Sourcepub fn inline(self, inline: bool) -> Self
pub fn inline(self, inline: bool) -> Self
Sets the inline flag of the input.
If true, the input is displayed inline with the title
Sourcepub fn password(self, password: bool) -> Self
pub fn password(self, password: bool) -> Self
Sets the password flag of the input.
If true, the input is masked with asterisks
Sourcepub fn placeholder(self, placeholder: &str) -> Self
pub fn placeholder(self, placeholder: &str) -> Self
Sets the placeholder of the input.
The placeholder is displayed in the input before the user enters any text
Sourcepub fn suggestions(self, suggestions: &'a [&'a str]) -> Self
pub fn suggestions(self, suggestions: &'a [&'a str]) -> Self
Sets the suggestions of the input (legacy method)
Sourcepub fn autocomplete<A: Autocomplete + 'static>(self, autocompleter: A) -> Self
pub fn autocomplete<A: Autocomplete + 'static>(self, autocompleter: A) -> Self
Sets a custom autocompleter for the input.
The autocompleter will be used to generate suggestions and completions based on user input.
Sourcepub fn autocomplete_fn<F>(self, suggester: F) -> Self
pub fn autocomplete_fn<F>(self, suggester: F) -> Self
Sets a function-based autocompleter for the input.
The function receives the current input and should return a list of suggestions.
Sourcepub fn max_suggestions_display(self, max: usize) -> Self
pub fn max_suggestions_display(self, max: usize) -> Self
Sets the maximum number of suggestions to display
Sourcepub fn prompt(self, prompt: &str) -> Self
pub fn prompt(self, prompt: &str) -> Self
Sets the prompt of the input.
The prompt is displayed after the title and description. If empty, the default prompt > is displayed.
Sourcepub fn default_value(self, default_value: impl Into<String>) -> Self
pub fn default_value(self, default_value: impl Into<String>) -> Self
Sets the default value of the input.
Sourcepub fn validation(self, validation: fn(&str) -> Result<(), &str>) -> Self
pub fn validation(self, validation: fn(&str) -> Result<(), &str>) -> Self
Sets the validation for the input.
If the input is valid, the Result is Ok(()). Otherwise, the Result is Err(&str).
Sourcepub fn validator(self, validation: impl InputValidator + 'static) -> Self
pub fn validator(self, validation: impl InputValidator + 'static) -> Self
Sets the validator for the input.
This is similar to the Input::validation method, but it’s more flexible. See InputValidator for examples
Auto Trait Implementations§
impl<'a> Freeze for Input<'a>
impl<'a> !RefUnwindSafe for Input<'a>
impl<'a> !Send for Input<'a>
impl<'a> !Sync for Input<'a>
impl<'a> Unpin for Input<'a>
impl<'a> !UnwindSafe for Input<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more