From f853f4a534374ddb4df9f828fa63d3ef2d167d75 Mon Sep 17 00:00:00 2001 From: Richard LeBer Date: Mon, 27 Jun 2011 07:49:39 -0400 Subject: [PATCH] Refactor rgb colors using const_missing. Add rgb color tests. Bug fixes -- all tests now pass --- lib/highline.rb | 35 ++++++++++++++++++++--------------- lib/highline/color_scheme.rb | 2 +- lib/highline/menu.rb | 2 +- lib/highline/question.rb | 3 +++ test/tc_highline.rb | 10 ++++++++++ 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/highline.rb b/lib/highline.rb index 482c5aaa..70cb0ad7 100644 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -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_ @@ -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 @@ -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) diff --git a/lib/highline/color_scheme.rb b/lib/highline/color_scheme.rb index 23754a58..ead095b0 100644 --- a/lib/highline/color_scheme.rb +++ b/lib/highline/color_scheme.rb @@ -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 diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index 3fdace89..c4daf379 100644 --- a/lib/highline/menu.rb +++ b/lib/highline/menu.rb @@ -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 diff --git a/lib/highline/question.rb b/lib/highline/question.rb index 96c752fb..09c19234 100644 --- a/lib/highline/question.rb +++ b/lib/highline/question.rb @@ -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. @@ -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 diff --git a/test/tc_highline.rb b/test/tc_highline.rb index ee62a040..b118ae43 100644 --- a/test/tc_highline.rb +++ b/test/tc_highline.rb @@ -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 }