diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 0000000..0b90d54 --- /dev/null +++ b/src/input.rs @@ -0,0 +1,41 @@ +use super::theme::ColoredTheme; +use dialoguer; +use std::io::Result; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +/// Prompt that takes user input and returns a string. +pub struct Input { + #[structopt(short, long)] + /// Message for the prompt + message: String, + + #[structopt(short, long)] + /// Default value for the prompt + default: Option, + + /// Allow empty input. Conflicts with `default` + #[structopt(short, long, conflicts_with = "default")] + allow_empty: bool, +} + +impl Input { + pub fn run(&self) -> Result<()> { + let theme = ColoredTheme::default(); + let mut input = dialoguer::Input::::with_theme(&theme); + + input + .with_prompt(&self.message) + .allow_empty(self.allow_empty); + + if self.default.is_some() { + input.default(self.default.as_ref().unwrap().to_string()); + } + + let value = input.interact()?; + + println!("{}", value); + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 1b30e20..492a4a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod confirm; +mod input; mod theme; use console::set_colors_enabled; @@ -19,6 +20,7 @@ struct Enquirer { #[derive(Debug, StructOpt)] enum EnquirerSubcommand { Confirm(confirm::Confirm), + Input(input::Input), } fn main() { @@ -28,6 +30,7 @@ fn main() { match program.cmd { EnquirerSubcommand::Confirm(x) => x.run(), + EnquirerSubcommand::Input(x) => x.run(), } .unwrap(); } diff --git a/src/theme.rs b/src/theme.rs index c2033ba..c491f87 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -51,6 +51,50 @@ impl ColoredTheme { } impl Theme for ColoredTheme { + // Input + fn format_singleline_prompt( + &self, + f: &mut dyn fmt::Write, + prompt: &str, + default: Option<&str>, + ) -> fmt::Result { + let details = match default { + Some(default) => format!(" [{}]", default), + None => "".to_string(), + }; + + write!( + f, + "{} {}{} {} ", + self.cyan.apply_to("?"), + self.prompts_style.apply_to(prompt), + self.brblack.apply_to(details), + self.brblack.apply_to("›"), + )?; + + Ok(()) + } + + // Input Selection + fn format_single_prompt_selection( + &self, + f: &mut dyn fmt::Write, + prompt: &str, + selection: &str, + ) -> fmt::Result { + write!( + f, + "{} {} {} {}", + self.green.apply_to("✔"), + self.prompts_style.apply_to(prompt), + self.brblack.apply_to("·"), + self.green.apply_to(selection), + )?; + + Ok(()) + } + + // Confirm fn format_confirmation_prompt( &self, f: &mut dyn fmt::Write, @@ -76,6 +120,7 @@ impl Theme for ColoredTheme { Ok(()) } + // Confirm Selection fn format_confirmation_prompt_selection( &self, f: &mut dyn fmt::Write,