Skip to content

Commit

Permalink
Added secret prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jan 17, 2020
1 parent 53ab46a commit 5296a0f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 19 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Changelog

## master

### Enhancements
* Added `secret` subcommand

### Breaking
* Changed structure of the theme

## 0.1.0

### Enhancements
* Added `confirm` subcommand
* Added `input` subcommand
* Added a theme for `dialoguer` library
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ SUBCOMMANDS:
confirm Prompt that returns `true` or `false`
help Prints this message or the help of the given subcommand(s)
input Prompt that takes user input and returns a string
secret Prompt that takes user input, hides it from the terminal, and returns a string
```

### Library
Expand Down Expand Up @@ -119,6 +120,7 @@ fn main() {

* [Confirm Prompt](#confirm-prompt)
* [Input Prompt](#input-prompt)
* [Secret Prompt](#secret-prompt)

### Confirm Prompt

Expand Down Expand Up @@ -173,6 +175,34 @@ OPTIONS:
-m, --message <message> Message for the prompt
```

### Secret Prompt

Prompt that takes user input, hides it from the terminal, and returns a string

<p align="center">
<img src="media/secret.svg" alt="Enquirer Secret Prompt" width="750">
</p>

#### Usage

```
enquirer-secret 0.1.0
Prompt that takes user input, hides it from the terminal, and returns a string
USAGE:
enquirer secret [FLAGS] [OPTIONS] --message <message>
FLAGS:
-a, --allow-empty Allow empty secret
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-c, --confirm <confirm> Enable confirmation prompt with this message
-e, --error <error> Error message when secrets doesn't match during confirmation
-m, --message <message> Message for the prompt
```

## About

### Changelog
Expand Down
1 change: 1 addition & 0 deletions media/secret.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod confirm;
mod input;
mod secret;
mod theme;

use console::set_colors_enabled;
Expand All @@ -21,6 +22,7 @@ struct Enquirer {
enum EnquirerSubcommand {
Confirm(confirm::Confirm),
Input(input::Input),
Secret(secret::Secret),
}

fn main() {
Expand All @@ -31,6 +33,7 @@ fn main() {
match program.cmd {
EnquirerSubcommand::Confirm(x) => x.run(),
EnquirerSubcommand::Input(x) => x.run(),
EnquirerSubcommand::Secret(x) => x.run(),
}
.unwrap();
}
48 changes: 48 additions & 0 deletions src/secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::theme::ColoredTheme;
use dialoguer::PasswordInput;
use std::io::Result;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
/// Prompt that takes user input, hides it from the terminal, and returns a string
pub struct Secret {
#[structopt(short, long)]
/// Message for the prompt
message: String,

/// Enable confirmation prompt with this message
#[structopt(short, long, requires = "error")]
confirm: Option<String>,

/// Error message when secrets doesn't match during confirmation
#[structopt(short, long, requires = "confirm")]
error: Option<String>,

/// Allow empty secret
#[structopt(short, long)]
allow_empty: bool,
}

impl Secret {
pub fn run(&self) -> Result<()> {
let theme = ColoredTheme::default();
let mut input = PasswordInput::with_theme(&theme);

input
.with_prompt(&self.message)
.allow_empty_password(self.allow_empty);

if self.confirm.is_some() {
input.with_confirmation(
self.confirm.as_ref().unwrap(),
&self.error.as_ref().unwrap(),
);
}

let value = input.interact()?;

println!("{}", value);

Ok(())
}
}
67 changes: 48 additions & 19 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ use std::fmt;
/// }
/// ```
pub struct ColoredTheme {
brblack: Style,
defaults_style: Style,
prompts_style: Style,
cyan: Style,
green: Style,
prefixes_style: Style,
values_style: Style,
errors_style: Style,
}

impl Default for ColoredTheme {
fn default() -> Self {
ColoredTheme {
brblack: Style::new().black().bold(),
defaults_style: Style::new().black().bold(),
prompts_style: Style::new().bold(),
cyan: Style::new().cyan(),
green: Style::new().green(),
prefixes_style: Style::new().cyan(),
values_style: Style::new().green(),
errors_style: Style::new().red(),
}
}
}
Expand All @@ -51,6 +53,18 @@ impl ColoredTheme {
}

impl Theme for ColoredTheme {
// Error
fn format_error(&self, f: &mut dyn fmt::Write, err: &str) -> fmt::Result {
write!(
f,
"{} {}",
self.errors_style.apply_to("✘"),
self.errors_style.apply_to(err)
)?;

Ok(())
}

// Input
fn format_singleline_prompt(
&self,
Expand All @@ -66,10 +80,10 @@ impl Theme for ColoredTheme {
write!(
f,
"{} {}{} {} ",
self.cyan.apply_to("?"),
self.prefixes_style.apply_to("?"),
self.prompts_style.apply_to(prompt),
self.brblack.apply_to(details),
self.brblack.apply_to("›"),
self.defaults_style.apply_to(details),
self.defaults_style.apply_to("›"),
)?;

Ok(())
Expand All @@ -85,10 +99,10 @@ impl Theme for ColoredTheme {
write!(
f,
"{} {} {} {}",
self.green.apply_to("✔"),
self.values_style.apply_to("✔"),
self.prompts_style.apply_to(prompt),
self.brblack.apply_to("·"),
self.green.apply_to(selection),
self.defaults_style.apply_to("·"),
self.values_style.apply_to(selection),
)?;

Ok(())
Expand All @@ -103,17 +117,23 @@ impl Theme for ColoredTheme {
) -> fmt::Result {
let details = match default {
None => self.empty(),
Some(true) => (self.brblack.apply_to("(Y/n)"), self.cyan.apply_to("true")),
Some(false) => (self.brblack.apply_to("(y/N)"), self.cyan.apply_to("false")),
Some(true) => (
self.defaults_style.apply_to("(Y/n)"),
self.prefixes_style.apply_to("true"),
),
Some(false) => (
self.defaults_style.apply_to("(y/N)"),
self.prefixes_style.apply_to("false"),
),
};

write!(
f,
"{} {} {} {} {} ",
self.cyan.apply_to("?"),
self.prefixes_style.apply_to("?"),
self.prompts_style.apply_to(prompt),
details.0,
self.brblack.apply_to("›"),
self.defaults_style.apply_to("›"),
details.1,
)?;

Expand All @@ -130,13 +150,22 @@ impl Theme for ColoredTheme {
write!(
f,
"{} {} {} {}",
self.green.apply_to("✔"),
self.values_style.apply_to("✔"),
self.prompts_style.apply_to(prompt),
self.brblack.apply_to("·"),
self.green
self.defaults_style.apply_to("·"),
self.values_style
.apply_to(if selection { "true" } else { "false" }),
)?;

Ok(())
}

// Password Selection
fn format_password_prompt_selection(
&self,
f: &mut dyn fmt::Write,
prompt: &str,
) -> fmt::Result {
self.format_single_prompt_selection(f, prompt, "********")
}
}

0 comments on commit 5296a0f

Please sign in to comment.