diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2019-04-08 20:17:23 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-08 20:17:23 -0300 |
commit | d0ee8766aa75bafa9cb1dad74b2eee9feeefccee (patch) | |
tree | d43b416e2aa7b32e0aeaaa993a323c1e5e3b323b | |
parent | defb6ec8d93448b79a4f20cd3d0c49da80e8df1f (diff) | |
parent | c553fbe9db01a1b969aa06bd609149daac736fb3 (diff) | |
download | highline-d0ee8766aa75bafa9cb1dad74b2eee9feeefccee.tar.gz |
Proudly Merge pull request #243 from The Biggest Mobprogramming Session Ever Ana06/mobprogramming
Add new capital_letter option to menu index
-rw-r--r-- | examples/menus.rb | 2 | ||||
-rwxr-xr-x | lib/highline/menu.rb | 14 | ||||
-rwxr-xr-x | test/test_menu.rb | 13 |
3 files changed, 22 insertions, 7 deletions
diff --git a/examples/menus.rb b/examples/menus.rb index 64e20e6..855f6b7 100644 --- a/examples/menus.rb +++ b/examples/menus.rb @@ -33,7 +33,7 @@ end say("\nThis is letter indexing...") choose do |menu| - menu.index = :letter + menu.index = :letter # Use :capital_letter for uppercase menu.index_suffix = ") " menu.prompt = "Please choose your favorite programming language? " diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index 8029b49..2ae1ad3 100755 --- a/lib/highline/menu.rb +++ b/lib/highline/menu.rb @@ -374,9 +374,10 @@ class HighLine end def map_items_by_index - if @index == :letter - l_index = "`" - all_items.map { l_index.succ!.dup } + if [:letter, :capital_letter].include?(@index) + # @ and ` are the previous ASCII characters to A and a respectively + prev_char = (@index == :capital_letter ? '@' : '`') + all_items.map { prev_char.succ!.dup } else (1..all_items.size).map(&:to_s) end @@ -436,7 +437,7 @@ class HighLine # 97 is the "a" letter at ascii table # Ex: For "a" it will return 0, and for "c" it will return 2 - index = selection.ord - 97 + index = selection.downcase.ord - 97 items[index] end @@ -517,8 +518,9 @@ class HighLine case @index when :number ["#{ix + 1}#{@index_suffix}", text] - when :letter - ["#{('a'.ord + ix).chr}#{@index_suffix}", text] + when :letter, :capital_letter + first_letter = (@index == :capital_letter ? 'A' : 'a') + ["#{(first_letter.ord + ix).chr}#{@index_suffix}", text] when :none [text, ""] else diff --git a/test/test_menu.rb b/test/test_menu.rb index 6278c7c..2a8cbaf 100755 --- a/test/test_menu.rb +++ b/test/test_menu.rb @@ -539,6 +539,19 @@ class TestMenu < Minitest::Test assert_equal(:load, selected) end + def test_select_by_capital_letter + @input << "B\n" + @input.rewind + + selected = @terminal.choose do |menu| + menu.index = :capital_letter + menu.choice :save + menu.choice :load + menu.choice :quit + end + assert_equal(:load, selected) + end + def test_shell @input << "save --some-option my_file.txt\n" @input.rewind |