Permalink
Browse files

Stash start on colorizing

  • Loading branch information...
1 parent 31f1777 commit 9777b48bd0276adc26d17728b4a340ec45469856 @alloy committed Apr 18, 2010
Showing with 154 additions and 4 deletions.
  1. +1 −1 Rakefile
  2. +1 −0 extensions/wirble/lib/wirble.rb
  3. +120 −0 lib/irb/ext/colorize.rb
  4. +5 −3 lib/irb/formatter.rb
  5. +27 −0 spec/colorize_spec.rb
View
@@ -13,7 +13,7 @@ end
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
@@ -436,6 +436,7 @@ def self.enable(custom_colors = nil)
def output_value
if @context.inspect?
val = Colorize.colorize(@context.last_value.inspect)
+ p val
printf @context.return_format, val
else
printf @context.return_format, @context.last_value
@@ -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
@@ -1,6 +1,6 @@
module IRB
- def self.formatter
- @formatter ||= Formatter.new
+ class << self
+ attr_accessor :formatter
end
class Formatter
@@ -46,4 +46,6 @@ def filter_backtrace(backtrace)
end
end
end
-end
+end
+
+IRB.formatter = IRB::Formatter.new
View
@@ -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

0 comments on commit 9777b48

Please sign in to comment.