Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configuring submit action key in select #163

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8ac4e83
Add test that demonstrates default list select behaviour
Geekfish May 14, 2021
993ced2
Fix typo in slider and list show_help
Geekfish May 14, 2021
44a179f
Support submit_keys and select_key in (multi_)select
Geekfish May 14, 2021
dce79bf
Fix hints for #select and #multi_select in README
Geekfish May 14, 2021
0a99a13
Rename select to select_choice
Geekfish May 16, 2021
9cb0352
Add enter to default submit keys
Geekfish May 16, 2021
5327ce9
Replace :tab with :ctrl_s as submit key in examples, docs and tests
Geekfish May 16, 2021
b24819d
Allow defining the label of submit keys
Geekfish May 16, 2021
135e62a
Add author handle in CHANGELOG
Geekfish May 16, 2021
395e6b1
Make multi-select select keys an array and allow custom labels
Geekfish May 16, 2021
3fa9f2c
Ensure cross-system compat for EOL chars in custom action keys
Geekfish May 16, 2021
1f1bc73
Rename clashing to conflicting and return exact conflict
Geekfish May 18, 2021
0dd6891
Fix multi-line block formatting and simplify implementation
Geekfish May 18, 2021
3df157e
Improve implementation of ensure_eol_compat
Geekfish May 18, 2021
e1b759d
Add examples and fix param/return types
Geekfish May 18, 2021
94e79a5
Reorder key-related functions in the same cluster
Geekfish May 18, 2021
1bcba3f
Set next version to unreleased and fix contributor name
Geekfish Jun 6, 2021
fd1cbdd
Rename :submit_keys to :confirm_keys
Geekfish Jun 6, 2021
e8bc200
Add support for non-special characters (incl. alphanumeric and symbols)
Geekfish Jun 6, 2021
d3ae0f8
Make default confirm keys for select a constant
Geekfish Jul 20, 2021
b4c71b6
Fix jruby-head bug which seems to ignore when statement
Geekfish Jul 21, 2021
bdbdd2b
Add DSL support for confirm_keys and select_keys
Geekfish Jul 30, 2021
5ec712f
Apply code review suggestions to support splat operator arguments
Geekfish Aug 1, 2021
e805155
Code review fixes
Geekfish Aug 2, 2021
9a70628
More code review fixes
Geekfish Aug 2, 2021
b43a32a
Make submit and select examples consistent, add missing yard option
Geekfish Aug 20, 2021
d42efff
Change conflicting keys error message
Geekfish Aug 20, 2021
9dea17b
Use helpers for confirm_keys DSL test
Geekfish Aug 20, 2021
7dc89e8
Fix yard warnings in list.rb and multi_list.rb
Geekfish Aug 21, 2021
2334b89
Fix hint description in README
Geekfish Aug 21, 2021
44945c4
Remove superfluous backticks from README code blocks
Geekfish Aug 22, 2021
33b4beb
Add missing newlines before code blocks in README
Geekfish Aug 22, 2021
79cd017
Fix help text in README and add key @param descriptions
Geekfish Aug 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change log

## [v0.24.0] - 2021-??-??
Geekfish marked this conversation as resolved.
Show resolved Hide resolved

### Changed
* Change #select and #multi_select to accept a :submit_keys option, instead of always defaulting to space and return
* Change #multi_select to accept a :select_key option, instead of always defaulting to space

### Fixed
* Fix #select and #multi_select hint to print out the correct submit_keys
* Fix typo in #slider show_help
Geekfish marked this conversation as resolved.
Show resolved Hide resolved

## [v0.23.1] - 2021-04-17

