pub struct Multiselect<T> { /* private fields */ }Expand description
Prompt that allows to select multiple options from the given list. Supports filtering and moving the selection with arrow keys.
use cli_prompts::{
prompts::{Multiselect, AbortReason},
DisplayPrompt,
};
fn main() {
let files = [
"hello.txt",
"image.jpg",
"music.mp3",
"document.pdf"
];
let prompt = Multiselect::new("Select files to copy", files.into_iter())
.dont_display_help_message()
.max_displayed_options(3);
let selection : Result<Vec<&str>, AbortReason> = prompt.display();
match selection {
Ok(selected_files) => {
for file in selected_files {
// Copy the file
}
},
Err(abort_reason) => println!("Prompt is aborted because of {:?}", abort_reason),
}
}Implementations§
Source§impl<T> Multiselect<T>
impl<T> Multiselect<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 50)
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> Multiselect<T>
impl<T> Multiselect<T>
Sourcepub fn new_transformed<S, I, F>(label: S, options: I, transformation: F) -> Self
pub fn new_transformed<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
Sourcepub fn help_message<S: Into<String>>(self, message: S) -> Self
pub fn help_message<S: Into<String>>(self, message: S) -> Self
Set help message to be displayed after the filter string
Sourcepub fn dont_display_help_message(self) -> Self
pub fn dont_display_help_message(self) -> Self
Makes prompt not to display the help message
Sourcepub fn max_displayed_options(self, max_options: u16) -> Self
pub fn max_displayed_options(self, max_options: u16) -> Self
Sets the maximum number of options that can be displayed on the screen
Trait Implementations§
Source§impl<T> MultiOptionPrompt<T> for Multiselect<T>
impl<T> MultiOptionPrompt<T> for Multiselect<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,
option_index: usize,
option_label: &str,
is_selected: bool,
commands: &mut impl CommandBuffer,
)
fn draw_option( &self, option_index: usize, option_label: &str, is_selected: bool, commands: &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<Vec<T>> for Multiselect<T>
impl<T> Prompt<Vec<T>> for Multiselect<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<Vec<T>>
fn on_key_pressed(&mut self, key: Key) -> EventOutcome<Vec<T>>
This should handle the keyboard key presses. Should return the
outcome of the keypress: Read more
Auto Trait Implementations§
impl<T> Freeze for Multiselect<T>
impl<T> RefUnwindSafe for Multiselect<T>where
T: RefUnwindSafe,
impl<T> Send for Multiselect<T>where
T: Send,
impl<T> Sync for Multiselect<T>where
T: Sync,
impl<T> Unpin for Multiselect<T>where
T: Unpin,
impl<T> UnwindSafe for Multiselect<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