diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-05-31 02:42:59 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-05-31 02:42:59 -0300 |
commit | c55d48cdcf9147718ecf4347695411ff860ccab7 (patch) | |
tree | 21ffd16706d8b6ce13a12b7bc8f742eb2dacb87d | |
parent | 60d8eb00d162a0911834339102314d573a15f602 (diff) | |
parent | ad24a60db16faed58b51a78cc222598428bafd3f (diff) | |
download | highline-c55d48cdcf9147718ecf4347695411ff860ccab7.tar.gz |
Merge pull request #194 from JEG2/aregic_coloringv2.0.0.pre.develop.7
Implement Menu's indices coloring - by Aregic
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | lib/highline/menu.rb | 42 | ||||
-rw-r--r-- | lib/highline/version.rb | 2 | ||||
-rw-r--r-- | test/test_menu.rb | 83 |
5 files changed, 135 insertions, 6 deletions
@@ -3,3 +3,5 @@ pkg .DS_Store coverage Gemfile.lock +*.swp +*.swo @@ -75,6 +75,18 @@ cli.choose do |menu| menu.choice(:ruby) { cli.say("Good choice!") } menu.choices(:python, :perl) { cli.say("Not from around here, are you?") } end + +## Using colored indices on Menus + +HighLine::Menu.index_color = :rgb_77bbff # set default index color + +cli.choose do |menu| + menu.index_color = :rgb_999999 # override default color of index + # you can also use constants like :blue + menu.prompt = "Please choose your favorite programming language? " + menu.choice(:ruby) { cli.say("Good choice!") } + menu.choices(:python, :perl) { cli.say("Not from around here, are you?") } +end ``` If you want to save some characters, you can inject/import HighLine methods on Kernel by doing the following. Just be sure to avoid name collisions in the top-level namespace. diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index 1560fde..2adc244 100644 --- a/lib/highline/menu.rb +++ b/lib/highline/menu.rb @@ -17,6 +17,21 @@ class HighLine # to {HighLine#choose} can detail all aspects of menu display and control. # class Menu < Question + # Pass +false+ to _color_ to turn off HighLine::Menu's + # index coloring. + # Pass a color and the Menu's indices will be colored. + def self.index_color=(color = :rgb_77bbff) + @index_color = color + end + + # Initialize it + self.index_color = false + + # Returns color used for coloring Menu's indices + def self.index_color + @index_color + end + # # Create an instance of HighLine::Menu. All customization is done # through the passed block, which should call accessors, {#choice} and @@ -32,7 +47,7 @@ class HighLine # menu.choices(:python, :perl) { say("Not from around here, are you?") } # end - def initialize( ) + def initialize # # Initialize Question objects with ignored values, we'll # adjust ours as needed. @@ -54,6 +69,10 @@ class HighLine @shell = false @nil_on_handled = false + # Used for coloring Menu indices. + # Set it to default. But you may override it. + @index_color = self.class.index_color + # Override Questions responses, we'll set our own. @responses = { } # Context for action code. @@ -130,6 +149,11 @@ class HighLine # Defaults to +false+. # attr_accessor :nil_on_handled + # + # The color of the index when displaying the menu. See Style class for + # available colors. + # + attr_accessor :index_color # # Adds _name_ to the list of available menu items. Menu items will be @@ -447,6 +471,14 @@ class HighLine end end + def decorate_index(index) + if index_color + HighLine.color(index, index_color) + else + index + end + end + # # Allows Menu objects to pass as Arrays, for use with HighLine.list(). # This method returns all menu items to be displayed, complete with @@ -455,14 +487,14 @@ class HighLine def to_ary( ) case @index when :number - @items.map { |i| "#{@items.index(i) + 1}#{@index_suffix}#{i.text}" } + @items.map { |i| decorate_index("#{@items.index(i) + 1}#{@index_suffix}") + "#{i.text}" } when :letter l_index = "`" - @items.map { |i| "#{l_index.succ!}#{@index_suffix}#{i.text}" } + @items.map { |i| decorate_index("#{l_index.succ!}#{@index_suffix}") + "#{i.text}" } when :none - @items.map { |i| "#{i.text}" } + @items.map { |i| decorate_index("#{i.text}") } else - @items.map { |i| "#{index}#{@index_suffix}#{i.text}" } + @items.map { |i| decorate_index("#{index}#{@index_suffix}") + "#{i.text}" } end end diff --git a/lib/highline/version.rb b/lib/highline/version.rb index d4d5537..b905781 100644 --- a/lib/highline/version.rb +++ b/lib/highline/version.rb @@ -2,5 +2,5 @@ class HighLine # The version of the installed library. - VERSION = "2.0.0-develop.6".freeze + VERSION = "2.0.0-develop.7".freeze end diff --git a/test/test_menu.rb b/test/test_menu.rb index f870245..bf4bcb5 100644 --- a/test/test_menu.rb +++ b/test/test_menu.rb @@ -235,6 +235,89 @@ class TestMenu < Minitest::Test assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string) end + def test_index_with_color + index_color = :rgb_77bbff + + @input << "Sample1\n" + @input.rewind + + @terminal.choose do |menu| + # Default: menu.index = :number + menu.index_color = index_color + + menu.choice "Sample1" + menu.choice "Sample2" + menu.choice "Sample3" + end + + assert_equal( + HighLine.color("1. ", index_color) + "Sample1\n" + + HighLine.color("2. ", index_color) + "Sample2\n" + + HighLine.color("3. ", index_color) + "Sample3\n" + + "? ", + @output.string + ) + + @output.truncate(@output.rewind) + @input.rewind + + @terminal.choose do |menu| + menu.index = :letter + menu.index_suffix = ") " + menu.index_color = index_color + + menu.choice "Sample1" + menu.choice "Sample2" + menu.choice "Sample3" + end + + assert_equal( + HighLine.color("a) ", index_color) + "Sample1\n" + + HighLine.color("b) ", index_color) + "Sample2\n" + + HighLine.color("c) ", index_color) + "Sample3\n? ", + @output.string + ) + + @output.truncate(@output.rewind) + @input.rewind + + @terminal.choose do |menu| + menu.index = :none + menu.index_color = index_color + + menu.choice "Sample1" + menu.choice "Sample2" + menu.choice "Sample3" + end + + assert_equal( + HighLine.color("Sample1", index_color) + "\n" + + HighLine.color("Sample2", index_color) + "\n" + + HighLine.color("Sample3", index_color) + "\n? ", + @output.string + ) + + @output.truncate(@output.rewind) + @input.rewind + + @terminal.choose do |menu| + menu.index = "*" + menu.index_color = index_color + + menu.choice "Sample1" + menu.choice "Sample2" + menu.choice "Sample3" + end + + colored_asterix = HighLine.color("* ", index_color) + assert_equal( + "#{colored_asterix}Sample1\n" + + "#{colored_asterix}Sample2\n" + + "#{colored_asterix}Sample3\n? ", + @output.string + ) + end + def test_layouts @input << "save\n" @input.rewind |