Skip to content

Commit

Permalink
Refactor rgb colors using const_missing. Add rgb color tests. Bug fix…
Browse files Browse the repository at this point in the history
…es -- all tests now pass
  • Loading branch information
rleber committed Jun 27, 2011
1 parent 4e36b1f commit f853f4a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
35 changes: 20 additions & 15 deletions lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ def self.using_color_scheme?
colors.each do |color|
const_set "ON_#{color}", const_get(color).sub(/\d+/) {|digits| (digits.to_i + 10).to_s }
end

# For RGB colors:
def self.const_missing(name)
if name.to_s =~ /^(ON_)?RGB_([A-F0-9]{6})$/ # RGB color
on = $1
rgb = $2.scan(/../).map{|part| part.to_i(16)} # Split into RGB parts as integers
code = 16 + rgb.inject(0) {|kode, color| kode*6 + (color/256.0*6.0).floor}
prefix = on ? 48 : 38
"\e[#{prefix};5;#{code}m"
else
raise NameError, "Bad color or uninitialized constant #{name}"
end
end

#
# Create an instance of HighLine, connected to the streams _input_
Expand Down Expand Up @@ -339,20 +352,12 @@ def self.color( string, *colors )

# In case you just want the color code, without the embedding and the CLEAR
def self.color_code(*colors)
original_colors = colors
colors = colors.map do |c|
if self.using_color_scheme? and self.color_scheme.include? c
self.color_scheme[c]

colors = colors.flatten.map do |c|
if using_color_scheme? and color_scheme.include? c
color_code(color_scheme[c])
elsif c.is_a? Symbol
if c.to_s =~ /^(on_)?rgb_([a-fA-F0-9]{6})$/ # RGB color
on = $1
rgb = $2.scan(/../).map{|part| part.to_i(16)} # Split into RGB parts as integers
code = 16 + rgb.inject(0) {|kode, color| kode*6 + (color/256.0*6.0).floor}
prefix = on ? 48 : 38
"\e[#{prefix};5;#{code}m"
else
self.const_get(c.to_s.upcase)
end
const_get(c.to_s.upcase)
else
c
end
Expand Down Expand Up @@ -567,11 +572,11 @@ def gather( )
@answers << ask(@question)
@gather -= 1
end
when String, Regexp
when ::String, Regexp
@answers << ask(@question)

original_question.question = ""
until (@gather.is_a?(String) and @answers.last.to_s == @gather) or
until (@gather.is_a?(::String) and @answers.last.to_s == @gather) or
(@gather.is_a?(Regexp) and @answers.last.to_s =~ @gather)
@question = original_question
@answers << ask(@question)
Expand Down
2 changes: 1 addition & 1 deletion lib/highline/color_scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def to_symbol( t )
# Return a normalized representation of a color setting.
def to_constant( v )
v = v.to_s if v.is_a?(Symbol)
if v.is_a?(String) then
if v.is_a?(::String) then
HighLine.const_get(v.upcase)
else
v
Expand Down
2 changes: 1 addition & 1 deletion lib/highline/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def index=( style )
@index = style

# Default settings.
if @index == :none or @index.is_a?(String)
if @index == :none or @index.is_a?(::String)
@index_suffix = " "
@select_by = :name
end
Expand Down
3 changes: 3 additions & 0 deletions lib/highline/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def change_case( answer_string )
# Pathname:: Same as File, save that a Pathname object is
# returned.
# String:: Answer is converted with Kernel.String().
# HighLine::String:: Answer is converted with HighLine::String()
# Regexp:: Answer is fed to Regexp.new().
# Symbol:: The method to_sym() is called on answer and
# the result returned.
Expand All @@ -301,6 +302,8 @@ def change_case( answer_string )
def convert( answer_string )
if @answer_type.nil?
answer_string
elsif @answer_type == HighLine::String
HighLine::String(answer_string)
elsif [Float, Integer, String].include?(@answer_type)
Kernel.send(@answer_type.to_s.to_sym, answer_string)
elsif @answer_type == Symbol
Expand Down
10 changes: 10 additions & 0 deletions test/tc_highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ def test_color

@output.truncate(@output.rewind)

@terminal.say("This should be <%= NONE %>none<%= CLEAR %>!")
assert_equal("This should be \e[38mnone\e[0m!\n", @output.string)

@output.truncate(@output.rewind)

@terminal.say("This should be <%= RGB_906030 %>rgb_906030<%= CLEAR %>!")
assert_equal("This should be \e[38;5;137mrgb_906030\e[0m!\n", @output.string)

@output.truncate(@output.rewind)

# turn off color
old_setting = HighLine.use_color?
assert_nothing_raised(Exception) { HighLine.use_color = false }
Expand Down

0 comments on commit f853f4a

Please sign in to comment.