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 1 commit
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
37 changes: 36 additions & 1 deletion lib/tty/prompt/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize(prompt, **options)
@filterable = options.fetch(:filter) { false }
@symbols = @prompt.symbols.merge(options.fetch(:symbols, {}))
@quiet = options.fetch(:quiet) { @prompt.quiet }
@submit_keys = keys_with_labels(options.fetch(:submit_keys) { default_submit_keys })
@submit_keys = init_action_keys(options.fetch(:submit_keys) { default_submit_keys })
@filter = []
@filter_cache = {}
@help = options[:help]
Expand Down Expand Up @@ -87,6 +87,41 @@ def default_submit_keys
%i[space return enter].freeze
end

# Ensure that if any EOL char is passed as an action key
# then all EOL chars are included (for cross-system compat)
# Maintain any custom labels.
#
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
# @return [Array[Hash]]
#
# @api private
def ensure_eol_compat(keys)
eol_symbols = %i[enter return].sort
key_symbols = keys.keys.sort
key_intersection = key_symbols & eol_symbols
Geekfish marked this conversation as resolved.
Show resolved Hide resolved

case key_intersection
when [], eol_symbols
keys
else
eol_label = keys[key_intersection[0]]
all_eol_keys = eol_symbols.reduce({}) { |hash, key|
hash.merge({key => eol_label})
}
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
all_eol_keys.merge(keys)
end
end

# Initialize any default or custom action keys
# setting up their labels and dealing with compat
#
# @return [Array[Hash]]
#
# @api private
def init_action_keys(keys)
Geekfish marked this conversation as resolved.
Show resolved Hide resolved
keys = keys_with_labels(keys)
ensure_eol_compat(keys)
end

# Select paginator based on the current navigation key
#
# @return [Paginator]
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/prompt/multi_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MultiList < List
def initialize(prompt, **options)
super
@selected = SelectedChoices.new
@select_keys = keys_with_labels(options.fetch(:select_keys, [:space]))
@select_keys = init_action_keys(options.fetch(:select_keys, [:space]))
@help = options[:help]
@echo = options.fetch(:echo, true)
@min = options[:min]
Expand Down
34 changes: 34 additions & 0 deletions spec/unit/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,40 @@ def exit_message(prompt, choice)
expect(actual_prompt_output).to eql(expected_prompt_output)
end

it "filters and chooses entry with enter even if only return is passed in submit_keys" do
prompt.input << "g" << "\n"
prompt.input.rewind

answer = prompt.select("What size?", %i[Small Medium Large Huge], filter: true, submit_keys: [:return])
expect(answer).to eql(:Large)

actual_prompt_output = prompt.output.string
expected_prompt_output =
output_helper("What size?", %w[Small Medium Large Huge], "Small", init: true,
hint: "Press #{up_down} arrow to move, Enter to select and letters to filter") +
output_helper("What size?", %w[Large Huge], "Large", hint: "Filter: \"g\"") +
exit_message("What size?", "Large")

expect(actual_prompt_output).to eql(expected_prompt_output)
end

it "filters and chooses entry with enter preserving the return key label" do
prompt.input << "g" << "\n"
prompt.input.rewind

answer = prompt.select("What size?", %i[Small Medium Large Huge], filter: true, submit_keys: [{return: ">ENTER<"}])
expect(answer).to eql(:Large)

actual_prompt_output = prompt.output.string
expected_prompt_output =
output_helper("What size?", %w[Small Medium Large Huge], "Small", init: true,
hint: "Press #{up_down} arrow to move, >ENTER< to select and letters to filter") +
output_helper("What size?", %w[Large Huge], "Large", hint: "Filter: \"g\"") +
exit_message("What size?", "Large")

expect(actual_prompt_output).to eql(expected_prompt_output)
end

it "filters and chooses entry with return and enter, with default key config" do
prompt.input << "g" << "\r\n"
prompt.input.rewind
Expand Down