Skip to main content

Crate pick_a_boo

Crate pick_a_boo 

Source
Expand description

§pick-a-boo

A very simple option picker for CLI tools!

§Installation

Add pick-a-boo to your project’s dependencies:

[dependencies]
pick-a-boo = "0.1"

§Usage

Here is just a simple example of how to use pick-a-boo in your Rust project:

fn main() -> std::io::Result<()> {
    let options = pick_a_boo::Options::from(&["Yes", "So so", "Maybe", "No"])
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
    match pick_a_boo::choose("🦀 Do you like Rust? 🦀", options) {
        Ok(Some(choice)) if choice == "Yes"   => println!("You like Rust! 🤩"),
        Ok(Some(choice)) if choice == "So so" => println!("You feel so so about Rust."),
        Ok(Some(choice)) if choice == "Maybe" => println!("You are unsure about Rust."),
        Ok(Some(choice)) if choice == "No"    => println!("You don't like Rust... 😭"),
        Ok(Some(_)) => panic!("Unknown choice. never reach here!"),
        Ok(None) => println!("Cancelled."),
        Err(e) => return Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
    }
    Ok(())
}

When you run the program, you’ll be presented with a prompt in your terminal:

Do you like Rust? [Yes /s/m/n]
Do you like Rust? [y/ So So /m/n]
Do you like Rust? [y/s/ Maybe /n]
Do you like Rust? [y/s/m/ No]
  • Navigate between “Yes” and “No” using the left and right arrow keys.
  • Press Enter to select your choice.
  • Press the corresponding key (y/s/m/n) to select an option directly.
  • Press Ctrl+C or Escape to cancel (returns None).

Macros§

item
Macro to create an Item instance with flexible arguments. The first positional argument is the long name (label) of the item. Following arguments can be provided as either positional or named arguments:

Structs§

Display
Display struct for formatting the options for display with Picker.
Item
Item struct represents a selectable option with a name, key, and optional description.
Options
Options struct holds a list of items and the current selection index. To create an instance, use the OptionBuilder or the Options::from helper method.
OptionsBuilder
Builder for Options.
Picker
Picker struct is the main interface for choosing options. It holds the following configuration for the picker behavior.
PickerBuilder
Builder for Picker.

Enums§

DescriptionNameWidth
DescriptionNameWidth enum defines how the width of item names is determined. This enum is used when displaying item descriptions (DescriptionShowMode::CurrentOnly, and DescriptionShowMode::All).
DescriptionShowMode
DescriptionShowMode enum defines how item descriptions are displayed.

Functions§

choose
Hellper function to choose an option from the provided Options with the given prompt. This routine is a shortcut for creating a default Picker instance and calling its Picker::choose method.
yes_or_no
Helper function to ask a yes-or-no question with the given prompt. This routine is a shortcut for creating a default Picker instance and calling its Picker::yes_or_no method.