Skip to content

Commit 1067ec6

Browse files
committed
Fix prompt select current with more than 9 options
This fixes hitting <enter> on option 1 when there are more than 9 options (or 2 when more than 20 etc.) by interpreting such selections as final.
1 parent 717db29 commit 1067ec6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/cli/ui/prompt/interactive_options.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ def down
216216

217217
# n is 1-indexed selection
218218
# n == 0 if "Done" was selected in @multiple mode
219-
sig { params(n: Integer).void }
220-
def select_n(n)
219+
sig { params(n: Integer, final: T::Boolean).void }
220+
def select_n(n, final: false)
221221
if @multiple
222222
if n == 0
223223
@answer = []
@@ -230,7 +230,7 @@ def select_n(n)
230230
end
231231
elsif n == 0
232232
# Ignore pressing "0" when not in multiple mode
233-
elsif should_enter_select_mode?(n)
233+
elsif !final && should_enter_select_mode?(n)
234234
# When we have more than 9 options, we need to enter select mode
235235
# to avoid pre-selecting (e.g) 1 when the user wanted 10.
236236
# This also applies to 2 and 20+ options, 3/30+, etc.
@@ -302,7 +302,7 @@ def select_current
302302
# Prevent selection of invisible options
303303
return unless presented_options.any? { |_, num| num == @active }
304304

305-
select_n(@active)
305+
select_n(@active, final: true)
306306
end
307307

308308
sig { void }

test/cli/ui/prompt_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def test_interactive_options_with_more_than_9_options_doesnt_enter_select_mode_w
3737
assert_output_includes('You chose: 2')
3838
end
3939

40+
def test_interactive_options_with_more_than_9_options_allows_enter
41+
run_in_process(<<~RUBY)
42+
options = ("a".."j").map { |o| o + " was selected"}
43+
CLI::UI::Prompt.ask('question', options: options)
44+
RUBY
45+
46+
write("\n") # Choose option 1
47+
assert_output_includes('a was selected')
48+
end
49+
4050
# ^C is not handled; raises Interrupt, which may be handled by caller.
4151
def test_confirm_sigint
4252
jruby_skip('SIGINT shuts down the JVM instead of raising Interrupt')

0 commit comments

Comments
 (0)