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<'_>
impl Select<'_>
Sourcepub fn clear(self, val: bool) -> Self
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.
Sourcepub fn default(self, val: usize) -> Self
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?
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
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}
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}
Sourcepub fn max_length(self, val: usize) -> Self
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?
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}
Sourcepub fn item<T: ToString>(self, item: T) -> Self
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?
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}
Sourcepub fn items<T: ToString>(self, items: &[T]) -> Self
pub fn items<T: ToString>(self, items: &[T]) -> Self
Adds multiple items to the selector.
Examples found in repository?
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
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}
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}
Sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
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?
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
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}
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}
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}
Sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report the selected value after interaction.
The default is to report the selection.
Sourcepub fn interact(self) -> Result<usize>
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?
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
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}
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}
Sourcepub fn interact_opt(self) -> Result<Option<usize>>
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?
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}
Sourcepub fn interact_on(self, term: &Term) -> Result<usize>
pub fn interact_on(self, term: &Term) -> Result<usize>
Like interact
but allows a specific terminal to be set.
Examples found in repository?
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}
Sourcepub fn interact_on_opt(self, term: &Term) -> Result<Option<usize>>
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>
impl<'a> Select<'a>
Sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
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?
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
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}
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}
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}