Struct Select

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

Renders a select prompt.

User can select from one or more options. Interaction returns index of an item selected in the order they appear in item invocation or items slice.

§Example

use dialoguer::Select;

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

    let selection = Select::new()
        .with_prompt("What do you choose?")
        .items(&items)
        .interact()
        .unwrap();

    println!("You chose: {}", items[selection]);
}

Implementations§

Source§

impl Select<'static>

Source

pub fn new() -> Self

Creates a select prompt with default theme.

Source§

impl Select<'_>

Source

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

Indicates whether select menu should be erased from the screen after interaction.

The default is to clear the menu.

Source

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

Sets initial selected element when select menu is rendered

Element is indicated by the index at which it appears in item method invocation or items slice.

Examples found in repository?
examples/paging.rs (line 37)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9        "Carrots",
10        "Peas",
11        "Pistacio",
12        "Mustard",
13        "Cream",
14        "Banana",
15        "Chocolate",
16        "Flakes",
17        "Corn",
18        "Cake",
19        "Tarte",
20        "Cheddar",
21        "Vanilla",
22        "Hazelnut",
23        "Flour",
24        "Sugar",
25        "Salt",
26        "Potato",
27        "French Fries",
28        "Pizza",
29        "Mousse au chocolat",
30        "Brown sugar",
31        "Blueberry",
32        "Burger",
33    ];
34
35    let selection = Select::with_theme(&ColorfulTheme::default())
36        .with_prompt("Pick your flavor")
37        .default(0)
38        .items(&selections[..])
39        .interact()
40        .unwrap();
41
42    println!("Enjoy your {}!", selections[selection]);
43}
More examples
Hide additional examples
examples/select.rs (line 13)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
examples/wizard.rs (line 42)
17fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
18    let theme = ColorfulTheme {
19        values_style: Style::new().yellow().dim(),
20        ..ColorfulTheme::default()
21    };
22    println!("Welcome to the setup wizard");
23
24    if !Confirm::with_theme(&theme)
25        .with_prompt("Do you want to continue?")
26        .interact()?
27    {
28        return Ok(None);
29    }
30
31    let interface = Input::with_theme(&theme)
32        .with_prompt("Interface")
33        .default("127.0.0.1".parse().unwrap())
34        .interact()?;
35
36    let hostname = Input::with_theme(&theme)
37        .with_prompt("Hostname")
38        .interact()?;
39
40    let tls = Select::with_theme(&theme)
41        .with_prompt("Configure TLS")
42        .default(0)
43        .item("automatic with ACME")
44        .item("manual")
45        .item("no")
46        .interact()?;
47
48    let (private_key, cert, use_acme) = match tls {
49        0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
50        1 => (
51            Some(
52                Input::with_theme(&theme)
53                    .with_prompt("  Path to private key")
54                    .interact()?,
55            ),
56            Some(
57                Input::with_theme(&theme)
58                    .with_prompt("  Path to certificate")
59                    .interact()?,
60            ),
61            false,
62        ),
63        _ => (None, None, false),
64    };
65
66    Ok(Some(Config {
67        hostname,
68        interface,
69        private_key,
70        cert,
71        use_acme,
72    }))
73}
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/select.rs (line 36)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
Source

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

Add a single item to the selector.

§Example
use dialoguer::Select;

