Better diff coloring #169

Merged
merged 3 commits into from Aug 25, 2012

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).

@myronmarston myronmarston commented on an outdated diff Aug 25, 2012
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
RSpec member

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 rspec:master Aug 25, 2012

1 check passed

Details default The Travis build passed
@myronmarston
RSpec member

Thanks, I just merged it.

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