[macruby-changes] [4675] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Oct 8 03:57:58 PDT 2010
Revision: 4675
http://trac.macosforge.org/projects/ruby/changeset/4675
Author: eloy.de.enige at gmail.com
Date: 2010-10-08 03:57:57 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Allow the prompt and result prefix ?\226?\128?\152=>?\226?\128?\153 be colorized too
From: Eloy Duran <eloy.de.enige at gmail.com>
Modified Paths:
--------------
DietRB/trunk/lib/irb/ext/colorize.rb
DietRB/trunk/spec/colorize_spec.rb
Modified: DietRB/trunk/lib/irb/ext/colorize.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/colorize.rb 2010-10-08 10:57:47 UTC (rev 4674)
+++ DietRB/trunk/lib/irb/ext/colorize.rb 2010-10-08 10:57:57 UTC (rev 4675)
@@ -28,7 +28,7 @@
# Return the escape code for a given color.
#
def self.escape(key)
- COLORS.key?(key) && "\033[#{COLORS[key]}m"
+ COLORS.key?(key) && "\e[#{COLORS[key]}m"
end
CLEAR = escape(:nothing)
@@ -38,6 +38,9 @@
# Default Wirble color scheme.
#
DEFAULT_COLOR_SCHEME = {
+ :prompt => :green,
+ :result_prefix => :light_purple,
+
# delimiter colors
:on_comma => :blue,
:on_op => :blue,
@@ -98,19 +101,28 @@
@colors ||= {}.update(DEFAULT_COLOR_SCHEME)
end
+ def colorize_token(type, token)
+ if color = colors[type]
+ "#{Color.escape(color)}#{token}#{Color::CLEAR}"
+ else
+ token
+ end
+ 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
+ Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
end
+ def prompt(context)
+ colorize_token(:prompt, super)
+ end
+
+ def result_prefix
+ colorize_token(:result_prefix, "=>")
+ end
+
def result(object)
- "=> #{colorize(object.inspect)}"
+ "#{result_prefix} #{colorize(object.inspect)}"
end
end
end
Modified: DietRB/trunk/spec/colorize_spec.rb
===================================================================
--- DietRB/trunk/spec/colorize_spec.rb 2010-10-08 10:57:47 UTC (rev 4674)
+++ DietRB/trunk/spec/colorize_spec.rb 2010-10-08 10:57:57 UTC (rev 4675)
@@ -6,6 +6,7 @@
describe "IRB::ColoredFormatter" do
before do
@formatter = IRB::ColoredFormatter.new
+ @formatter.colors.delete(:result_prefix)
@context = IRB::Context.new(main)
end
@@ -30,4 +31,14 @@
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
+
+ it "colorizes the prompt" do
+ @formatter.colors[:prompt] = :light_green
+ @formatter.prompt(@context).should == "\e[1;32m#{IRB::Formatter.new.prompt(@context)}\e[0;0m"
+ end
+
+ it "colorizes the result prefix" do
+ @formatter.colors[:result_prefix] = :light_red
+ @formatter.result("").should == "\e[1;31m=>\e[0;0m \e[0;31m\"\e[0;0m\e[0;31m\"\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/0c624413/attachment.html>
More information about the macruby-changes
mailing list