Skip to main content

Select

Struct Select 

Source
pub struct Select<T> { /* private fields */ }
Expand description

Prompt the user to select from a list of named choices.

Each choice is a (label, value) pair. The user selects by number.

§Example

use rusty_rich::Select;

let choice = Select::new("Pick a color")
    .choice("Red", "red")
    .choice("Green", "green")
    .choice("Blue", "blue")
    .ask()
    .unwrap();

Implementations§

Source§

impl<T> Select<T>

Source

pub fn new(prompt: impl Into<String>) -> Self

Create a new select prompt.

Source

pub fn console(self, console: Console) -> Self

Builder: set the console.

Source

pub fn choice(self, label: impl Into<String>, value: T) -> Self

Builder: add a choice with the given label and value.

Source§

impl<T: Display> Select<T>

Source

pub fn render(&self) -> String

Render the select prompt as a numbered list.

Returns a multi-line string like:

Pick a color:
  1) Red
  2) Green
  3) Blue
Enter number [1-3]:
Source§

impl<T: Display + Clone> Select<T>

Source

pub fn ask(&self) -> Result<T, PromptError>

Ask the user to select from the choices.

Displays a numbered list, then prompts for a number. Loops until a valid number is entered.

§Errors

Returns PromptError::Cancelled on EOF or Ctrl+C. Returns PromptError::InvalidResponse if there are no choices.

Auto Trait Implementations§

§

impl<T> Freeze for Select<T>

§

impl<T> !RefUnwindSafe for Select<T>

§

impl<T> Send for Select<T>
where T: Send,

§

impl<T> !Sync for Select<T>

§

impl<T> Unpin for Select<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Select<T>

§

impl<T> !UnwindSafe for Select<T>

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