aboutsummaryrefslogtreecommitdiff
path: root/vendor/dialoguer/examples
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/dialoguer/examples')
-rw-r--r--vendor/dialoguer/examples/buffered.rs42
-rw-r--r--vendor/dialoguer/examples/completion.rs44
-rw-r--r--vendor/dialoguer/examples/confirm.rs70
-rw-r--r--vendor/dialoguer/examples/editor.rs10
-rw-r--r--vendor/dialoguer/examples/fuzzyselect.rs43
-rw-r--r--vendor/dialoguer/examples/history.rs51
-rw-r--r--vendor/dialoguer/examples/input.rs44
-rw-r--r--vendor/dialoguer/examples/multi_select.rs42
-rw-r--r--vendor/dialoguer/examples/paging.rs43
-rw-r--r--vendor/dialoguer/examples/password.rs18
-rw-r--r--vendor/dialoguer/examples/select.rs42
-rw-r--r--vendor/dialoguer/examples/sort.rs32
-rw-r--r--vendor/dialoguer/examples/wizard.rs81
13 files changed, 562 insertions, 0 deletions
diff --git a/vendor/dialoguer/examples/buffered.rs b/vendor/dialoguer/examples/buffered.rs
new file mode 100644
index 0000000..96f62bd
--- /dev/null
+++ b/vendor/dialoguer/examples/buffered.rs
@@ -0,0 +1,42 @@
+use console::Term;
+use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select, Sort};
+
+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();
+}
diff --git a/vendor/dialoguer/examples/completion.rs b/vendor/dialoguer/examples/completion.rs
new file mode 100644
index 0000000..76d790b
--- /dev/null
+++ b/vendor/dialoguer/examples/completion.rs
@@ -0,0 +1,44 @@
+use dialoguer::{theme::ColorfulTheme, Completion, Input};
+
+fn main() -> Result<(), std::io::Error> {
+ println!("Use the Right arrow or Tab to complete your command");
+ let completion = MyCompletion::default();
+ Input::<String>::with_theme(&ColorfulTheme::default())
+ .with_prompt("dialoguer")
+ .completion_with(&completion)
+ .interact_text()?;
+ Ok(())
+}
+
+struct MyCompletion {
+ options: Vec<String>,
+}
+
+impl Default for MyCompletion {
+ fn default() -> Self {
+ MyCompletion {
+ options: vec![
+ "orange".to_string(),
+ "apple".to_string(),
+ "banana".to_string(),
+ ],
+ }
+ }
+}
+
+impl Completion for MyCompletion {
+ /// Simple completion implementation based on substring
+ fn get(&self, input: &str) -> Option<String> {
+ let matches = self
+ .options
+ .iter()
+ .filter(|option| option.starts_with(input))
+ .collect::<Vec<_>>();
+
+ if matches.len() == 1 {
+ Some(matches[0].to_string())
+ } else {
+ None
+ }
+ }
+}
diff --git a/vendor/dialoguer/examples/confirm.rs b/vendor/dialoguer/examples/confirm.rs
new file mode 100644
index 0000000..3b1d3ca
--- /dev/null
+++ b/vendor/dialoguer/examples/confirm.rs
@@ -0,0 +1,70 @@
+use dialoguer::{theme::ColorfulTheme, Confirm};
+
+fn main() {
+ if Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you want to continue?")
+ .interact()
+ .unwrap()
+ {
+ println!("Looks like you want to continue");
+ } else {
+ println!("nevermind then :(");
+ }
+
+ if Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you really want to continue?")
+ .default(true)
+ .interact()
+ .unwrap()
+ {
+ println!("Looks like you want to continue");
+ } else {
+ println!("nevermind then :(");
+ }
+
+ if Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you really really want to continue?")
+ .default(true)
+ .show_default(false)
+ .wait_for_newline(true)
+ .interact()
+ .unwrap()
+ {
+ println!("Looks like you want to continue");
+ } else {
+ println!("nevermind then :(");
+ }
+
+ if Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you really really really want to continue?")
+ .wait_for_newline(true)
+ .interact()
+ .unwrap()
+ {
+ println!("Looks like you want to continue");
+ } else {
+ println!("nevermind then :(");
+ }
+
+ match Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you really really really really want to continue?")
+ .interact_opt()
+ .unwrap()
+ {
+ Some(true) => println!("Looks like you want to continue"),
+ Some(false) => println!("nevermind then :("),
+ None => println!("Ok, we can start over later"),
+ }
+
+ match Confirm::with_theme(&ColorfulTheme::default())
+ .with_prompt("Do you really really really really really want to continue?")
+ .default(true)
+ .wait_for_newline(true)
+ .interact_opt()
+ .unwrap()
+ {
+ Some(true) => println!("Looks like you want to continue"),
+ Some(false) => println!("nevermind then :("),
+ None => println!("Ok, we can start over later"),
+ }
+}
diff --git a/vendor/dialoguer/examples/editor.rs b/vendor/dialoguer/examples/editor.rs
new file mode 100644
index 0000000..5cc21db
--- /dev/null
+++ b/vendor/dialoguer/examples/editor.rs
@@ -0,0 +1,10 @@
+use dialoguer::Editor;
+
+fn main() {
+ if let Some(rv) = Editor::new().edit("Enter a commit message").unwrap() {
+ println!("Your message:");
+ println!("{}", rv);
+ } else {
+ println!("Abort!");
+ }
+}
diff --git a/vendor/dialoguer/examples/fuzzyselect.rs b/vendor/dialoguer/examples/fuzzyselect.rs
new file mode 100644
index 0000000..144d8b6
--- /dev/null
+++ b/vendor/dialoguer/examples/fuzzyselect.rs
@@ -0,0 +1,43 @@
+use dialoguer::{theme::ColorfulTheme, FuzzySelect};
+
+fn main() {
+ let selections = &[
+ "Ice Cream",
+ "Vanilla Cupcake",
+ "Chocolate Muffin",
+ "A Pile of sweet, sweet mustard",
+ "Carrots",
+ "Peas",
+ "Pistacio",
+ "Mustard",
+ "Cream",
+ "Banana",
+ "Chocolate",
+ "Flakes",
+ "Corn",
+ "Cake",
+ "Tarte",
+ "Cheddar",
+ "Vanilla",
+ "Hazelnut",
+ "Flour",
+ "Sugar",
+ "Salt",
+ "Potato",
+ "French Fries",
+ "Pizza",
+ "Mousse au chocolat",
+ "Brown sugar",
+ "Blueberry",
+ "Burger",
+ ];
+
+ let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
+ .with_prompt("Pick your flavor")
+ .default(0)
+ .items(&selections[..])
+ .interact()
+ .unwrap();
+
+ println!("Enjoy your {}!", selections[selection]);
+}
diff --git a/vendor/dialoguer/examples/history.rs b/vendor/dialoguer/examples/history.rs
new file mode 100644
index 0000000..0d69b27
--- /dev/null
+++ b/vendor/dialoguer/examples/history.rs
@@ -0,0 +1,51 @@
+use dialoguer::{theme::ColorfulTheme, History, Input};
+use std::{collections::VecDeque, process};
+
+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);
+ }
+ }
+}
+
+struct MyHistory {
+ max: usize,
+ history: VecDeque<String>,
+}
+
+impl Default for MyHistory {
+ fn default() -> Self {
+ MyHistory {
+ max: 4,
+ 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) {
+ if self.history.len() == self.max {
+ self.history.pop_back();
+ }
+ self.history.push_front(val.to_string());
+ }
+}
diff --git a/vendor/dialoguer/examples/input.rs b/vendor/dialoguer/examples/input.rs
new file mode 100644
index 0000000..8691c2e
--- /dev/null
+++ b/vendor/dialoguer/examples/input.rs
@@ -0,0 +1,44 @@
+use dialoguer::{theme::ColorfulTheme, Input};
+
+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);
+}
diff --git a/vendor/dialoguer/examples/multi_select.rs b/vendor/dialoguer/examples/multi_select.rs
new file mode 100644
index 0000000..97d3bae
--- /dev/null
+++ b/vendor/dialoguer/examples/multi_select.rs
@@ -0,0 +1,42 @@
+use dialoguer::{theme::ColorfulTheme, MultiSelect};
+
+fn main() {
+ let multiselected = &[
+ "Ice Cream",
+ "Vanilla Cupcake",
+ "Chocolate Muffin",
+ "A Pile of sweet, sweet mustard",
+ ];
+ let defaults = &[false, false, true, false];
+ let selections = MultiSelect::with_theme(&ColorfulTheme::default())
+ .with_prompt("Pick your food")
+ .items(&multiselected[..])
+ .defaults(&defaults[..])
+ .interact()
+ .unwrap();
+
+ if selections.is_empty() {
+ println!("You did not select anything :(");
+ } else {
+ println!("You selected these things:");
+ for selection in selections {
+ println!(" {}", multiselected[selection]);
+ }
+ }
+
+ let selections = MultiSelect::with_theme(&ColorfulTheme::default())
+ .with_prompt("Pick your food")
+ .items(&multiselected[..])
+ .defaults(&defaults[..])
+ .max_length(2)
+ .interact()
+ .unwrap();
+ if selections.is_empty() {
+ println!("You did not select anything :(");
+ } else {
+ println!("You selected these things:");
+ for selection in selections {
+ println!(" {}", multiselected[selection]);
+ }
+ }
+}
diff --git a/vendor/dialoguer/examples/paging.rs b/vendor/dialoguer/examples/paging.rs
new file mode 100644
index 0000000..63f9726
--- /dev/null
+++ b/vendor/dialoguer/examples/paging.rs
@@ -0,0 +1,43 @@
+use dialoguer::{theme::ColorfulTheme, Select};
+
+fn main() {
+ let selections = &[
+ "Ice Cream",
+ "Vanilla Cupcake",
+ "Chocolate Muffin",
+ "A Pile of sweet, sweet mustard",
+ "Carrots",
+ "Peas",
+ "Pistacio",
+ "Mustard",
+ "Cream",
+ "Banana",
+ "Chocolate",
+ "Flakes",
+ "Corn",
+ "Cake",
+ "Tarte",
+ "Cheddar",
+ "Vanilla",
+ "Hazelnut",
+ "Flour",
+ "Sugar",
+ "Salt",
+ "Potato",
+ "French Fries",
+ "Pizza",
+ "Mousse au chocolat",
+ "Brown sugar",
+ "Blueberry",
+ "Burger",
+ ];
+
+ let selection = Select::with_theme(&ColorfulTheme::default())
+ .with_prompt("Pick your flavor")
+ .default(0)
+ .items(&selections[..])
+ .interact()
+ .unwrap();
+
+ println!("Enjoy your {}!", selections[selection]);
+}
diff --git a/vendor/dialoguer/examples/password.rs b/vendor/dialoguer/examples/password.rs
new file mode 100644
index 0000000..d85df1b
--- /dev/null
+++ b/vendor/dialoguer/examples/password.rs
@@ -0,0 +1,18 @@
+use dialoguer::{theme::ColorfulTheme, Password};
+
+fn main() {
+ let password = Password::with_theme(&ColorfulTheme::default())
+ .with_prompt("Password")
+ .with_confirmation("Repeat password", "Error: the passwords don't match.")
+ .validate_with(|input: &String| -> Result<(), &str> {
+ if input.len() > 3 {
+ Ok(())
+ } else {
+ Err("Password must be longer than 3")
+ }
+ })
+ .interact()
+ .unwrap();
+
+ println!("Your password is {} characters long", password.len());
+}
diff --git a/vendor/dialoguer/examples/select.rs b/vendor/dialoguer/examples/select.rs
new file mode 100644
index 0000000..3382e8b
--- /dev/null
+++ b/vendor/dialoguer/examples/select.rs
@@ -0,0 +1,42 @@
+use dialoguer::{theme::ColorfulTheme, Select};
+
+fn main() {
+ let selections = &[
+ "Ice Cream",
+ "Vanilla Cupcake",
+ "Chocolate Muffin",
+ "A Pile of sweet, sweet mustard",
+ ];
+
+ let selection = Select::with_theme(&ColorfulTheme::default())
+ .with_prompt("Pick your flavor")
+ .default(0)
+ .items(&selections[..])
+ .interact()
+ .unwrap();
+
+ println!("Enjoy your {}!", selections[selection]);
+
+ let selection = Select::with_theme(&ColorfulTheme::default())
+ .with_prompt("Optionally pick your flavor")
+ .default(0)
+ .items(&selections[..])
+ .interact_opt()
+ .unwrap();
+
+ if let Some(selection) = selection {
+ println!("Enjoy your {}!", selections[selection]);
+ } else {
+ println!("You didn't select anything!");
+ }
+
+ let selection = Select::with_theme(&ColorfulTheme::default())
+ .with_prompt("Optionally pick your flavor, hint it might be on the second page")
+ .default(0)
+ .max_length(2)
+ .items(&selections[..])
+ .interact()
+ .unwrap();
+
+ println!("Enjoy your {}!", selections[selection]);
+}
diff --git a/vendor/dialoguer/examples/sort.rs b/vendor/dialoguer/examples/sort.rs
new file mode 100644
index 0000000..ae920aa
--- /dev/null
+++ b/vendor/dialoguer/examples/sort.rs
@@ -0,0 +1,32 @@
+use dialoguer::{theme::ColorfulTheme, Sort};
+
+fn main() {
+ let list = &[
+ "Ice Cream",
+ "Vanilla Cupcake",
+ "Chocolate Muffin",
+ "A Pile of sweet, sweet mustard",
+ ];
+ let sorted = Sort::with_theme(&ColorfulTheme::default())
+ .with_prompt("Order your foods by preference")
+ .items(&list[..])
+ .interact()
+ .unwrap();
+
+ println!("Your favorite item:");
+ println!(" {}", list[sorted[0]]);
+ println!("Your least favorite item:");
+ println!(" {}", list[sorted[sorted.len() - 1]]);
+
+ let sorted = Sort::with_theme(&ColorfulTheme::default())
+ .with_prompt("Order your foods by preference")
+ .items(&list[..])
+ .max_length(2)
+ .interact()
+ .unwrap();
+
+ println!("Your favorite item:");
+ println!(" {}", list[sorted[0]]);
+ println!("Your least favorite item:");
+ println!(" {}", list[sorted[sorted.len() - 1]]);
+}
diff --git a/vendor/dialoguer/examples/wizard.rs b/vendor/dialoguer/examples/wizard.rs
new file mode 100644
index 0000000..b914032
--- /dev/null
+++ b/vendor/dialoguer/examples/wizard.rs
@@ -0,0 +1,81 @@
+use std::error::Error;
+use std::net::IpAddr;
+
+use console::Style;
+use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
+
+#[derive(Debug)]
+#[allow(dead_code)]
+struct Config {
+ interface: IpAddr,
+ hostname: String,
+ use_acme: bool,
+ private_key: Option<String>,
+ cert: Option<String>,
+}
+
+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,
+ }))
+}
+
+fn main() {
+ match init_config() {
+ Ok(None) => println!("Aborted."),
+ Ok(Some(config)) => println!("{:#?}", config),
+ Err(err) => println!("error: {}", err),
+ }
+}