fn main() {
    let selection = Select::new()
        .item("Item 1")
        .item("Item 2")
        .interact()
        .unwrap();
}
Examples found in repository?
examples/wizard.rs (line 43)
17fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
18    let theme = ColorfulTheme {
19        values_style: Style::new().yellow().dim(),
20        ..ColorfulTheme::default()
21    };
22    println!("Welcome to the setup wizard");
23
24    if !Confirm::with_theme(&theme)
25        .with_prompt("Do you want to continue?")
26        .interact()?
27    {
28        return Ok(None);
29    }
30
31    let interface = Input::with_theme(&theme)
32        .with_prompt("Interface")
33        .default("127.0.0.1".parse().unwrap())
34        .interact()?;
35
36    let hostname = Input::with_theme(&theme)
37        .with_prompt("Hostname")
38        .interact()?;
39
40    let tls = Select::with_theme(&theme)
41        .with_prompt("Configure TLS")
42        .default(0)
43        .item("automatic with ACME")
44        .item("manual")
45        .item("no")
46        .interact()?;
47
48    let (private_key, cert, use_acme) = match tls {
49        0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
50        1 => (
51            Some(
52                Input::with_theme(&theme)
53                    .with_prompt("  Path to private key")
54                    .interact()?,
55            ),
56            Some(
57                Input::with_theme(&theme)
58                    .with_prompt("  Path to certificate")
59                    .interact()?,
60            ),
61            false,
62        ),
63        _ => (None, None, false),
64    };
65
66    Ok(Some(Config {
67        hostname,
68        interface,
69        private_key,
70        cert,
71        use_acme,
72    }))
73}
Source

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

Adds multiple items to the selector.

Examples found in repository?
examples/paging.rs (line 38)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9        "Carrots",
10        "Peas",
11        "Pistacio",
12        "Mustard",
13        "Cream",
14        "Banana",
15        "Chocolate",
16        "Flakes",
17        "Corn",
18        "Cake",
19        "Tarte",
20        "Cheddar",
21        "Vanilla",
22        "Hazelnut",
23        "Flour",
24        "Sugar",
25        "Salt",
26        "Potato",
27        "French Fries",
28        "Pizza",
29        "Mousse au chocolat",
30        "Brown sugar",
31        "Blueberry",
32        "Burger",
33    ];
34
35    let selection = Select::with_theme(&ColorfulTheme::default())
36        .with_prompt("Pick your flavor")
37        .default(0)
38        .items(&selections[..])
39        .interact()
40        .unwrap();
41
42    println!("Enjoy your {}!", selections[selection]);
43}
More examples
Hide additional examples
examples/buffered.rs (line 27)
4fn main() {
5    let items = &[
6        "Ice Cream",
7        "Vanilla Cupcake",
8        "Chocolate Muffin",
9        "A Pile of sweet, sweet mustard",
10    ];
11    let term = Term::buffered_stderr();
12    let theme = ColorfulTheme::default();
13
14    println!("All the following controls are run in a buffered terminal");
15    Confirm::with_theme(&theme)
16        .with_prompt("Do you want to continue?")
17        .interact_on(&term)
18        .unwrap();
19
20    let _: String = Input::with_theme(&theme)
21        .with_prompt("Your name")
22        .interact_on(&term)
23        .unwrap();
24
25    Select::with_theme(&theme)
26        .with_prompt("Pick an item")
27        .items(items)
28        .interact_on(&term)
29        .unwrap();
30
31    MultiSelect::with_theme(&theme)
32        .with_prompt("Pick some items")
33        .items(items)
34        .interact_on(&term)
35        .unwrap();
36
37    Sort::with_theme(&theme)
38        .with_prompt("Order these items")
39        .items(items)
40        .interact_on(&term)
41        .unwrap();
42}
examples/select.rs (line 14)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
Source

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

