Multiselect

Struct Multiselect 

Source
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>
where T: Into<String> + Clone,

Source

pub fn new<S, I>(label: S, options: I) -> Self
where S: Into<String>, I: Iterator<Item = T>,

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>

Source

pub fn new_transformed<S, I, F>(label: S, options: I, transformation: F) -> Self
where S: Into<String>, I: Iterator<Item = T>, F: Fn(&T) -> String,

Create new prompt with the given label and a transformation function that will convert the iterator items to strings

Source

pub fn help_message<S: Into<String>>(self, message: S) -> Self

Set help message to be displayed after the filter string

Source

pub fn dont_display_help_message(self) -> Self

Makes prompt not to display the help message

Source

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>

Source§

fn max_options_count(&self) -> u16

Maximum number of options that can be displayed on the screen
Source§

fn options(&self) -> &Options<T>

Returns the reference to the Options struct
Source§

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, )

Draws the option with the given index and label
Source§

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, )

Draws the entire prompt with all the options. Call this from within the Prompt::draw() method
Source§

impl<T> Prompt<Vec<T>> for Multiselect<T>

Source§

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>>

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, P> DisplayPrompt<T> for P
where P: Prompt<T>,

Source§

fn display(self) -> Result<T, AbortReason>

Draws the prompt on the screen and handles the input. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.