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
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 4 entries");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = MyHistory::default();
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 8 entries and contains no duplicates");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
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();
}
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
let theme = ColorfulTheme {
values_style: Style::new().yellow().dim(),
..ColorfulTheme::default()
};
println!("Welcome to the setup wizard");
if !Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact()?
{
return Ok(None);
}
let interface = Input::with_theme(&theme)
.with_prompt("Interface")
.default("127.0.0.1".parse().unwrap())
.interact()?;
let hostname = Input::with_theme(&theme)
.with_prompt("Hostname")
.interact()?;
let tls = Select::with_theme(&theme)
.with_prompt("Configure TLS")
.default(0)
.item("automatic with ACME")
.item("manual")
.item("no")
.interact()?;
let (private_key, cert, use_acme) = match tls {
0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
1 => (
Some(
Input::with_theme(&theme)
.with_prompt(" Path to private key")
.interact()?,
),
Some(
Input::with_theme(&theme)
.with_prompt(" Path to certificate")
.interact()?,
),
false,
),
_ => (None, None, false),
};
Ok(Some(Config {
hostname,
interface,
private_key,
cert,
use_acme,
}))
}
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?
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
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?
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
More examples
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
let theme = ColorfulTheme {
values_style: Style::new().yellow().dim(),
..ColorfulTheme::default()
};
println!("Welcome to the setup wizard");
if !Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact()?
{
return Ok(None);
}
let interface = Input::with_theme(&theme)
.with_prompt("Interface")
.default("127.0.0.1".parse().unwrap())
.interact()?;
let hostname = Input::with_theme(&theme)
.with_prompt("Hostname")
.interact()?;
let tls = Select::with_theme(&theme)
.with_prompt("Configure TLS")
.default(0)
.item("automatic with ACME")
.item("manual")
.item("no")
.interact()?;
let (private_key, cert, use_acme) = match tls {
0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
1 => (
Some(
Input::with_theme(&theme)
.with_prompt(" Path to private key")
.interact()?,
),
Some(
Input::with_theme(&theme)
.with_prompt(" Path to certificate")
.interact()?,
),
false,
),
_ => (None, None, false),
};
Ok(Some(Config {
hostname,
interface,
private_key,
cert,
use_acme,
}))
}
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
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 4 entries");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = MyHistory::default();
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 8 entries and contains no duplicates");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
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();
}
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
let theme = ColorfulTheme {
values_style: Style::new().yellow().dim(),
..ColorfulTheme::default()
};
println!("Welcome to the setup wizard");
if !Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact()?
{
return Ok(None);
}
let interface = Input::with_theme(&theme)
.with_prompt("Interface")
.default("127.0.0.1".parse().unwrap())
.interact()?;
let hostname = Input::with_theme(&theme)
.with_prompt("Hostname")
.interact()?;
let tls = Select::with_theme(&theme)
.with_prompt("Configure TLS")
.default(0)
.item("automatic with ACME")
.item("manual")
.item("no")
.interact()?;
let (private_key, cert, use_acme) = match tls {
0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
1 => (
Some(
Input::with_theme(&theme)
.with_prompt(" Path to private key")
.interact()?,
),
Some(
Input::with_theme(&theme)
.with_prompt(" Path to certificate")
.interact()?,
),
false,
),
_ => (None, None, false),
};
Ok(Some(Config {
hostname,
interface,
private_key,
cert,
use_acme,
}))
}
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?
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 4 entries");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = MyHistory::default();
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
More examples
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 8 entries and contains no duplicates");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
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) -> Selfwhere
V: InputValidator<T> + 'a,
V::Err: ToString,
pub fn validate_with<V>(self, validator: V) -> Selfwhere V: InputValidator<T> + 'a, V::Err: ToString,
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?
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
source§impl<T> Input<'_, T>where
T: Clone + ToString + FromStr,
<T as FromStr>::Err: ToString,
impl<T> Input<'_, T>where T: Clone + ToString + FromStr, <T as FromStr>::Err: ToString,
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
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 4 entries");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = MyHistory::default();
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn main() {
println!("Use 'exit' to quit the prompt");
println!("In this example, history is limited to 8 entries and contains no duplicates");
println!("Use the Up/Down arrows to scroll through history");
println!();
let mut history = BasicHistory::new().max_entries(8).no_duplicates(true);
loop {
if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("dialoguer")
.history_with(&mut history)
.interact_text()
{
if cmd == "exit" {
process::exit(0);
}
println!("Entered {}", cmd);
}
}
}
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 44
fn main() {
let input: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your name")
.interact_text()
.unwrap();
println!("Hello {}!", input);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your email")
.validate_with({
let mut force = None;
move |input: &String| -> Result<(), &str> {
if input.contains('@') || force.as_ref().map_or(false, |old| old == input) {
Ok(())
} else {
force = Some(input.clone());
Err("This is not a mail address; type the same value again to force use")
}
}
})
.interact_text()
.unwrap();
println!("Email: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your planet")
.default("Earth".to_string())
.interact_text()
.unwrap();
println!("Planet: {}", mail);
let mail: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Your galaxy")
.with_initial_text("Milky Way".to_string())
.interact_text()
.unwrap();
println!("Galaxy: {}", mail);
}
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?
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
fn init_config() -> Result<Option<Config>, Box<dyn Error>> {
let theme = ColorfulTheme {
values_style: Style::new().yellow().dim(),
..ColorfulTheme::default()
};
println!("Welcome to the setup wizard");
if !Confirm::with_theme(&theme)
.with_prompt("Do you want to continue?")
.interact()?
{
return Ok(None);
}
let interface = Input::with_theme(&theme)
.with_prompt("Interface")
.default("127.0.0.1".parse().unwrap())
.interact()?;
let hostname = Input::with_theme(&theme)
.with_prompt("Hostname")
.interact()?;
let tls = Select::with_theme(&theme)
.with_prompt("Configure TLS")
.default(0)
.item("automatic with ACME")
.item("manual")
.item("no")
.interact()?;
let (private_key, cert, use_acme) = match tls {
0 => (Some("acme.pkey".into()), Some("acme.cert".into()), true),
1 => (
Some(
Input::with_theme(&theme)
.with_prompt(" Path to private key")
.interact()?,
),
Some(
Input::with_theme(&theme)
.with_prompt(" Path to certificate")
.interact()?,
),
false,
),
_ => (None, None, false),
};
Ok(Some(Config {
hostname,
interface,
private_key,
cert,
use_acme,
}))
}
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?
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();
}