pub struct Selection<T> { /* private fields */ }Expand description
Prompt that allows to select one option from the given list. Supports filtering and moving the selection with arrow keys.
use cli_prompts::{
prompts::{Selection, AbortReason},
DisplayPrompt,
};
fn main() {
let social_media = [
"Facebook",
"Instagram",
"Twitter",
"Snapchat"
];
let prompt = Selection::new("Where you want to post?", social_media.into_iter())
.displayed_options_count(3);
let selection : Result<&str, AbortReason> = prompt.display();
match selection {
Ok(media) => println!("Posting to {}", media),
Err(abort_reason) => println!("Prompt is aborted because of {:?}", abort_reason),
}
}Implementations§
Source§impl<T> Selection<T>
impl<T> Selection<T>
Sourcepub fn new<S, I>(label: S, options: I) -> Self
pub fn new<S, I>(label: S, options: I) -> Self
Create new prompt with the given label and the iterator over a type that is convertable to
String
Examples found in repository?
examples/general.rs (line 46)
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}Source§impl<T> Selection<T>
impl<T> Selection<T>
Sourcepub fn new_with_transformation<S, I, F>(
label: S,
options: I,
transformation: F,
) -> Self
pub fn new_with_transformation<S, I, F>( label: S, options: I, transformation: F, ) -> Self
Create new prompt with the given label and a transformation function that will convert the iterator items to strings
Examples found in repository?
examples/general.rs (line 48)
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 displayed_options_count(self, options_count: u16) -> Self
pub fn displayed_options_count(self, options_count: u16) -> Self
Set maximum number of options that can be displayed on the screen
Sourcepub fn style(self, style: SelectionStyle) -> Self
pub fn style(self, style: SelectionStyle) -> Self
Set the prompt style
Trait Implementations§
Source§impl<T> MultiOptionPrompt<T> for Selection<T>
impl<T> MultiOptionPrompt<T> for Selection<T>
Source§fn max_options_count(&self) -> u16
fn max_options_count(&self) -> u16
Maximum number of options that can be displayed on the screen
Source§fn currently_selected_index(&self) -> usize
fn currently_selected_index(&self) -> usize
Get the index of the option on the screen that is currently selected
Source§fn draw_option(
&self,
_: usize,
option_label: &str,
is_selected: bool,
cmd_buffer: &mut impl CommandBuffer,
)
fn draw_option( &self, _: usize, option_label: &str, is_selected: bool, cmd_buffer: &mut impl CommandBuffer, )
Draws the option with the given index and label
Source§fn draw_header(&self, commands: &mut impl CommandBuffer, is_submitted: bool)
fn draw_header(&self, commands: &mut impl CommandBuffer, is_submitted: bool)
Draws the prompt header
Source§fn draw_multioption(
&self,
label: &str,
is_submitted: bool,
label_style: &LabelStyle,
cmd_buffer: &mut impl CommandBuffer,
)
fn draw_multioption( &self, label: &str, is_submitted: bool, label_style: &LabelStyle, cmd_buffer: &mut impl CommandBuffer, )
Draws the entire prompt with all the options. Call this from within the
Prompt::draw()
methodSource§impl<T> Prompt<T> for Selection<T>
impl<T> Prompt<T> for Selection<T>
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<T> Freeze for Selection<T>
impl<T> RefUnwindSafe for Selection<T>where
T: RefUnwindSafe,
impl<T> Send for Selection<T>where
T: Send,
impl<T> Sync for Selection<T>where
T: Sync,
impl<T> Unpin for Selection<T>where
T: Unpin,
impl<T> UnwindSafe for Selection<T>where
T: 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