Sets the select 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/paging.rs (line 36)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9        "Carrots",
10        "Peas",
11        "Pistacio",
12        "Mustard",
13        "Cream",
14        "Banana",
15        "Chocolate",
16        "Flakes",
17        "Corn",
18        "Cake",
19        "Tarte",
20        "Cheddar",
21        "Vanilla",
22        "Hazelnut",
23        "Flour",
24        "Sugar",
25        "Salt",
26        "Potato",
27        "French Fries",
28        "Pizza",
29        "Mousse au chocolat",
30        "Brown sugar",
31        "Blueberry",
32        "Burger",
33    ];
34
35    let selection = Select::with_theme(&ColorfulTheme::default())
36        .with_prompt("Pick your flavor")
37        .default(0)
38        .items(&selections[..])
39        .interact()
40        .unwrap();
41
42    println!("Enjoy your {}!", selections[selection]);
43}
More examples
Hide additional examples
examples/buffered.rs (line 26)
4fn main() {
5    let items = &[
6        "Ice Cream",
7        "Vanilla Cupcake",
8        "Chocolate Muffin",
9        "A Pile of sweet, sweet mustard",
10    ];
11    let term = Term::buffered_stderr();
12    let theme = ColorfulTheme::default();
13
14    println!("All the following controls are run in a buffered terminal");
15    Confirm::with_theme(&theme)
16        .with_prompt("Do you want to continue?")
17        .interact_on(&term)
18        .unwrap();
19
20    let _: String = Input::with_theme(&theme)
21        .with_prompt("Your name")
22        .interact_on(&term)
23        .unwrap();
24
25    Select::with_theme(&theme)
26        .with_prompt("Pick an item")
27        .items(items)
28        .interact_on(&term)
29        .unwrap();
30
31    MultiSelect::with_theme(&theme)
32        .with_prompt("Pick some items")
33        .items(items)
34        .interact_on(&term)
35        .unwrap();
36
37    Sort::with_theme(&theme)
38        .with_prompt("Order these items")
39        .items(items)
40        .interact_on(&term)
41        .unwrap();
42}
examples/select.rs (line 12)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
examples/wizard.rs (line 41)
17fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
18    let theme = ColorfulTheme {
19        values_style: Style::new().yellow().dim(),
20        ..ColorfulTheme::default()
21    };
22    println!("Welcome to the setup wizard");
23
24    if !Confirm::with_theme(&theme)
25        .with_prompt("Do you want to continue?")
26        .interact()?
27    {
28        return Ok(None);
29    }
30
31    let interface = Input::with_theme(&theme)
32        .with_prompt("Interface")
33        .default("127.0.0.1".parse().unwrap())
34        .interact()?;
35
36    let hostname = Input::with_theme(&theme)
37        .with_prompt("Hostname")
38        .interact()?;
39
40    let tls = Select::with_theme(&theme)
41        .with_prompt("Configure TLS")
42        .default(0)
43        .item("automatic with ACME")
44        .item("manual")
45        .item("no")
46        .interact()?;
47
48    let (private_key, cert, use_acme) = match tls {
49        0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
50        1 => (
51            Some(
52                Input::with_theme(&theme)
53                    .with_prompt("  Path to private key")
54                    .interact()?,
55            ),
56            Some(
57                Input::with_theme(&theme)
58                    .with_prompt("  Path to certificate")
59                    .interact()?,
60            ),
61            false,
62        ),
63        _ => (None, None, false),
64    };
65
66    Ok(Some(Config {
67        hostname,
68        interface,
69        private_key,
70        cert,
71        use_acme,
72    }))
73}
Source

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

Indicates whether to report the selected value after interaction.

The default is to report the selection.

Source

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

Enables user interaction and returns the result.

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

Examples found in repository?
examples/paging.rs (line 39)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9        "Carrots",
10        "Peas",
11        "Pistacio",
12        "Mustard",
13        "Cream",
14        "Banana",
15        "Chocolate",
16        "Flakes",
17        "Corn",
18        "Cake",
19        "Tarte",
20        "Cheddar",
21        "Vanilla",
22        "Hazelnut",
23        "Flour",
24        "Sugar",
25        "Salt",
26        "Potato",
27        "French Fries",
28        "Pizza",
29        "Mousse au chocolat",
30        "Brown sugar",
31        "Blueberry",
32        "Burger",
33    ];
34
35    let selection = Select::with_theme(&ColorfulTheme::default())
36        .with_prompt("Pick your flavor")
37        .default(0)
38        .items(&selections[..])
39        .interact()
40        .unwrap();
41
42    println!("Enjoy your {}!", selections[selection]);
43}
More examples
Hide additional examples
examples/select.rs (line 15)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
examples/wizard.rs (line 46)
17fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
18    let theme = ColorfulTheme {
19        values_style: Style::new().yellow().dim(),
20        ..ColorfulTheme::default()
21    };
22    println!("Welcome to the setup wizard");
23
24    if !Confirm::with_theme(&theme)
25        .with_prompt("Do you want to continue?")
26        .interact()?
27    {
28        return Ok(None);
29    }
30
31    let interface = Input::with_theme(&theme)
32        .with_prompt("Interface")
33        .default("127.0.0.1".parse().unwrap())
34        .interact()?;
35
36    let hostname = Input::with_theme(&theme)
37        .with_prompt("Hostname")
38        .interact()?;
39
40    let tls = Select::with_theme(&theme)
41        .with_prompt("Configure TLS")
42        .default(0)
43        .item("automatic with ACME")
44        .item("manual")
45        .item("no")
46        .interact()?;
47
48    let (private_key, cert, use_acme) = match tls {
49        0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
50        1 => (
51            Some(
52                Input::with_theme(&theme)
53                    .with_prompt("  Path to private key")
54                    .interact()?,
55            ),
56            Some(
57                Input::with_theme(&theme)
58                    .with_prompt("  Path to certificate")
59                    .interact()?,
60            ),
61            false,
62        ),
63        _ => (None, None, false),
64    };
65
66    Ok(Some(Config {
67        hostname,
68        interface,
69        private_key,
70        cert,
71        use_acme,
72    }))
73}
Source

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

