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

Added index coloring functionality in menu #193

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pkg
.DS_Store
coverage
Gemfile.lock
*.swp
*.swo
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?") }
Expand Down
37 changes: 33 additions & 4 deletions lib/highline/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = { }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/highline/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
73 changes: 73 additions & 0 deletions test/test_menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down