Skip to content

Commit

Permalink
Added input prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jan 17, 2020
1 parent 1e6a61c commit e86c6ea
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

/// 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::<String>::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(())
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod confirm;
mod input;
mod theme;

use console::set_colors_enabled;
Expand All @@ -19,6 +20,7 @@ struct Enquirer {
#[derive(Debug, StructOpt)]
enum EnquirerSubcommand {
Confirm(confirm::Confirm),
Input(input::Input),
}

fn main() {
Expand All @@ -28,6 +30,7 @@ fn main() {

match program.cmd {
EnquirerSubcommand::Confirm(x) => x.run(),
EnquirerSubcommand::Input(x) => x.run(),
}
.unwrap();
}
45 changes: 45 additions & 0 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -76,6 +120,7 @@ impl Theme for ColoredTheme {
Ok(())
}

// Confirm Selection
fn format_confirmation_prompt_selection(
&self,
f: &mut dyn fmt::Write,
Expand Down

0 comments on commit e86c6ea

Please sign in to comment.