pub struct Input<'a, T> { /* private fields */ }
Expand description
Renders an input prompt.
Example usage
use dialoguer::Input;
let input : String = Input::new()
.with_prompt("Tea or coffee?")
.with_initial_text("Yes")
.default("No".into())
.interact_text()?;
It can also be used with turbofish notation:
let input = Input::<String>::new()
.interact_text()?;
Implementations§
source§impl<T> Input<'_, T>
impl<T> Input<'_, T>
sourcepub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut 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 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>>(
&mut self,
post_completion_text: S
) -> &mut Self
pub fn with_post_completion_text<S: Into<String>>( &mut self, post_completion_text: S ) -> &mut Self
Changes the prompt text to the post completion text after input is complete
sourcepub fn report(&mut self, val: bool) -> &mut Self
pub fn report(&mut self, val: bool) -> &mut 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>>(&mut self, val: S) -> &mut Self
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut 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(&mut self, value: T) -> &mut Self
pub fn default(&mut self, value: T) -> &mut 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(&mut self, val: bool) -> &mut Self
pub fn allow_empty(&mut self, val: bool) -> &mut 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(&mut self, val: bool) -> &mut Self
pub fn show_default(&mut self, val: bool) -> &mut 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.
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 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>(&mut self, history: &'a mut H) -> &mut Selfwhere
H: History<T>,
pub fn history_with<H>(&mut self, history: &'a mut H) -> &mut Selfwhere H: History<T>,
Enable history processing
Example
let mut history = MyHistory::default();
loop {
if let Ok(input) = Input::<String>::new()
.with_prompt("hist")
.history_with(&mut history)
.interact_text()
{
// Do something with the input
}
}
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);
}
}
}
sourcepub fn completion_with<C>(&mut self, completion: &'a C) -> &mut Selfwhere
C: Completion,
pub fn completion_with<C>(&mut self, completion: &'a C) -> &mut 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>(&mut self, validator: V) -> &mut Selfwhere
V: Validator<T> + 'a,
V::Err: ToString,
pub fn validate_with<V>(&mut self, validator: V) -> &mut Selfwhere V: Validator<T> + 'a, V::Err: ToString,
Registers a validator.
Example
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: Debug + ToString,
impl<T> Input<'_, T>where T: Clone + ToString + FromStr, <T as FromStr>::Err: Debug + ToString,
sourcepub fn interact_text(&mut self) -> Result<T>
pub fn interact_text(&mut 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);
}
}
}
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(&mut self, term: &Term) -> Result<T>
pub fn interact_text_on(&mut self, term: &Term) -> Result<T>
Like interact_text
but allows a specific terminal to be set.
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(&mut self) -> Result<T>
pub fn interact(&mut 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(&mut self, term: &Term) -> Result<T>
pub fn interact_on(&mut 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();
}