Struct cargo_lambda_interactive::CustomType
source · [−]pub struct CustomType<'a, T> {
pub message: &'a str,
pub default: Option<(T, &'a (dyn Fn(T) + 'a))>,
pub placeholder: Option<&'a str>,
pub help_message: Option<&'a str>,
pub formatter: &'a (dyn Fn(T) + 'a),
pub parser: &'a (dyn Fn(&str) + 'a),
pub error_message: String,
pub render_config: RenderConfig,
}
Expand description
Generic prompt suitable for when you need to parse the user input into a specific type, for example an f64
or a rust_decimal
, maybe even an uuid
.
This prompt has all of the validation, parsing and error handling features built-in to reduce as much boilerplaste as possible from your prompts. Its defaults are necessarily very simple in order to cover a large range of generic cases, for example a “Invalid input” error message.
You can customize as many aspects of this prompt as you like: prompt message, help message, default value, placeholder, value parser and value formatter.
Behavior
When initializing this prompt via the new()
method, some constraints on the return type T
are added to make sure we can apply a default parser and formatter to the prompt.
The default parser calls the str.parse
method, which means that T
must implement the FromStr
trait. When the parsing fails for any reason, a default error message “Invalid input” is displayed to the user.
After the user submits, the prompt handler tries to parse the input into the expected type. If the operation succeeds, the value is returned to the prompt caller. If it fails, the message defined in error_message
is displayed to the user.
The default formatter simply calls to_string()
on the parsed value, which means that T
must implement the ToString
trait, which normally happens implicitly when you implement the Display
trait.
If your type T
does not satisfy these constraints, you can always manually instantiate the entire struct yourself like this:
use inquire::{CustomType, ui::RenderConfig};
let amount_prompt: CustomType<f64> = CustomType {
message: "How much is your travel going to cost?",
formatter: &|i| format!("${:.2}", i),
default: None,
placeholder: Some("123.45"),
error_message: "Please type a valid number.".into(),
help_message: "Do not use currency and the number should use dots as the decimal separator.".into(),
parser: &|i| match i.parse::<f64>() {
Ok(val) => Ok(val),
Err(_) => Err(()),
},
render_config: RenderConfig::default(),
};
Example
use inquire::CustomType;
let amount = CustomType::<f64>::new("How much do you want to donate?")
.with_formatter(&|i| format!("${:.2}", i))
.with_error_message("Please type a valid number")
.with_help_message("Type the amount in US dollars using a decimal point as a separator")
.prompt();
match amount {
Ok(_) => println!("Thanks a lot for donating that much money!"),
Err(_) => println!("We could not process your donation"),
}
Fields
message: &'a str
Message to be presented to the user.
default: Option<(T, &'a (dyn Fn(T) + 'a))>
Default value, returned when the user input is empty.
placeholder: Option<&'a str>
Short hint that describes the expected value of the input.
help_message: Option<&'a str>
Help message to be presented to the user.
formatter: &'a (dyn Fn(T) + 'a)
Function that formats the user input and presents it to the user as the final rendering of the prompt.
parser: &'a (dyn Fn(&str) + 'a)
Function that parses the user input and returns the result value.
error_message: String
Error message displayed when value could not be parsed from input.
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
sourceimpl<'a, T> CustomType<'a, T> where
T: Clone,
impl<'a, T> CustomType<'a, T> where
T: Clone,
sourcepub fn new(message: &'a str) -> CustomType<'a, T> where
T: FromStr + ToString,
pub fn new(message: &'a str) -> CustomType<'a, T> where
T: FromStr + ToString,
Creates a CustomType with the provided message and default configuration values.
sourcepub fn with_default(
self,
default: (T, &'a (dyn Fn(T) + 'a))
) -> CustomType<'a, T>
pub fn with_default(
self,
default: (T, &'a (dyn Fn(T) + 'a))
) -> CustomType<'a, T>
Sets the default input.
sourcepub fn with_placeholder(self, placeholder: &'a str) -> CustomType<'a, T>
pub fn with_placeholder(self, placeholder: &'a str) -> CustomType<'a, T>
Sets the placeholder.
sourcepub fn with_help_message(self, message: &'a str) -> CustomType<'a, T>
pub fn with_help_message(self, message: &'a str) -> CustomType<'a, T>
Sets the help message of the prompt.
sourcepub fn with_formatter(self, formatter: &'a (dyn Fn(T) + 'a)) -> CustomType<'a, T>
pub fn with_formatter(self, formatter: &'a (dyn Fn(T) + 'a)) -> CustomType<'a, T>
Sets the formatter
sourcepub fn with_parser(self, parser: &'a (dyn Fn(&str) + 'a)) -> CustomType<'a, T>
pub fn with_parser(self, parser: &'a (dyn Fn(&str) + 'a)) -> CustomType<'a, T>
Sets the parser.
sourcepub fn with_error_message(self, error_message: &'a str) -> CustomType<'a, T>
pub fn with_error_message(self, error_message: &'a str) -> CustomType<'a, T>
Sets a custom error message displayed when a submission could not be parsed to a value.
sourcepub fn with_render_config(self, render_config: RenderConfig) -> CustomType<'a, T>
pub fn with_render_config(self, render_config: RenderConfig) -> CustomType<'a, T>
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.
sourcepub fn prompt_skippable(self) -> Result<Option<T>, InquireError>
pub fn prompt_skippable(self) -> Result<Option<T>, InquireError>
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
.
sourcepub fn prompt(self) -> Result<T, InquireError>
pub fn prompt(self) -> Result<T, InquireError>
Parses the provided behavioral and rendering options and prompts the CLI user for input according to the defined rules.
Trait Implementations
sourceimpl<'a, T> Clone for CustomType<'a, T> where
T: Clone,
impl<'a, T> Clone for CustomType<'a, T> where
T: Clone,
sourcefn clone(&self) -> CustomType<'a, T>
fn clone(&self) -> CustomType<'a, T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<'a> From<Confirm<'a>> for CustomType<'a, bool>
impl<'a> From<Confirm<'a>> for CustomType<'a, bool>
sourcefn from(co: Confirm<'a>) -> CustomType<'a, bool>
fn from(co: Confirm<'a>) -> CustomType<'a, bool>
Converts to this type from the input type.
Auto Trait Implementations
impl<'a, T> !RefUnwindSafe for CustomType<'a, T>
impl<'a, T> !Send for CustomType<'a, T>
impl<'a, T> !Sync for CustomType<'a, T>
impl<'a, T> Unpin for CustomType<'a, T> where
T: Unpin,
impl<'a, T> !UnwindSafe for CustomType<'a, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more