### Changed
Expand Down
155 changes: 137 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Or install it yourself as:
* [2.6.2.5 :per_page](#2625-per_page)
* [2.6.2.6 :disabled](#2626-disabled)
* [2.6.2.7 :filter](#2627-filter)
* [2.6.2.8 :submit_keys](#2628-submit_keys)
* [2.6.3 multi_select](#263-multi_select)
* [2.6.3.1 :cycle](#2631-cycle)
* [2.6.3.2 :enum](#2632-enum)
Expand All @@ -99,6 +100,8 @@ Or install it yourself as:
* [2.6.3.7 :filter](#2637-filter)
* [2.6.3.8 :min](#2638-min)
* [2.6.3.9 :max](#2639-max)
* [2.6.3.10 :submit_keys](#26310-submit_keys)
* [2.6.3.11 :select_key](#262311-select_key)
* [2.6.4 enum_select](#264-enum_select)
* [2.6.4.1 :per_page](#2641-per_page)
* [2.6.4.1 :disabled](#2641-disabled)
Expand Down Expand Up @@ -158,7 +161,7 @@ Asking question with list of options couldn't be easier using `select` like so:
```ruby
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move, Space or Enter to select)
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
# ‣ Scorpion
# Kano
# Jax
Expand Down Expand Up @@ -636,7 +639,7 @@ By default the choice name is also the value the prompt will return when selecte
choices = {small: 1, medium: 2, large: 3}
prompt.select("What size?", choices)
# =>
# What size? (Press ↑/↓ arrow to move and Enter to select)
# What size? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ small
# medium
# large
Expand All @@ -663,7 +666,7 @@ prompt.select("What size?") do |menu|
menu.choice name: "large", value: 3
end
# =>
# What size? (Press ↑/↓ arrow to move and Enter to select)
# What size? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ small
# ✘ medium (out of stock)
# large
Expand Down Expand Up @@ -698,7 +701,7 @@ For asking questions involving list of options use `select` method by passing th
```ruby
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ Scorpion
# Kano
# Jax
Expand All @@ -713,7 +716,7 @@ prompt.select("Choose your destiny?") do |menu|
menu.choice "Jax"
end
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ Scorpion
# Kano
# Jax
Expand All @@ -728,7 +731,7 @@ prompt.select("Choose your destiny?") do |menu|
menu.choice "Jax", -> { "Nice choice captain!" }
end
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ Scorpion
# Kano
# Jax
Expand All @@ -753,7 +756,7 @@ prompt.select("Choose your destiny?") do |menu|
menu.choice "Jax", 3
end
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# Scorpion
# Kano
# ‣ Jax
Expand All @@ -766,7 +769,7 @@ You can navigate the choices using the arrow keys or define your own key mapping
```ruby
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax), cycle: true)
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ Scorpion
# Kano
# Jax
Expand All @@ -785,7 +788,7 @@ prompt.select("Choose your destiny?") do |menu|
menu.choice "Jax", 3
end
# =>
# Choose your destiny? (Use ↑/↓ arrow or number (0-9) keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow or 1-9 number to move and Space or Enter to select)
# 1. Scorpion
# 2. Kano
# ‣ 3. Jax
Expand Down Expand Up @@ -813,7 +816,7 @@ You can configure active marker like so:
choices = %w(Scorpion Kano Jax)
prompt.select("Choose your destiny?", choices, symbols: { marker: ">" })
# =>
# Choose your destiny? (Use ↑/↓ and ←/→ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ to move and Space or Enter to select)
# > Scorpion
# Kano
# Jax
Expand All @@ -827,7 +830,7 @@ By default the menu is paginated if selection grows beyond `6` items. To change
letters = ("A".."Z").to_a
prompt.select("Choose your letter?", letters, per_page: 4)
# =>
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Enter to select)
# Which letter? (Press ↑/↓/←/→ arrow to move and press Space or Enter to select)
# ‣ A
# B
# C
Expand Down Expand Up @@ -870,7 +873,7 @@ The disabled choice will be displayed with a cross `✘` character next to it an
```ruby
prompt.select("Choose your destiny?", warriors)
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select)
# Choose your destiny? (Press ↑/↓ arrow to move and Space or Enter to select)
# ‣ Scorpion
# Kano
# ✘ Goro (injury)
Expand All @@ -887,7 +890,7 @@ To activate dynamic list searching on letter/number key presses use `:filter` op
warriors = %w(Scorpion Kano Jax Kitana Raiden)
prompt.select("Choose your destiny?", warriors, filter: true)
# =>
# Choose your destiny? (Use ↑/↓ arrow keys, press Enter to select, and letter keys to filter)
# Choose your destiny? (Press ↑/↓ arrow to move, Space or Enter to select and letters to filter)
# ‣ Scorpion
# Kano
# Jax
Expand Down Expand Up @@ -916,6 +919,52 @@ Filter characters can be deleted partially or entirely via, respectively, Backsp

If the user changes or deletes a filter, the choices previously selected remain selected.

#### 2.6.2.8 `:submit_keys`

You can configure which key(s) submit your selection (`:space` and `:return` by default):

```ruby
choices = %w(Scorpion Kano Jax)
prompt.select("Choose your destiny?", choices, submit_keys: [:tab])
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
# =>
# Choose your destiny? (Press ↑/↓ to move and Tab to select)
# ‣ Scorpion
# Kano
# Jax
```

This is particularly useful in conjunction with `:filter`, as you may have choices that include spaces.

```ruby
choices = ["Jax Sr", "Jax", "Jax Jr"]
prompt.select("Choose your destiny?", choices, submit_keys: [:tab], filter: true)
# =>
# Choose your destiny? (Press ↑/↓ to move and Tab to select and letters to filter)
# ‣ Jax Sr
# Jax
# Jax Jr
```

After the user types "Jax":

```ruby
# =>
# Choose your destiny? (Filter: "Jax")
# ‣ Jax Sr
# Jax
# Jax Jr
```

After the user presses space:
```ruby
# =>
# Choose your destiny? (Filter: "Jax ")
# ‣ Jax Sr
# Jax Jr
```

Please note that alphanumeric keys are *not* supported.

### 2.6.3 multi_select

For asking questions involving multiple selection list use `multi_select` method by passing the question and possible choices:
Expand All @@ -925,7 +974,7 @@ choices = %w(vodka beer wine whisky bourbon)
prompt.multi_select("Select drinks?", choices)
# =>
#
# Select drinks? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)"
# Select drinks? (Press ↑/↓ arrow keys to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)
piotrmurach marked this conversation as resolved.
Show resolved Hide resolved
# ‣ ⬡ vodka
# ⬡ beer
# ⬡ wine
Expand Down Expand Up @@ -1038,7 +1087,7 @@ By default the menu is paginated if selection grows beyond `6` items. To change
letters = ("A".."Z").to_a
prompt.multi_select("Choose your letter?", letters, per_page: 4)
# =>
# Which letter? (Use ↑/↓ and ←/→ arrow keys, press Space to select and Enter to finish)
# Which letter? (Press ↑/↓/←/→ arrow keys to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)
# ‣ ⬡ A
# ⬡ B
# ⬡ C
Expand All @@ -1065,7 +1114,7 @@ The disabled choice will be displayed with a cross `✘` character next to it an
```ruby
prompt.multi_select("Choose your favourite drink?", drinks)
# =>
# Choose your favourite drink? (Use ↑/↓ arrow keys, press Space to select and Enter to finish)
# Choose your favourite drink? (Press ↑/↓ arrow keys to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)
# ‣ ⬡ bourbon
# ✘ sake (out of stock)
# ⬡ vodka
Expand All @@ -1083,7 +1132,7 @@ header use the :echo option:
choices = %w(vodka beer wine whisky bourbon)
prompt.multi_select("Select drinks?", choices, echo: false)
# =>
# Select drinks?
# Select drinks? (Press ↑/↓ arrow keys to move, Space/Ctrl+A|R to select (all|rev) and Enter to finish)
# ⬡ vodka
# ⬢ 2) beer
# ⬡ 3) wine
Expand All @@ -1099,7 +1148,7 @@ To activate dynamic list filtering on letter/number typing, use the :filter opti
choices = %w(vodka beer wine whisky bourbon)
prompt.multi_select("Select drinks?", choices, filter: true)
# =>
# Select drinks? (Use ↑/↓ arrow keys, press Space to select and Enter to finish, and letter keys to filter)
# Select drinks? (Press ↑/↓ arrow keys to move, Space/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter)
# ‣ ⬡ vodka
# ⬡ beer
# ⬡ wine
Expand Down Expand Up @@ -1153,6 +1202,76 @@ prompt.multi_select("Select drinks?", choices, max: 3)
# ‣ ⬡ bourbon
```

#### 2.6.3.10 `:submit_keys`

This works similar to the [same option for `select`](#2628-submit_keys).
You can configure which key(s) submit your selection (`:return` by default):

```ruby
choices = %w(vodka beer wine whisky bourbon)
prompt.multi_select("Select drinks?", choices, submit_keys: [:tab])
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
# =>
# Select drinks? vodka, beer, whisky
# ⬢ vodka
# ⬢ beer
# ⬡ wine
# ⬢ whisky
# ‣ ⬡ bourbon
```

The user can then press the `tab` key to confirm their selection:
```ruby
# =>
# Select drinks? vodka, beer, whisky
```

Please note that alphanumeric keys are *not* supported.
Geekfish marked this conversation as resolved.
Show resolved Hide resolved

#### 2.6.3.11 `:select_key`

You can configure which key selects an option (`:space` default).
This is particularly useful in conjunction with the `filter` option, as you may have choices that include spaces.

```ruby
choices = ["gin", "gin tonic", "gin fizz", "beer"]
prompt.multi_select("Select drinks?", choices, filter: true, select_key: :tab)
# =>
# Select drinks? (Press ↑/↓ arrow keys to move, Tab/Ctrl+A|R to select (all|rev), Enter to finish and letters to filter)
# ‣ ⬡ gin
# ⬡ gin tonic
# ⬡ gin fizz
# ⬡ beer
```

After the user types "gin":

```ruby
# =>
# Select drinks? (Filter: "gin")
# ‣ ⬡ gin
# ⬡ gin tonic
# ⬡ gin fizz
```

and then presses space:

```ruby
# =>
# Select drinks? (Filter: "gin ")
# ‣ ⬡ gin tonic
# ⬡ gin fizz
```

The user can then press the `tab` key to select the options:
```ruby
# =>
# Select drinks? (Filter: "gin ")
# ‣ ⬢ gin tonic
# ⬡ gin fizz
```

Please note that alphanumeric keys are *not* supported.

### 2.6.4 enum_select

In order to ask for standard selection from indexed list you can use `enum_select` and pass question together with possible choices:
Expand Down
8 changes: 8 additions & 0 deletions examples/multi_select_filtered.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require_relative "../lib/tty-prompt"

prompt = TTY::Prompt.new

drinks = %w[vodka beer wine whisky bourbon]
prompt.multi_select("Choose your favourite drink?", drinks, filter: true)
11 changes: 11 additions & 0 deletions examples/select_filtered_with_spaces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true
piotrmurach marked this conversation as resolved.
Show resolved Hide resolved

require_relative "../lib/tty-prompt"

prompt = TTY::Prompt.new

warriors = ["Jax", "Jax Jr", "Kitana", "Raiden ft. Thunder"]

answer = prompt.select("Choose your destiny?", warriors, filter: true, submit_keys: [:return, :tab])

puts answer.inspect
Loading