HighLine:HighLine#color の挙動、中身

  • lib/highline/import.rb で delegate されていないので、HighLine.new せずに color では使用できない
  • 普通は、say などの ERB の中で使うから メソッド単体で使用(記述)できるのか?
  • メソッド内で呼ばれるのは、HighLine.use_color?, HighLine.using_color_scheme?, HighLine.color_scheme
  • コード処理としては、@@color_scheme に含まれているものは、@@color_scheme[c] で返し、CLEAR など HighLine で定義されていてシンボルで渡されたものは、 HighLine.const_get(c.to_s.upcase) で返して、HighLine::CLEAR など定数で渡されたものはそのまま返す
>> require "highline/import"
=> true
>> class HighLine
>>   def color( string, *colors )
>>     return string unless self.class.use_color?
>>     
?>     colors.map! do |c|
?>       if self.class.using_color_scheme? and self.class.color_scheme.include? c
>>         p "* 1"
>>         self.class.color_scheme[c]
>>       elsif c.is_a? Symbol
>>         p "* 2"
>>         self.class.const_get(c.to_s.upcase)
>>       else
?>         p "* 3"
>>         c
>>       end
>>     end
>>     "#{colors.flatten.join}#{string}#{CLEAR}"
>>   end
>> end
=> nil
>> HighLine.new.color("foo", :BOLD)
"* 2"
=> "\e[1mfoo\e[0m"
>> HighLine.new.color("foo", HighLine::BOLD)
"* 3"
=> "\e[1mfoo\e[0m"
>> ft = HighLine::ColorScheme.new {|cs| cs[:headline] = [ :bold, :yellow, :on_black ]}
=> #<HighLine::ColorScheme:0xb79d31c4 @scheme={"headline"=>["\e[1m", "\e[33m", "\e[40m"]}>
>> HighLine.color_scheme = ft
=> #<HighLine::ColorScheme:0xb79d31c4 @scheme={"headline"=>["\e[1m", "\e[33m", "\e[40m"]}>
>> HighLine.new.color("foo")
=> "foo\e[0m"
>> HighLine.new.color("foo", :headline)
"* 1"
=> "\e[1m\e[33m\e[40mfoo\e[0m"
>> HighLine.new.color("foo", :bold)
"* 2"
=> "\e[1mfoo\e[0m"
>> HighLine.new.color("foo", :clear)
"* 2"
=> "\e[0mfoo\e[0m"
>> HighLine.new.color("foo", :CLEAR)
"* 2"
=> "\e[0mfoo\e[0m"