Enables user interaction and returns the result.

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

§Example
 use dialoguer::Select;

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

     let selection = Select::new()
         .with_prompt("What do you choose?")
         .items(&items)
         .interact_opt()
         .unwrap();

     match selection {
         Some(index) => println!("You chose: {}", items[index]),
         None => println!("You did not choose anything.")
     }
 }
Examples found in repository?
examples/select.rs (line 24)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
Source

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

Like interact but allows a specific terminal to be set.

Examples found in repository?
examples/buffered.rs (line 28)
4fn main() {
5    let items = &[
6        "Ice Cream",
7        "Vanilla Cupcake",
8        "Chocolate Muffin",
9        "A Pile of sweet, sweet mustard",
10    ];
11    let term = Term::buffered_stderr();
12    let theme = ColorfulTheme::default();
13
14    println!("All the following controls are run in a buffered terminal");
15    Confirm::with_theme(&theme)
16        .with_prompt("Do you want to continue?")
17        .interact_on(&term)
18        .unwrap();
19
20    let _: String = Input::with_theme(&theme)
21        .with_prompt("Your name")
22        .interact_on(&term)
23        .unwrap();
24
25    Select::with_theme(&theme)
26        .with_prompt("Pick an item")
27        .items(items)
28        .interact_on(&term)
29        .unwrap();
30
31    MultiSelect::with_theme(&theme)
32        .with_prompt("Pick some items")
33        .items(items)
34        .interact_on(&term)
35        .unwrap();
36
37    Sort::with_theme(&theme)
38        .with_prompt("Order these items")
39        .items(items)
40        .interact_on(&term)
41        .unwrap();
42}
Source

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

Like interact_opt but allows a specific terminal to be set.

Source§

impl<'a> Select<'a>

Source

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

Creates a select prompt with a specific theme.

§Example
use dialoguer::{theme::ColorfulTheme, Select};

