Selection

Struct Selection 

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

Source

pub fn new<S, I>(label: S, options: I) -> Self
where T: Into<String> + Clone, 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 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>

Source

pub fn new_with_transformation<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

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

pub fn displayed_options_count(self, options_count: u16) -> Self

Set maximum number of options that can be displayed on the screen

Source

pub fn style(self, style: SelectionStyle) -> Self

Set the prompt style

Trait Implementations§

Source§

impl<T> MultiOptionPrompt<T> for Selection<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, _: 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)

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<T> for Selection<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<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> 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.