Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Better diff coloring #169

Merged
merged 3 commits into from

3 participants

@alexcoplan

Change configuration in RSpec::Matchers to use the same color config method as RSpec::Core (if core is available). Also made the diff coloring code more readable and succinct.

@travisbot

This pull request passes (merged 248004e into d427bac).

lib/rspec/expectations/differ.rb
@@ -63,30 +63,36 @@ 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
+
+ # define methods for each color name
+ {
+ :red => 31,
+ :green => 32,
+ :blue => 34
+ }.each do |color_name, color_code|
+ define_method color_name do |text|
+ color(text, color_code)
+ end
end
@myronmarston Owner

Honestly, I find this to be less readable and succinct than what you had before. I don't have a preference for color(text, red) over red(text) as an API (both seem equally fine to me), but using a hash and define_method adds extra indirection over what you had before. If you want the separate methods for each color, I'd prefer:

def red
  color(text, 31)
end

def green
  color(text, 32)
end

def blue
  color(text, 34)
end

I know on 1.8 that methods defined with define_method are slower when called than methods defined with def. Not sure about 1.9 (I haven't benchmarked it), but I expect it's still slower. So that's another reason to prefer def over define_method for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged 7cdd88b into d427bac).

@alexcoplan

@myronmarston OK, have changed it to multiple method defs.

@myronmarston myronmarston merged commit f4d4cd0 into from
@myronmarston

Thanks, I just merged it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.