Struct dialoguer::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

use dialoguer::Sort;

fn main() {
    let items = vec!["foo", "bar", "baz"];

    let ordered = Sort::new()
        .with_prompt("Which order do you prefer?")
        .items(&items)
        .interact()
        .unwrap();

    println!("You prefer:");

    for i in ordered {
        println!("{}", items[i]);
    }
}

Implementations§

source§

impl Sort<'static>

source

pub fn new() -> Self

Creates a sort prompt with default theme.

source§

impl Sort<'_>

source

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

Sets the clear behavior of the menu.

The default is to clear the menu after user interaction.

source

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

Sets an optional max length for a page

Max length is disabled by None

Examples found in repository?
examples/sort.rs (line 24)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let list = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);

    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .max_length(2)
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);
}
source

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

Add a single item to the selector.

source

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

Adds multiple items to the selector.

Examples found in repository?
examples/sort.rs (line 12)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let list = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);

    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .max_length(2)
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);
}
More examples
Hide additional examples
examples/buffered.rs (line 39)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let items = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let term = Term::buffered_stderr();
    let theme = ColorfulTheme::default();

    println!("All the following controls are run in a buffered terminal");
    Confirm::with_theme(&theme)
        .with_prompt("Do you want to continue?")
        .interact_on(&term)
        .unwrap();

    let _: String = Input::with_theme(&theme)
        .with_prompt("Your name")
        .interact_on(&term)
        .unwrap();

    Select::with_theme(&theme)
        .with_prompt("Pick an item")
        .items(items)
        .interact_on(&term)
        .unwrap();

    MultiSelect::with_theme(&theme)
        .with_prompt("Pick some items")
        .items(items)
        .interact_on(&term)
        .unwrap();

    Sort::with_theme(&theme)
        .with_prompt("Order these items")
        .items(items)
        .interact_on(&term)
        .unwrap();
}
source

pub fn with_prompt<S: Into<String>>(self, prompt: S) -> 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.

Examples found in repository?
examples/sort.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let list = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);

    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .max_length(2)
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);
}
More examples
Hide additional examples
examples/buffered.rs (line 38)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let items = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let term = Term::buffered_stderr();
    let theme = ColorfulTheme::default();

    println!("All the following controls are run in a buffered terminal");
    Confirm::with_theme(&theme)
        .with_prompt("Do you want to continue?")
        .interact_on(&term)
        .unwrap();

    let _: String = Input::with_theme(&theme)
        .with_prompt("Your name")
        .interact_on(&term)
        .unwrap();

    Select::with_theme(&theme)
        .with_prompt("Pick an item")
        .items(items)
        .interact_on(&term)
        .unwrap();

    MultiSelect::with_theme(&theme)
        .with_prompt("Pick some items")
        .items(items)
        .interact_on(&term)
        .unwrap();

    Sort::with_theme(&theme)
        .with_prompt("Order these items")
        .items(items)
        .interact_on(&term)
        .unwrap();
}
source

pub fn report(self, val: bool) -> 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’.

Examples found in repository?
examples/sort.rs (line 13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let list = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);

    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .max_length(2)
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);
}
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’.

Example
use dialoguer::Sort;

fn main() {
    let items = vec!["foo", "bar", "baz"];

    let ordered = Sort::new()
        .items(&items)
        .interact_opt()
        .unwrap();

    match ordered {
        Some(positions) => {
            println!("You prefer:");

            for i in positions {
                println!("{}", items[i]);
            }
        },
        None => println!("You did not prefer anything.")
    }
}
source

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

Like interact but allows a specific terminal to be set.

Examples found in repository?
examples/buffered.rs (line 40)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let items = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let term = Term::buffered_stderr();
    let theme = ColorfulTheme::default();

    println!("All the following controls are run in a buffered terminal");
    Confirm::with_theme(&theme)
        .with_prompt("Do you want to continue?")
        .interact_on(&term)
        .unwrap();

    let _: String = Input::with_theme(&theme)
        .with_prompt("Your name")
        .interact_on(&term)
        .unwrap();

    Select::with_theme(&theme)
        .with_prompt("Pick an item")
        .items(items)
        .interact_on(&term)
        .unwrap();

    MultiSelect::with_theme(&theme)
        .with_prompt("Pick some items")
        .items(items)
        .interact_on(&term)
        .unwrap();

    Sort::with_theme(&theme)
        .with_prompt("Order these items")
        .items(items)
        .interact_on(&term)
        .unwrap();
}
source

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

Like interact_opt but allows a specific terminal to be set.

source§

impl<'a> Sort<'a>

source

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

Creates a sort prompt with a specific theme.

Example
use dialoguer::{theme::ColorfulTheme, Sort};

fn main() {
    let ordered = Sort::with_theme(&ColorfulTheme::default())
        .items(&["foo", "bar", "baz"])
        .interact()
        .unwrap();
}
Examples found in repository?
examples/sort.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let list = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);

    let sorted = Sort::with_theme(&ColorfulTheme::default())
        .with_prompt("Order your foods by preference")
        .items(&list[..])
        .max_length(2)
        .interact()
        .unwrap();

    println!("Your favorite item:");
    println!("  {}", list[sorted[0]]);
    println!("Your least favorite item:");
    println!("  {}", list[sorted[sorted.len() - 1]]);
}
More examples
Hide additional examples
examples/buffered.rs (line 37)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let items = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
    ];
    let term = Term::buffered_stderr();
    let theme = ColorfulTheme::default();

    println!("All the following controls are run in a buffered terminal");
    Confirm::with_theme(&theme)
        .with_prompt("Do you want to continue?")
        .interact_on(&term)
        .unwrap();

    let _: String = Input::with_theme(&theme)
        .with_prompt("Your name")
        .interact_on(&term)
        .unwrap();

    Select::with_theme(&theme)
        .with_prompt("Pick an item")
        .items(items)
        .interact_on(&term)
        .unwrap();

    MultiSelect::with_theme(&theme)
        .with_prompt("Pick some items")
        .items(items)
        .interact_on(&term)
        .unwrap();

    Sort::with_theme(&theme)
        .with_prompt("Order these items")
        .items(items)
        .interact_on(&term)
        .unwrap();
}

Trait Implementations§

source§

impl<'a> Clone for Sort<'a>

source§

fn clone(&self) -> Sort<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Sort<'static>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.