Struct dialoguer::FuzzySelect

source ·
pub struct FuzzySelect<'a> { /* private fields */ }
Expand description

Renders a selection menu that user can fuzzy match to reduce set.

User can use fuzzy search to limit selectable items. Interaction returns index of an item selected in the order they appear in item invocation or items slice.

Examples

use dialoguer::{
    FuzzySelect,
    theme::ColorfulTheme
};
use console::Term;

fn main() -> std::io::Result<()> {
    let items = vec!["Item 1", "item 2"];
    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .items(&items)
        .default(0)
        .interact_on_opt(&Term::stderr())?;

    match selection {
        Some(index) => println!("User selected item : {}", items[index]),
        None => println!("User did not select anything")
    }

    Ok(())
}

Implementations§

source§

impl FuzzySelect<'static>

source

pub fn new() -> Self

Creates the prompt with a specific text.

source§

impl FuzzySelect<'_>

source

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

Sets the clear behavior of the menu.

The default is to clear the menu.

source

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

Sets a default for the menu

Examples found in repository?
examples/fuzzyselect.rs (line 37)
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
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let selections = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
        "Carrots",
        "Peas",
        "Pistacio",
        "Mustard",
        "Cream",
        "Banana",
        "Chocolate",
        "Flakes",
        "Corn",
        "Cake",
        "Tarte",
        "Cheddar",
        "Vanilla",
        "Hazelnut",
        "Flour",
        "Sugar",
        "Salt",
        "Potato",
        "French Fries",
        "Pizza",
        "Mousse au chocolat",
        "Brown sugar",
        "Blueberry",
        "Burger",
    ];

    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Pick your flavor")
        .default(0)
        .items(&selections[..])
        .interact()
        .unwrap();

    println!("Enjoy your {}!", selections[selection]);
}
source

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

Add a single item to the fuzzy selector.

source

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

Adds multiple items to the fuzzy selector.

Examples found in repository?
examples/fuzzyselect.rs (line 38)
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
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let selections = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
        "Carrots",
        "Peas",
        "Pistacio",
        "Mustard",
        "Cream",
        "Banana",
        "Chocolate",
        "Flakes",
        "Corn",
        "Cake",
        "Tarte",
        "Cheddar",
        "Vanilla",
        "Hazelnut",
        "Flour",
        "Sugar",
        "Salt",
        "Potato",
        "French Fries",
        "Pizza",
        "Mousse au chocolat",
        "Brown sugar",
        "Blueberry",
        "Burger",
    ];

    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Pick your flavor")
        .default(0)
        .items(&selections[..])
        .interact()
        .unwrap();

    println!("Enjoy your {}!", selections[selection]);
}
source

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

Sets the search text that a fuzzy search starts with.

source

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

Prefaces the menu with a prompt.

When a prompt is set the system also prints out a confirmation after the fuzzy selection.

Examples found in repository?
examples/fuzzyselect.rs (line 36)
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
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let selections = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
        "Carrots",
        "Peas",
        "Pistacio",
        "Mustard",
        "Cream",
        "Banana",
        "Chocolate",
        "Flakes",
        "Corn",
        "Cake",
        "Tarte",
        "Cheddar",
        "Vanilla",
        "Hazelnut",
        "Flour",
        "Sugar",
        "Salt",
        "Potato",
        "French Fries",
        "Pizza",
        "Mousse au chocolat",
        "Brown sugar",
        "Blueberry",
        "Burger",
    ];

    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Pick your flavor")
        .default(0)
        .items(&selections[..])
        .interact()
        .unwrap();

    println!("Enjoy your {}!", selections[selection]);
}
source

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

Indicates whether to report the selected value after interaction.

The default is to report the selection.

source

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

Indicates whether to highlight matched indices

The default is to highlight the indices

source

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

Sets the maximum number of visible options.

The default is the height of the terminal minus 2.

source

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

Enables user interaction and returns the result.

The user can select the items using ‘Enter’ and the index of selected item will be returned. The dialog is rendered on stderr. Result contains index of selected item if user hit ‘Enter’. This unlike interact_opt does not allow to quit with ‘Esc’ or ‘q’.

Examples found in repository?
examples/fuzzyselect.rs (line 39)
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
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let selections = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
        "Carrots",
        "Peas",
        "Pistacio",
        "Mustard",
        "Cream",
        "Banana",
        "Chocolate",
        "Flakes",
        "Corn",
        "Cake",
        "Tarte",
        "Cheddar",
        "Vanilla",
        "Hazelnut",
        "Flour",
        "Sugar",
        "Salt",
        "Potato",
        "French Fries",
        "Pizza",
        "Mousse au chocolat",
        "Brown sugar",
        "Blueberry",
        "Burger",
    ];

    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Pick your flavor")
        .default(0)
        .items(&selections[..])
        .interact()
        .unwrap();

    println!("Enjoy your {}!", selections[selection]);
}
source

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

Enables user interaction and returns the result.

The user can select the items using ‘Enter’ and the index of selected item will be returned. The dialog is rendered on stderr. Result contains Some(index) if user hit ‘Enter’ or None if user cancelled with ‘Esc’ or ‘q’.

source

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

Like interact but allows a specific terminal to be set.

source

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

Like interact but allows a specific terminal to be set.

source§

impl<'a> FuzzySelect<'a>

source

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

Same as new but with a specific theme.

Examples found in repository?
examples/fuzzyselect.rs (line 35)
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
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let selections = &[
        "Ice Cream",
        "Vanilla Cupcake",
        "Chocolate Muffin",
        "A Pile of sweet, sweet mustard",
        "Carrots",
        "Peas",
        "Pistacio",
        "Mustard",
        "Cream",
        "Banana",
        "Chocolate",
        "Flakes",
        "Corn",
        "Cake",
        "Tarte",
        "Cheddar",
        "Vanilla",
        "Hazelnut",
        "Flour",
        "Sugar",
        "Salt",
        "Potato",
        "French Fries",
        "Pizza",
        "Mousse au chocolat",
        "Brown sugar",
        "Blueberry",
        "Burger",
    ];

    let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
        .with_prompt("Pick your flavor")
        .default(0)
        .items(&selections[..])
        .interact()
        .unwrap();

    println!("Enjoy your {}!", selections[selection]);
}

Trait Implementations§

source§

impl Default for FuzzySelect<'static>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<'a> Unpin for FuzzySelect<'a>

§

impl<'a> !UnwindSafe for FuzzySelect<'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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.