Function console_utils::select

source ·
pub fn select(
    before: &str,
    options: &[String],
    allow_empty: bool,
    multiple: bool
) -> Option<Vec<bool>>
Expand description

Allows the user to select options from a list using the console.

This function displays a list of options with checkboxes. The user can navigate through the options using arrow keys or ‘w’ and ‘s’ keys. Pressing the spacebar toggles the selection of the current option. If the user presses Enter, the function returns a vector of booleans indicating which options were selected.

Arguments

  • before - The text to display before the list of options.
  • options - A vector of strings representing the available options.
  • allow_empty - If true, allows the user to exit without selecting any option.
  • multiple - If true, allows the user to select multiple options.

Returns

Returns an Option<Vec<bool>> containing a vector of booleans indicating which options were selected. Returns None if no option was selected and allow_empty is false.

Example

use console_utils::select;

let options = vec![
    "Option 1".to_string(),
    "Option 2".to_string(),
    "Option 3".to_string(),
];

let selected_indices = select("Select an option:", &options, false, false);

match selected_indices {
    Some(indices) => {
        println!("Selected indices: {:?}", indices);
    }
    None => {
        println!("No option selected.");
    }
}