pub struct Input<'a, T> { /* private fields */ }
Expand description
Renders an input prompt.
§Example
use dialoguer::Input;
fn main() {
let name: String = Input::new()
.with_prompt("Your name?")
.interact_text()
.unwrap();
println!("Your name is: {}", name);
}
It can also be used with turbofish notation:
use dialoguer::Input;
fn main() {
let name = Input::<String>::new()
.with_prompt("Your name?")
.interact_text()
.unwrap();
println!("Your name is: {}", name);
}
Implementations§
Source§impl<T> Input<'_, T>
impl<T> Input<'_, T>
Sourcepub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
pub fn with_prompt<S: Into<String>>(self, prompt: S) -> Self
Sets the input prompt.
Examples found in repository?
More examples
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 4 entries");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = MyHistory::default();
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 8 entries and contains no duplicates");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
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 input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
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 with_post_completion_text<S: Into<String>>(
self,
post_completion_text: S,
) -> Self
pub fn with_post_completion_text<S: Into<String>>( self, post_completion_text: S, ) -> Self
Changes the prompt text to the post completion text after input is complete
Sourcepub fn report(self, val: bool) -> Self
pub fn report(self, val: bool) -> Self
Indicates whether to report the input value after interaction.
The default is to report the input value.
Sourcepub fn with_initial_text<S: Into<String>>(self, val: S) -> Self
pub fn with_initial_text<S: Into<String>>(self, val: S) -> Self
Sets initial text that user can accept or erase.
Examples found in repository?
3fn main() {
4 let input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
Sourcepub fn default(self, value: T) -> Self
pub fn default(self, value: T) -> Self
Sets a default.
Out of the box the prompt does not have a default and will continue to display until the user inputs something and hits enter. If a default is set the user can instead accept the default with enter.
Examples found in repository?
3fn main() {
4 let input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
More examples
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 allow_empty(self, val: bool) -> Self
pub fn allow_empty(self, val: bool) -> Self
Enables or disables an empty input
By default, if there is no default value set for the input, the user must input a non-empty string.
Sourcepub fn show_default(self, val: bool) -> Self
pub fn show_default(self, val: bool) -> Self
Disables or enables the default value display.
The default behaviour is to append default
to the prompt to tell the
user what is the default value.
This method does not affect existence of default value, only its display in the prompt!
Source§impl<'a, T> Input<'a, T>
impl<'a, T> Input<'a, T>
Sourcepub fn with_theme(theme: &'a dyn Theme) -> Self
pub fn with_theme(theme: &'a dyn Theme) -> Self
Creates an input prompt with a specific theme.
§Example
use dialoguer::{theme::ColorfulTheme, Input};
fn main() {
let name: String = Input::with_theme(&ColorfulTheme::default())
.interact()
.unwrap();
}
Examples found in repository?
More examples
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 4 entries");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = MyHistory::default();
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 8 entries and contains no duplicates");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
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 input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
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 history_with<H>(self, history: &'a mut H) -> Selfwhere
H: History<T>,
pub fn history_with<H>(self, history: &'a mut H) -> Selfwhere
H: History<T>,
Enable history processing
§Example
use std::{collections::VecDeque, fmt::Display};
use dialoguer::{History, Input};
struct MyHistory {
history: VecDeque<String>,
}
impl Default for MyHistory {
fn default() -> Self {
MyHistory {
history: VecDeque::new(),
}
}
}
impl<T: ToString> History<T> for MyHistory {
fn read(&self, pos: usize) -> Option<String> {
self.history.get(pos).cloned()
}
fn write(&mut self, val: &T)
where
{
self.history.push_front(val.to_string());
}
}
fn main() {
let mut history = MyHistory::default();
let input = Input::<String>::new()
.history_with(&mut history)
.interact_text()
.unwrap();
}
Examples found in repository?
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 4 entries");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = MyHistory::default();
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
More examples
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 8 entries and contains no duplicates");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
Sourcepub fn completion_with<C>(self, completion: &'a C) -> Selfwhere
C: Completion,
pub fn completion_with<C>(self, completion: &'a C) -> Selfwhere
C: Completion,
Enable completion
Source§impl<'a, T> Input<'a, T>where
T: 'a,
impl<'a, T> Input<'a, T>where
T: 'a,
Sourcepub fn validate_with<V>(self, validator: V) -> Self
pub fn validate_with<V>(self, validator: V) -> Self
Registers a validator.
§Example
use dialoguer::Input;
fn main() {
let mail: String = Input::new()
.with_prompt("Enter email")
.validate_with(|input: &String| -> Result<(), &str> {
if input.contains('@') {
Ok(())
} else {
Err("This is not a mail address")
}
})
.interact()
.unwrap();
}
Examples found in repository?
3fn main() {
4 let input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
Source§impl<T> Input<'_, T>
impl<T> Input<'_, T>
Sourcepub fn interact_text(self) -> Result<T>
pub fn interact_text(self) -> Result<T>
Enables the user to enter a printable ascii sequence and returns the result.
Its difference from interact
is that it only allows ascii characters for string,
while interact
allows virtually any character to be used e.g arrow keys.
The dialog is rendered on stderr.
Examples found in repository?
More examples
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 4 entries");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = MyHistory::default();
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
4fn main() {
5 println!("Use 'exit' to quit the prompt");
6 println!("In this example, history is limited to 8 entries and contains no duplicates");
7 println!("Use the Up/Down arrows to scroll through history");
8 println!();
9
10 let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
11
12 loop {
13 if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
14 .with_prompt("dialoguer")
15 .history_with(&mut history)
16 .interact_text()
17 {
18 if cmd == "exit" {
19 process::exit(0);
20 }
21 println!("Entered {}", cmd);
22 }
23 }
24}
3fn main() {
4 let input: String = Input::with_theme(&ColorfulTheme::default())
5 .with_prompt("Your name")
6 .interact_text()
7 .unwrap();
8
9 println!("Hello {}!", input);
10
11 let mail: String = Input::with_theme(&ColorfulTheme::default())
12 .with_prompt("Your email")
13 .validate_with({
14 let mut force = None;
15 move |input: &String| -> Result<(), &str> {
16 if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
17 Ok(())
18 } else {
19 force = Some(input.clone());
20 Err("This is not a mail address; type the same value again to force use")
21 }
22 }
23 })
24 .interact_text()
25 .unwrap();
26
27 println!("Email: {}", mail);
28
29 let mail: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("Your planet")
31 .default("Earth".to_string())
32 .interact_text()
33 .unwrap();
34
35 println!("Planet: {}", mail);
36
37 let mail: String = Input::with_theme(&ColorfulTheme::default())
38 .with_prompt("Your galaxy")
39 .with_initial_text("Milky Way".to_string())
40 .interact_text()
41 .unwrap();
42
43 println!("Galaxy: {}", mail);
44}
Sourcepub fn interact_text_on(self, term: &Term) -> Result<T>
pub fn interact_text_on(self, term: &Term) -> Result<T>
Like interact_text
but allows a specific terminal to be set.
Sourcepub fn interact(self) -> Result<T>
pub fn interact(self) -> Result<T>
Enables user interaction and returns the result.
Allows any characters as input, including e.g arrow keys.
Some of the keys might have undesired behavior.
For more limited version, see interact_text
.
If the user confirms the result is true
, false
otherwise.
The dialog is rendered on stderr.
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 interact_on(self, term: &Term) -> Result<T>
pub fn interact_on(self, term: &Term) -> Result<T>
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}