pub struct Input<F> { /* private fields */ }Expand description
This is a normal text input prompt with the following features:
- Custom label
- Validation of the input with error reporting
- Transformation of the text input to arbitrary Rust type
- Optional default value
- Optional help message
- Customizable colors and formatting
use cli_prompts::{
prompts::{Input, AbortReason::{self, Error}},
style::{InputStyle, Formatting},
DisplayPrompt
};
fn validate_and_transform(input: &str) -> Result<u16, String> {
input
.parse::<u16>()
.map_err(|e| "Provided input is not a valid time of the day".into())
.and_then(|n| if n <= 24 { Ok(n) } else { Err("Provided input is not a valid time of the day".into()) })
}
fn main() {
let input = Input::new("At what time do you usually eat lunch?", validate_and_transform)
.default_value("12")
.help_message("Enter an hour of the day as a number from 0 to 24")
.style(InputStyle::default()
.default_value_formatting(Formatting::default().bold())
);
let lunch_time : Result<u16, AbortReason> = input.display();
match lunch_time {
Ok(time) => println!("You eat lunch at {} o'clock", time),
Err(abort_reason) => match abort_reason {
Interrupt => println!("The prompt was interrupted by pressing the ESC key"),
Error(err) => println!("I/O error has occured: {:?}", err),
}
}
}Implementations§
Source§impl<F, T> Input<F>
impl<F, T> Input<F>
Sourcepub fn new(label: impl Into<String>, validation: F) -> Self
pub fn new(label: impl Into<String>, validation: F) -> Self
Constructs an input prompt with a given label and a validation function
The function serves both as validator and transformer: it should return Ok
of the arbitrary type T if validation passed and Err(String) if it failed.
The containing String will be displayed as an error message and the prompt will continue
until this function returns Ok
Examples found in repository?
examples/styling.rs (line 30)
17fn main() -> Result<()> {
18 let label_style = LabelStyle::default()
19 .prefix("*")
20 .prefix_formatting(Formatting::default().foreground_color(Color::Cyan))
21 .prompt_formatting(
22 Formatting::default()
23 .bold()
24 .underline()
25 .foreground_color(Color::Magenta),
26 );
27 let input_formatting = Formatting::default().foreground_color(Color::Cyan);
28 let submitted_formatting = Formatting::default().foreground_color(Color::DarkCyan);
29
30 let name = Input::new("Enter your name", name_validation)
31 .style(
32 InputStyle::default()
33 .label_style(label_style.clone())
34 .input_formatting(input_formatting.clone())
35 .submitted_formatting(submitted_formatting.clone()),
36 )
37 .display();
38 let coffee = Confirmation::new("Do you want a cup of coffee")
39 .style(
40 ConfirmationStyle::default()
41 .label_style(label_style.clone())
42 .input_formatting(input_formatting.clone())
43 .submitted_formatting(submitted_formatting.clone()),
44 )
45 .display();
46
47 println!("Name: {:?}", name);
48 println!("Coffee: {:?}", coffee);
49
50 Ok(())
51}More examples
examples/general.rs (line 42)
21fn main() {
22 let desserts = [
23 "Tiramisu",
24 "Cheesecake",
25 "Brownee",
26 "Cookie",
27 "Jelly",
28 "Chupa-Chups",
29 "Pudding",
30 ];
31 let subjects = [
32 "Physics",
33 "Math",
34 "Polish",
35 "English",
36 "Sport",
37 "Geography",
38 "History",
39 ];
40 let cars = [CarModel::Audi, CarModel::BMW, CarModel::Chevrolet];
41
42 let input_prompt = Input::new("Enter your name", |s| Ok(s.to_string()))
43 .default_value("John")
44 .help_message("Please provide your real name");
45 let confirmation = Confirmation::new("Do you want a cup of coffee?").default_positive(true);
46 let dessert_selection = Selection::new("Your favoite dessert", desserts.into_iter());
47 let car_selection =
48 Selection::new_with_transformation("Your car model", cars.into_iter(), car_to_string);
49 let subjects_selection =
50 Multiselect::new("What are your favourite subjects", subjects.into_iter());
51
52 let name = input_prompt.display();
53 let is_coffee = confirmation.display();
54 let dessert = dessert_selection.display();
55 let car = car_selection.display();
56 let subjects = subjects_selection.display();
57
58 println!("Name: {:?}", name);
59 println!("Is coffee: {:?}", is_coffee);
60 println!("Dessert: {:?}", dessert);
61 println!("Car: {:?}", car);
62 println!("Subjects: {:?}", subjects);
63}Sourcepub fn help_message<S: Into<String>>(self, message: S) -> Self
pub fn help_message<S: Into<String>>(self, message: S) -> Self
Sets a help message which will be displayed after the input string until the prompt is completed
Examples found in repository?
examples/general.rs (line 44)
21fn main() {
22 let desserts = [
23 "Tiramisu",
24 "Cheesecake",
25 "Brownee",
26 "Cookie",
27 "Jelly",
28 "Chupa-Chups",
29 "Pudding",
30 ];
31 let subjects = [
32 "Physics",
33 "Math",
34 "Polish",
35 "English",
36 "Sport",
37 "Geography",
38 "History",
39 ];
40 let cars = [CarModel::Audi, CarModel::BMW, CarModel::Chevrolet];
41
42 let input_prompt = Input::new("Enter your name", |s| Ok(s.to_string()))
43 .default_value("John")
44 .help_message("Please provide your real name");
45 let confirmation = Confirmation::new("Do you want a cup of coffee?").default_positive(true);
46 let dessert_selection = Selection::new("Your favoite dessert", desserts.into_iter());
47 let car_selection =
48 Selection::new_with_transformation("Your car model", cars.into_iter(), car_to_string);
49 let subjects_selection =
50 Multiselect::new("What are your favourite subjects", subjects.into_iter());
51
52 let name = input_prompt.display();
53 let is_coffee = confirmation.display();
54 let dessert = dessert_selection.display();
55 let car = car_selection.display();
56 let subjects = subjects_selection.display();
57
58 println!("Name: {:?}", name);
59 println!("Is coffee: {:?}", is_coffee);
60 println!("Dessert: {:?}", dessert);
61 println!("Car: {:?}", car);
62 println!("Subjects: {:?}", subjects);
63}Sourcepub fn default_value<S: Into<String>>(self, val: S) -> Self
pub fn default_value<S: Into<String>>(self, val: S) -> Self
Sets the default value for the prompt. It is cleared once any key is pressed other than Enter.
Examples found in repository?
examples/general.rs (line 43)
21fn main() {
22 let desserts = [
23 "Tiramisu",
24 "Cheesecake",
25 "Brownee",
26 "Cookie",
27 "Jelly",
28 "Chupa-Chups",
29 "Pudding",
30 ];
31 let subjects = [
32 "Physics",
33 "Math",
34 "Polish",
35 "English",
36 "Sport",
37 "Geography",
38 "History",
39 ];
40 let cars = [CarModel::Audi, CarModel::BMW, CarModel::Chevrolet];
41
42 let input_prompt = Input::new("Enter your name", |s| Ok(s.to_string()))
43 .default_value("John")
44 .help_message("Please provide your real name");
45 let confirmation = Confirmation::new("Do you want a cup of coffee?").default_positive(true);
46 let dessert_selection = Selection::new("Your favoite dessert", desserts.into_iter());
47 let car_selection =
48 Selection::new_with_transformation("Your car model", cars.into_iter(), car_to_string);
49 let subjects_selection =
50 Multiselect::new("What are your favourite subjects", subjects.into_iter());
51
52 let name = input_prompt.display();
53 let is_coffee = confirmation.display();
54 let dessert = dessert_selection.display();
55 let car = car_selection.display();
56 let subjects = subjects_selection.display();
57
58 println!("Name: {:?}", name);
59 println!("Is coffee: {:?}", is_coffee);
60 println!("Dessert: {:?}", dessert);
61 println!("Car: {:?}", car);
62 println!("Subjects: {:?}", subjects);
63}Sourcepub fn style(self, style: InputStyle) -> Self
pub fn style(self, style: InputStyle) -> Self
Sets the style for the prompt
Examples found in repository?
examples/styling.rs (lines 31-36)
17fn main() -> Result<()> {
18 let label_style = LabelStyle::default()
19 .prefix("*")
20 .prefix_formatting(Formatting::default().foreground_color(Color::Cyan))
21 .prompt_formatting(
22 Formatting::default()
23 .bold()
24 .underline()
25 .foreground_color(Color::Magenta),
26 );
27 let input_formatting = Formatting::default().foreground_color(Color::Cyan);
28 let submitted_formatting = Formatting::default().foreground_color(Color::DarkCyan);
29
30 let name = Input::new("Enter your name", name_validation)
31 .style(
32 InputStyle::default()
33 .label_style(label_style.clone())
34 .input_formatting(input_formatting.clone())
35 .submitted_formatting(submitted_formatting.clone()),
36 )
37 .display();
38 let coffee = Confirmation::new("Do you want a cup of coffee")
39 .style(
40 ConfirmationStyle::default()
41 .label_style(label_style.clone())
42 .input_formatting(input_formatting.clone())
43 .submitted_formatting(submitted_formatting.clone()),
44 )
45 .display();
46
47 println!("Name: {:?}", name);
48 println!("Coffee: {:?}", coffee);
49
50 Ok(())
51}Trait Implementations§
Source§impl<T, F> Prompt<T> for Input<F>
impl<T, F> Prompt<T> for Input<F>
Source§fn draw(&self, commands: &mut impl CommandBuffer)
fn draw(&self, commands: &mut impl CommandBuffer)
Defines how to draw the prompt with a set of commands.
The goal of this method is to populate the
commands buffer
with a set of commands that will draw your prompt to the screen.Source§fn on_key_pressed(&mut self, key: Key) -> EventOutcome<T>
fn on_key_pressed(&mut self, key: Key) -> EventOutcome<T>
This should handle the keyboard key presses. Should return the
outcome of the keypress: Read more
Auto Trait Implementations§
impl<F> Freeze for Input<F>where
F: Freeze,
impl<F> RefUnwindSafe for Input<F>where
F: RefUnwindSafe,
impl<F> Send for Input<F>where
F: Send,
impl<F> Sync for Input<F>where
F: Sync,
impl<F> Unpin for Input<F>where
F: Unpin,
impl<F> UnwindSafe for Input<F>where
F: UnwindSafe,
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
Mutably borrows from an owned value. Read more