diff --git a/lib/ap/core_ext/string.rb b/lib/ap/core_ext/string.rb index 4b8d29c..f922403 100755 --- a/lib/ap/core_ext/string.rb +++ b/lib/ap/core_ext/string.rb @@ -4,11 +4,15 @@ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ class String + ANSI_ESCAPE = "\033" + ANSI_COLOR_BASE = 30 + ANSI_BRIGHT = 1 + ANSI_NORMAL = 0 [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i| if STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb' - define_method color do "\033[1;#{30+i}m#{self}\033[0m" end - define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end + define_method color do "#{ANSI_ESCAPE}[#{ANSI_BRIGHT};#{ANSI_COLOR_BASE+i}m#{self}#{ANSI_ESCAPE}[#{ANSI_NORMAL}m" end + define_method :"#{color}ish" do "#{ANSI_ESCAPE}[#{ANSI_NORMAL};#{ANSI_COLOR_BASE+i}m#{self}#{ANSI_ESCAPE}[#{ANSI_NORMAL}m" end else define_method color do self end alias_method :"#{color}ish", color diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index db7b3ed..b63d60c 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -164,11 +164,11 @@ EOS it "colored multiline (default)" do @hash.ai.should == <<-EOS.strip { - 1\e[1;30m => \e[0m{ - :sym\e[1;30m => \e[0m{ - \"str\"\e[1;30m => \e[0m{ - [ 1, 2, 3 ]\e[1;30m => \e[0m{ - { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m + 1\e[0;37m => \e[0m{ + :sym\e[0;37m => \e[0m{ + \"str\"\e[0;37m => \e[0m{ + [ 1, 2, 3 ]\e[0;37m => \e[0m{ + { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } @@ -180,11 +180,11 @@ EOS it "colored multiline indented" do @hash.ai(:indent => 2).should == <<-EOS.strip { - 1\e[1;30m => \e[0m{ - :sym\e[1;30m => \e[0m{ - \"str\"\e[1;30m => \e[0m{ - [ 1, 2, 3 ]\e[1;30m => \e[0m{ - { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m + 1\e[0;37m => \e[0m{ + :sym\e[0;37m => \e[0m{ + \"str\"\e[0;37m => \e[0m{ + [ 1, 2, 3 ]\e[0;37m => \e[0m{ + { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } @@ -194,7 +194,7 @@ EOS end it "colored single line" do - @hash.ai(:multiline => false).should == "{ 1\e[1;30m => \e[0m{ :sym\e[1;30m => \e[0m{ \"str\"\e[1;30m => \e[0m{ [ 1, 2, 3 ]\e[1;30m => \e[0m{ { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m } } } } }" + @hash.ai(:multiline => false).should == "{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }" end end diff --git a/spec/string_spec.rb b/spec/string_spec.rb index 6cdd8d8..009333c 100644 --- a/spec/string_spec.rb +++ b/spec/string_spec.rb @@ -14,5 +14,7 @@ describe "String extensions" do it "should have black and pale colors" do "black".send(:black).should == "black".send(:grayish) "pale".send(:pale).should == "pale".send(:whiteish) + "pale".send(:pale).should == "\e[0;37mpale\e[0m" + "whiteish".send(:whiteish).should == "\e[0;37mwhiteish\e[0m" end end