diff --git a/.gitignore b/.gitignore index b8c8e9cd..7828f4cc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ pkg .DS_Store coverage Gemfile.lock +*.swp +*.swo diff --git a/README.md b/README.md index 07042769..adae0173 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,12 @@ cli.say("This should be <%= color('bold', BOLD) %>!") # Menus: + +HighLine::Menu.use_color = true # coloring is off by default + 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?") } diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index 1560fdef..47811a2d 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 + + # The setting used to disable color output. + @use_color = true + + # Pass +false+ to _setting_ to turn off HighLine's color escapes. + def self.use_color=( setting ) + @use_color = setting + end + + # Returns true if HighLine is currently using color escapes. + def self.use_color? + @use_color + end + + # # Create an instance of HighLine::Menu. All customization is done # through the passed block, which should call accessors, {#choice} and @@ -53,6 +68,7 @@ def initialize( ) @layout = :list @shell = false @nil_on_handled = false + @index_color = :rgb_77bbff # Override Questions responses, we'll set our own. @responses = { } @@ -130,6 +146,11 @@ def initialize( ) # 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 +468,14 @@ def value_for_hash_selections(items, selections, details) end end + def decorate_index(index) + if self.class.use_color? + HighLine.color(index, self.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 +484,14 @@ def value_for_hash_selections(items, selections, details) 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 d4d55371..b9057818 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 f8702455..30cad4f5 100644 --- a/test/test_menu.rb +++ b/test/test_menu.rb @@ -19,6 +19,7 @@ def setup @input = StringIO.new @output = StringIO.new @terminal = HighLine.new(@input, @output) + HighLine::Menu.use_color = false end def test_choices @@ -235,6 +236,78 @@ def test_index assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string) end + def test_index_with_color + index_color = :rgb_77bbff + HighLine::Menu.use_color = true + + @input << "Sample1\n" + @input.rewind + + @terminal.choose do |menu| + # Default: menu.index = :number + + 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.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.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.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