[macruby-changes] [4673] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:57:39 PDT 2010


Revision: 4673
          http://trac.macosforge.org/projects/ruby/changeset/4673
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:57:37 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Stash start on colorizing

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/Rakefile
    DietRB/trunk/lib/irb/formatter.rb

Added Paths:
-----------
    DietRB/trunk/lib/irb/ext/colorize.rb
    DietRB/trunk/spec/colorize_spec.rb

Modified: DietRB/trunk/Rakefile
===================================================================
--- DietRB/trunk/Rakefile	2010-10-08 10:57:26 UTC (rev 4672)
+++ DietRB/trunk/Rakefile	2010-10-08 10:57:37 UTC (rev 4673)
@@ -13,7 +13,7 @@
 
 desc "Run dietrb with ruby19"
 task :run do
-  sh "ruby19 -Ilib ./bin/dietrb -r irb/ext/completion"
+  sh "ruby19 -Ilib ./bin/dietrb -r irb/ext/completion -r irb/ext/colorize"
 end
 
 begin

Added: DietRB/trunk/lib/irb/ext/colorize.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/colorize.rb	                        (rev 0)
+++ DietRB/trunk/lib/irb/ext/colorize.rb	2010-10-08 10:57:37 UTC (rev 4673)
@@ -0,0 +1,120 @@
+module IRB
+  class ColoredFormatter < Formatter
+    #
+    # Terminal escape codes for colors.
+    #
+    module Color
+      COLORS = {
+        :nothing      => '0;0',
+        :black        => '0;30',
+        :red          => '0;31',
+        :green        => '0;32',
+        :brown        => '0;33',
+        :blue         => '0;34',
+        :cyan         => '0;36',
+        :purple       => '0;35',
+        :light_gray   => '0;37',
+        :dark_gray    => '1;30',
+        :light_red    => '1;31',
+        :light_green  => '1;32',
+        :yellow       => '1;33',
+        :light_blue   => '1;34',
+        :light_cyan   => '1;36',
+        :light_purple => '1;35',
+        :white        => '1;37',
+      }
+      
+      #
+      # Return the escape code for a given color.
+      #
+      def self.escape(key)
+        COLORS.key?(key) && "\033[#{COLORS[key]}m"
+      end
+      
+      CLEAR = escape(:nothing)
+    end
+    
+    #
+    # Default Wirble color scheme.
+    # 
+    DEFAULT_COLOR_SCHEME = {
+      # delimiter colors
+      :on_comma           => :blue,
+      :on_op              => :blue,
+      
+      # container colors (hash and array)
+      :on_lbrace          => :green,
+      :on_rbrace          => :green,
+      :on_lbracket        => :green,
+      :on_rbracket        => :green,
+      
+      # object colors
+      :open_object        => :light_red,
+      :object_class       => :white,
+      :object_addr_prefix => :blue,
+      :object_line_prefix => :blue,
+      :close_object       => :light_red,
+      
+      # symbol colors
+      :on_ident           => :yellow, # hmm ident...
+      :on_symbeg          => :yellow,
+      
+      # string colors
+      :on_tstring_beg     => :red,
+      :on_tstring_content => :cyan,
+      :on_tstring_end     => :red,
+      
+      # misc colors
+      :on_int             => :cyan,
+      :keyword            => :green,
+      :class              => :light_green,
+      :range              => :red,
+    }
+    
+    #
+    # Fruity testing colors.
+    # 
+    TESTING_COLOR_SCHEME = {
+      :comma            => :red,
+      :refers           => :red,
+      :open_hash        => :blue,
+      :close_hash       => :blue,
+      :open_array       => :green,
+      :close_array      => :green,
+      :open_object      => :light_red,
+      :object_class     => :light_green,
+      :object_addr      => :purple,
+      :object_line      => :light_purple,
+      :close_object     => :light_red,
+      :symbol           => :yellow,
+      :symbol_prefix    => :yellow,
+      :number           => :cyan,
+      :string           => :cyan,
+      :keyword          => :white,
+      :range            => :light_blue,
+    }
+    
+    attr_reader :colors
+    
+    def colors
+      @colors ||= {}.update(DEFAULT_COLOR_SCHEME)
+    end
+    
+    def colorize(str)
+      Ripper.lex(str).map do |_, type, token|
+        p type, token
+        if color = colors[type]
+          "#{Color.escape(color)}#{token}#{Color::CLEAR}"
+        else
+          token
+        end
+      end.join
+    end
+    
+    def result(object)
+      "=> #{colorize(object.inspect)}"
+    end
+  end
+end
+
+IRB.formatter = IRB::ColoredFormatter.new
\ No newline at end of file

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:57:26 UTC (rev 4672)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:57:37 UTC (rev 4673)
@@ -1,6 +1,6 @@
 module IRB
-  def self.formatter
-    @formatter ||= Formatter.new
+  class << self
+    attr_accessor :formatter
   end
   
   class Formatter
@@ -46,4 +46,6 @@
       end
     end
   end
-end
\ No newline at end of file
+end
+
+IRB.formatter = IRB::Formatter.new
\ No newline at end of file

Added: DietRB/trunk/spec/colorize_spec.rb
===================================================================
--- DietRB/trunk/spec/colorize_spec.rb	                        (rev 0)
+++ DietRB/trunk/spec/colorize_spec.rb	2010-10-08 10:57:37 UTC (rev 4673)
@@ -0,0 +1,27 @@
+require File.expand_path('../spec_helper', __FILE__)
+require 'irb/ext/colorize'
+
+main = self
+  
+describe "IRB::ColoredFormatter" do
+  before do
+    @formatter = IRB::ColoredFormatter.new
+    @context = IRB::Context.new(main)
+  end
+  
+  it "colorizes a constant" do
+    @formatter.result("Hash").should == "=> \e[1;32mHash\e[0;0m"
+  end
+  
+  it "colorizes a String" do
+    @formatter.result("foo bar").should == "=> \e[0;31m\"\e[0;0m\e[0;36mfoo bar\e[0;0m\e[0;31m\"\e[0;0m"
+  end
+  
+  it "colorizes a Hash" do
+    @formatter.result(:ok => :foo).should == "=> \e[0;32m{\e[0;0m\e[1;33m:\e[0;0m\e[1;33mok\e[0;0m\e[0;34m=>\e[0;0m\e[1;33m:\e[0;0m\e[1;33mfoo\e[0;0m\e[0;32m}\e[0;0m"
+  end
+  
+  it "colorizes an Array" do
+    @formatter.result([1, 2]).should == "=> \e[0;32m[\e[0;0m\e[0;36m1\e[0;0m\e[0;34m,\e[0;0m \e[0;36m2\e[0;0m\e[0;32m]\e[0;0m"
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/19d696af/attachment-0001.html>


More information about the macruby-changes mailing list