fn main() {
    let selection = Select::with_theme(&ColorfulTheme::default())
        .items(&["foo", "bar", "baz"])
        .interact()
        .unwrap();
}
Examples found in repository?
examples/paging.rs (line 35)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9        "Carrots",
10        "Peas",
11        "Pistacio",
12        "Mustard",
13        "Cream",
14        "Banana",
15        "Chocolate",
16        "Flakes",
17        "Corn",
18        "Cake",
19        "Tarte",
20        "Cheddar",
21        "Vanilla",
22        "Hazelnut",
23        "Flour",
24        "Sugar",
25        "Salt",
26        "Potato",
27        "French Fries",
28        "Pizza",
29        "Mousse au chocolat",
30        "Brown sugar",
31        "Blueberry",
32        "Burger",
33    ];
34
35    let selection = Select::with_theme(&ColorfulTheme::default())
36        .with_prompt("Pick your flavor")
37        .default(0)
38        .items(&selections[..])
39        .interact()
40        .unwrap();
41
42    println!("Enjoy your {}!", selections[selection]);
43}
More examples
Hide additional examples
examples/buffered.rs (line 25)
4fn main() {
5    let items = &[
6        "Ice Cream",
7        "Vanilla Cupcake",
8        "Chocolate Muffin",
9        "A Pile of sweet, sweet mustard",
10    ];
11    let term = Term::buffered_stderr();
12    let theme = ColorfulTheme::default();
13
14    println!("All the following controls are run in a buffered terminal");
15    Confirm::with_theme(&theme)
16        .with_prompt("Do you want to continue?")
17        .interact_on(&term)
18        .unwrap();
19
20    let _: String = Input::with_theme(&theme)
21        .with_prompt("Your name")
22        .interact_on(&term)
23        .unwrap();
24
25    Select::with_theme(&theme)
26        .with_prompt("Pick an item")
27        .items(items)
28        .interact_on(&term)
29        .unwrap();
30
31    MultiSelect::with_theme(&theme)
32        .with_prompt("Pick some items")
33        .items(items)
34        .interact_on(&term)
35        .unwrap();
36
37    Sort::with_theme(&theme)
38        .with_prompt("Order these items")
39        .items(items)
40        .interact_on(&term)
41        .unwrap();
42}
examples/select.rs (line 11)
3fn main() {
4    let selections = &[
5        "Ice Cream",
6        "Vanilla Cupcake",
7        "Chocolate Muffin",
8        "A Pile of sweet, sweet mustard",
9    ];
10
11    let selection = Select::with_theme(&ColorfulTheme::default())
12        .with_prompt("Pick your flavor")
13        .default(0)
14        .items(&selections[..])
15        .interact()
16        .unwrap();
17
18    println!("Enjoy your {}!", selections[selection]);
19
20    let selection = Select::with_theme(&ColorfulTheme::default())
21        .with_prompt("Optionally pick your flavor")
22        .default(0)
23        .items(&selections[..])
24        .interact_opt()
25        .unwrap();
26
27    if let Some(selection) = selection {
28        println!("Enjoy your {}!", selections[selection]);
29    } else {
30        println!("You didn't select anything!");
31    }
32
33    let selection = Select::with_theme(&ColorfulTheme::default())
34        .with_prompt("Optionally pick your flavor, hint it might be on the second page")
35        .default(0)
36        .max_length(2)
37        .items(&selections[..])
38        .interact()
39        .unwrap();
40
41    println!("Enjoy your {}!", selections[selection]);
42}
examples/wizard.rs (line 40)
17fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
18    let theme = ColorfulTheme {
19        values_style: Style::new().yellow().dim(),
20        ..ColorfulTheme::default()
21    };
22    println!("Welcome to the setup wizard");
23
24    if !Confirm::with_theme(&theme)
25        .with_prompt("Do you want to continue?")
26        .interact()?
27    {
28        return Ok(None);
29    }
30
31    let interface = Input::with_theme(&theme)
32        .with_prompt("Interface")
33        .default("127.0.0.1".parse().unwrap())
34        .interact()?;
35
36    let hostname = Input::with_theme(&theme)
37        .with_prompt("Hostname")
38        .interact()?;
39
40    let tls = Select::with_theme(&theme)
41        .with_prompt("Configure TLS")
42        .default(0)
43        .item("automatic with ACME")
44        .item("manual")
45        .item("no")
46        .interact()?;
47
48    let (private_key, cert, use_acme) = match tls {
49        0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
50        1 => (
51            Some(
52                Input::with_theme(&theme)
53                    .with_prompt("  Path to private key")
54                    .interact()?,
55            ),
56            Some(
57                Input::with_theme(&theme)
58                    .with_prompt("  Path to certificate")
59                    .interact()?,
60            ),
61            false,
62        ),
63        _ => (None, None, false),
64    };
65
66    Ok(Some(Config {
67        hostname,
68        interface,
69        private_key,
70        cert,
71        use_acme,
72    }))
73}

Trait Implementations§

Source§

impl<'a> Clone for Select<'a>

Source§

fn clone(&self) -> Select<'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 Select<'static>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a> Freeze for Select<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for Select<'a>

§

impl<'a> !UnwindSafe for Select<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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