Skip to content

Commit

Permalink
Added sort prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jan 18, 2020
1 parent 384e35f commit 52ebaf1
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements
* Added `multi-select` subcommand
* Added `select` subcommand
* Added `sort` subcommand

### Breaking
* Removed versions from subcommands
Expand Down
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ FLAGS:
-V, --version Prints version information
SUBCOMMANDS:
confirm Prompt that returns `true` or `false`
confirm Prompt that returns `true` or `false` (as strings)
help Prints this message or the help of the given subcommand(s)
input Prompt that takes user input and returns a string
multi-select Prompt that allows the user to select multiple items from a list of options
secret Prompt that takes user input, hides it from the terminal, and returns a string
select Prompt that allows the user to select from a list of options
sort Prompt that allows the user to sort items in a list
```

### Library
Expand Down Expand Up @@ -127,10 +128,11 @@ fn main() {
* [Secret Prompt](#secret-prompt)
* [Select Prompt](#select-prompt)
* [Multi Select Prompt](#multi-select-prompt)
* [Sort Prompt](#sort-prompt)

### Confirm Prompt

Prompt that returns `true` or `false` (as strings).
Prompt that returns `true` or `false` (as strings)

<p align="center">
<img src="media/confirm.svg" alt="Enquirer Confirm Prompt" width="750">
Expand All @@ -140,7 +142,7 @@ Prompt that returns `true` or `false` (as strings).

```
enquirer-confirm 0.2.0
Prompt that returns `true` or `false`
Prompt that returns `true` or `false` (as strings)
USAGE:
enquirer confirm [FLAGS] [OPTIONS] --message <message>
Expand All @@ -155,7 +157,7 @@ OPTIONS:

### Input Prompt

Prompt that takes user input and returns a string.
Prompt that takes user input and returns a string

<p align="center">
<img src="media/input.svg" alt="Enquirer Input Prompt" width="750">
Expand Down Expand Up @@ -265,6 +267,35 @@ ARGS:
<items>... Items that can be selected
```

### Sort Prompt

Prompt that allows the user to sort items in a list

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

#### Usage

```
enquirer-sort 0.2.0
Prompt that allows the user to sort items in a list
USAGE:
enquirer sort [FLAGS] --message <message> [items]...
FLAGS:
-h, --help Prints help information
--no-inline Do not print the sorted items on the prompt line
-p, --paged Enables paging. Uses your terminal size
OPTIONS:
-m, --message <message> Message for the prompt
ARGS:
<items>... Items that can be sorted
```

## About

### Roadmap
Expand Down
1 change: 1 addition & 0 deletions media/sort.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Result;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
/// Prompt that returns `true` or `false`
/// Prompt that returns `true` or `false` (as strings)
pub struct Confirm {
#[structopt(short, long)]
/// Message for the prompt
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod input;
mod multi_select;
mod secret;
mod select;
mod sort;
mod theme;

use console::set_colors_enabled;
Expand All @@ -28,6 +29,7 @@ enum EnquirerSubcommand {
Secret(secret::Secret),
MultiSelect(multi_select::MultiSelect),
Select(select::Select),
Sort(sort::Sort),
}

fn main() {
Expand All @@ -41,6 +43,7 @@ fn main() {
EnquirerSubcommand::Secret(x) => x.run(),
EnquirerSubcommand::MultiSelect(x) => x.run(),
EnquirerSubcommand::Select(x) => x.run(),
EnquirerSubcommand::Sort(x) => x.run(),
}
.unwrap();
}
52 changes: 52 additions & 0 deletions src/sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::theme::ColoredTheme;
use dialoguer::OrderList;
use std::io::Result;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
/// Prompt that allows the user to sort items in a list
pub struct Sort {
#[structopt(short, long)]
/// Message for the prompt
message: String,

/// Enables paging. Uses your terminal size
#[structopt(short, long)]
paged: bool,

/// Do not print the sorted items on the prompt line
#[structopt(long)]
no_inline: bool,

/// Items that can be sorted
items: Vec<String>,
}

impl Sort {
pub fn run(&self) -> Result<()> {
let item_len = self.items.len();

if item_len == 0 {
return Ok(());
}

let theme = ColoredTheme::default()
.set_sort(true)
.inline_selections(!self.no_inline);
let mut input = OrderList::with_theme(&theme);

input
.with_prompt(&self.message)
.paged(self.paged)
.clear(true)
.items(&self.items);

let value = input.interact()?;

for i in value {
println!("{}", self.items[i]);
}

Ok(())
}
}
35 changes: 32 additions & 3 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub struct ColoredTheme {
pub errors_style: Style,
pub selected_style: Style,
pub unselected_style: Style,
/// Defaults to `true`
pub inline_selections: bool,
/// Defaults to `false`
pub is_sort: bool,
}

impl Default for ColoredTheme {
Expand All @@ -45,6 +48,7 @@ impl Default for ColoredTheme {
selected_style: Style::new().cyan().bold(),
unselected_style: Style::new(),
inline_selections: true,
is_sort: true,
}
}
}
Expand All @@ -66,6 +70,22 @@ impl ColoredTheme {
self
}

/// OrderList by default prints like Checkboxes. This function
/// allows the user to specify that the theme needs to use
/// a different style for sort.
///
/// # Examples
///
/// ```
/// use enquirer::ColoredTheme;
///
/// let theme = ColoredTheme::default().set_sort(true);
/// ```
pub fn set_sort(mut self, val: bool) -> Self {
self.is_sort = val;
self
}

fn empty(&self) -> (StyledObject<&str>, StyledObject<&str>) {
(
self.prompts_style.apply_to(""),
Expand Down Expand Up @@ -213,19 +233,28 @@ impl Theme for ColoredTheme {
) -> fmt::Result {
let strings = match style {
SelectionStyle::CheckboxCheckedSelected => (
self.values_style.apply_to("✔"),
self.values_style
.apply_to(if self.is_sort { "❯" } else { "✔" }),
self.selected_style.apply_to(text),
),
SelectionStyle::CheckboxCheckedUnselected => (
self.values_style.apply_to("✔"),
self.unselected_style.apply_to(text),
),
SelectionStyle::CheckboxUncheckedSelected => (
self.defaults_style.apply_to("✔"),
if self.is_sort {
self.defaults_style.apply_to(" ")
} else {
self.defaults_style.apply_to("✔")
},
self.selected_style.apply_to(text),
),
SelectionStyle::CheckboxUncheckedUnselected => (
self.defaults_style.apply_to("✔"),
if self.is_sort {
self.defaults_style.apply_to(" ")
} else {
self.defaults_style.apply_to("✔")
},
self.unselected_style.apply_to(text),
),
SelectionStyle::MenuSelected => (
Expand Down

0 comments on commit 52ebaf1

Please sign in to comment.