Sort

Struct Sort 

Source
pub struct Sort<'a> { /* private fields */ }
Expand description

Renders a sort prompt.

Returns list of indices in original items list sorted according to user input.

§Example usage

use dialoguer::Sort;

let items_to_order = vec!["Item 1", "Item 2", "Item 3"];
let ordered = Sort::new()
    .with_prompt("Order the items")
    .items(&items_to_order)
    .interact()?;

Implementations§

Source§

impl Sort<'static>

Source

pub fn new() -> Self

Creates a sort prompt.

Source§

impl Sort<'_>

Source

pub fn clear(&mut self, val: bool) -> &mut Self

Sets the clear behavior of the menu.

The default is to clear the menu after user interaction.

Source

pub fn max_length(&mut self, val: usize) -> &mut Self

Sets an optional max length for a page

Max length is disabled by None

Source

pub fn item<T: ToString>(&mut self, item: T) -> &mut Self

Add a single item to the selector.

Source

pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self

Adds multiple items to the selector.

Source

pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self

Prefaces the menu with a prompt.

By default, when a prompt is set the system also prints out a confirmation after the selection. You can opt-out of this with report.

Source

pub fn report(&mut self, val: bool) -> &mut Self

Indicates whether to report the selected order after interaction.

The default is to report the selected order.

Source

pub fn interact(&self) -> Result<Vec<usize>>

Enables user interaction and returns the result.

The user can order the items with the ‘Space’ bar and the arrows. On ‘Enter’ ordered list of the incides of items will be returned. The dialog is rendered on stderr. Result contains Vec<index> if user hit ‘Enter’. This unlike interact_opt does not allow to quit with ‘Esc’ or ‘q’.

Source

pub fn interact_opt(&self) -> Result<Option<Vec<usize>>>

Enables user interaction and returns the result.

The user can order the items with the ‘Space’ bar and the arrows. On ‘Enter’ ordered list of the incides of items will be returned. The dialog is rendered on stderr. Result contains Some(Vec<index>) if user hit ‘Enter’ or None if user cancelled with ‘Esc’ or ‘q’.

Source

pub fn interact_on(&self, term: &Term) -> Result<Vec<usize>>

Like interact but allows a specific terminal to be set.

§Examples
 use dialoguer::Sort;
 use console::Term;

 fn main() -> std::io::Result<()> {
     let selections = Sort::new()
         .item("Option A")
         .item("Option B")
         .interact_on(&Term::stderr())?;

     println!("User sorted options as indices {:?}", selections);

     Ok(())
 }
Source

pub fn interact_on_opt(&self, term: &Term) -> Result<Option<Vec<usize>>>

Like interact_opt but allows a specific terminal to be set.

§Examples
use dialoguer::Sort;
use console::Term;

fn main() -> std::io::Result<()> {
    let selections = Sort::new()
        .item("Option A")
        .item("Option B")
        .interact_on_opt(&Term::stdout())?;

    match selections {
        Some(positions) => println!("User sorted options as indices {:?}", positions),
        None => println!("User exited using Esc or q")
    }

    Ok(())
}
Source§

impl<'a> Sort<'a>

Source

pub fn with_theme(theme: &'a dyn Theme) -> Self

Creates a sort prompt with a specific theme.

Trait Implementations§

Source§

impl Default for Sort<'static>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Sort<'a>

§

impl<'a> !RefUnwindSafe for Sort<'a>

§

impl<'a> !Send for Sort<'a>

§

impl<'a> !Sync for Sort<'a>

§

impl<'a> Unpin for Sort<'a>

§

impl<'a> !UnwindSafe for Sort<'a>

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.