Better diff coloring #169

Merged
merged 3 commits into from Aug 25, 2012
View
31 lib/rspec/expectations/differ.rb
@@ -63,30 +63,37 @@ def context_lines
3
end
- def color(text, code)
- "\e[#{code}m#{text}\e[0m"
+ def color(text, color_code)
+ "\e[#{color_code}m#{text}\e[0m"
+ end
+
+ def red(text)
+ color(text, 31)
+ end
+
+ def green(text)
+ color(text, 32)
+ end
+
+ def blue(text)
+ color(text, 34)
end
def color_diff(diff)
return diff unless RSpec::Matchers.configuration.color?
- red = 31
- green = 32
- blue = 34
-
- lines = diff.lines.map do |line|
+ diff.lines.map { |line|
case line[0].chr
when "+"
- color(line, green)
+ green line
when "-"
- color(line, red)
+ red line
when "@"
- line[1].chr == "@" ? color(line, blue) : line
+ line[1].chr == "@" ? blue(line) : line
else
line
end
- end
- lines.join
+ }.join
end
def object_to_string(object)
View
2 lib/rspec/matchers/configuration.rb
@@ -43,7 +43,7 @@ def syntax
# fallback if rspec core not available
if defined?(RSpec::Core)
def color?
- RSpec.configuration.color
+ RSpec.configuration.color_enabled?
end
else
attr_writer :color