Skip to content

Commit 1b6417b

Browse files
brunsa2mcmire
authored andcommitted
Add configuration for custom colors
1 parent 8ef543c commit 1b6417b

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ require "super_diff/rspec"
155155

156156
## Configuration
157157

158+
### Custom colors
159+
160+
If you want to use something other than the default colors, you can
161+
configure them by adding them to your test helper file
162+
(`rails_helper` or `spec_helper`):
163+
164+
``` ruby
165+
SuperDiff.configure do |config|
166+
config.set_actual_color(:green)
167+
config.set_expected_color(:red)
168+
config.set_border_color(:yellow)
169+
config.set_header_color(:yellow)
170+
end
171+
```
172+
173+
See [eight_bit_color.rb](lib/super_diff/csi/eight_bit_color.rb) for the list
174+
of available colors.
175+
176+
### Diffing custom objects
177+
158178
As capable as this library is,
159179
it doesn't know how to deal with every kind of object out there.
160180
If you have a custom class,

lib/super_diff.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ module SuperDiff
2121
autoload :Operations, "super_diff/operations"
2222
autoload :RecursionGuard, "super_diff/recursion_guard"
2323

24-
COLORS = {
25-
alpha: :magenta,
26-
beta: :yellow,
27-
border: :blue,
28-
header: :white,
29-
}.freeze
30-
3124
def self.configure
3225
yield configuration
3326
end

lib/super_diff/colorized_document_extensions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def self.extended(extendee)
77
end
88

99
def alpha(*args, **opts, &block)
10-
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:alpha), &block)
10+
colorize(*args, **opts, fg: SuperDiff.configuration.alpha_color, &block)
1111
end
1212

1313
def beta(*args, **opts, &block)
14-
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:beta), &block)
14+
colorize(*args, **opts, fg: SuperDiff.configuration.beta_color, &block)
1515
end
1616
end
1717
end

lib/super_diff/configuration.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class Configuration
66
:extra_operation_tree_classes,
77
:extra_diff_formatter_classes,
88
:extra_inspector_classes,
9+
:alpha_color,
10+
:beta_color,
11+
:border_color,
12+
:header_color,
913
)
1014

1115
def initialize
@@ -14,6 +18,10 @@ def initialize
1418
@extra_operation_tree_classes = [].freeze
1519
@extra_diff_formatter_classes = [].freeze
1620
@extra_inspector_classes = [].freeze
21+
@alpha_color = :magenta
22+
@beta_color = :yellow
23+
@border_color = :blue
24+
@header_color = :white
1725
end
1826

1927
def add_extra_differ_classes(*classes)
@@ -56,5 +64,29 @@ def add_extra_inspector_classes(*classes)
5664
:add_extra_inspector_class,
5765
:add_extra_inspector_classes,
5866
)
67+
68+
def set_alpha_color(color)
69+
@alpha_color = color
70+
end
71+
alias_method(
72+
:set_expected_color,
73+
:set_alpha_color
74+
)
75+
76+
def set_beta_color(color)
77+
@beta_color = color
78+
end
79+
alias_method(
80+
:set_actual_color,
81+
:set_beta_color
82+
)
83+
84+
def set_border_color(color)
85+
@border_color = color
86+
end
87+
88+
def set_header_color(color)
89+
@header_color = color
90+
end
5991
end
6092
end

lib/super_diff/rspec/matcher_text_builders/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def add_extra_after_error
4646
end
4747

4848
def beta_color
49-
SuperDiff::COLORS.fetch(:beta)
49+
SuperDiff.configuration.beta_color
5050
end
5151

5252
def alpha_color
53-
SuperDiff::COLORS.fetch(:alpha)
53+
SuperDiff.configuration.alpha_color
5454
end
5555

5656
private

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,34 +272,34 @@ def self.from(expected)
272272
return expected if self === expected
273273

274274
text =
275-
colorizer.wrap("Diff:", SuperDiff::COLORS.fetch(:header)) +
275+
colorizer.wrap("Diff:", SuperDiff.configuration.header_color) +
276276
"\n\n" +
277277
colorizer.wrap(
278278
"┌ (Key) ──────────────────────────┐",
279-
SuperDiff::COLORS.fetch(:border)
279+
SuperDiff.configuration.border_color
280280
) +
281281
"\n" +
282-
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
282+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
283283
colorizer.wrap(
284284
"‹-› in expected, not in actual",
285-
SuperDiff::COLORS.fetch(:alpha)
285+
SuperDiff.configuration.alpha_color
286286
) +
287-
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
287+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
288288
"\n" +
289-
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
289+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
290290
colorizer.wrap(
291291
"‹+› in actual, not in expected",
292-
SuperDiff::COLORS.fetch(:beta)
292+
SuperDiff.configuration.beta_color
293293
) +
294-
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
294+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
295295
"\n" +
296-
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
296+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
297297
"‹ › in both expected and actual" +
298-
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
298+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
299299
"\n" +
300300
colorizer.wrap(
301301
"└─────────────────────────────────┘",
302-
SuperDiff::COLORS.fetch(:border)
302+
SuperDiff.configuration.border_color
303303
)
304304

305305
new([[expected, text]])

0 commit comments

Comments
 (0)