Skip to content

Commit 7adedf3

Browse files
committed
Option to disable the key
1 parent a8ed2b1 commit 7adedf3

File tree

6 files changed

+290
-56
lines changed

6 files changed

+290
-56
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,41 @@ end
184184
See [eight_bit_color.rb](lib/super_diff/csi/eight_bit_color.rb)
185185
for the list of available colors.
186186

187+
### Enable or disable the key
188+
189+
You can disable the key by changing the following config (default: true)
190+
191+
``` ruby
192+
SuperDiff.configure do |config|
193+
config.key_enabled = false
194+
end
195+
```
196+
197+
### Elision
198+
199+
When looking at a large diff for which many of the lines do not change,
200+
it can be difficult to locate the lines which do. Text-oriented
201+
diffs such as those you get from a conventional version control system
202+
solve this problem by removing those unchanged lines from the diff
203+
entirely. The same can be done in SuperDiff.
204+
205+
``` ruby
206+
SuperDiff.configure do |config|
207+
config.diff_elision_enabled = false
208+
config.diff_elision_maximum = 3
209+
end
210+
```
211+
212+
* `diff_elision_enabled` — The elision logic is disabled by default so
213+
as not to surprise people, so setting this to `true` will turn it on.
214+
* `diff_elision_maximum` — This number controls what happens to
215+
unchanged lines (i.e. lines that are neither "insert" lines nor
216+
"delete" lines) that are in between changed lines. If a section of
217+
unchanged lines is beyond this number, the gem will elide (a fancy
218+
word for remove) the data structures within that section as much as
219+
possible until the limit is reached or it cannot go further. Elided
220+
lines are replaced with a `# ...` marker.
221+
187222
### Diffing custom objects
188223

189224
If you are comparing two data structures

lib/super_diff/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Configuration
1515
:elision_marker_color,
1616
:expected_color,
1717
:header_color,
18+
:key_enabled,
1819
)
1920

2021
def initialize(options = {})
@@ -31,6 +32,7 @@ def initialize(options = {})
3132
@extra_operation_tree_builder_classes = [].freeze
3233
@extra_operation_tree_classes = [].freeze
3334
@header_color = :white
35+
@key_enabled = true
3436

3537
merge!(options)
3638
end
@@ -54,6 +56,10 @@ def diff_elision_enabled?
5456
@diff_elision_enabled
5557
end
5658

59+
def key_enabled?
60+
@key_enabled
61+
end
62+
5763
def merge!(configuration_or_options)
5864
options =
5965
if configuration_or_options.is_a?(self.class)
@@ -131,6 +137,7 @@ def to_h
131137
extra_operation_tree_builder_classes.dup,
132138
extra_operation_tree_classes: extra_operation_tree_classes.dup,
133139
header_color: header_color,
140+
key_enabled: key_enabled?,
134141
}
135142
end
136143

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -286,36 +286,38 @@ class ExpectedsForMultipleDiffs
286286
def from(expected)
287287
return expected if self === expected
288288

289-
text =
290-
colorizer.wrap("Diff:", SuperDiff.configuration.header_color) +
291-
"\n\n" +
292-
colorizer.wrap(
293-
"┌ (Key) ──────────────────────────┐",
294-
SuperDiff.configuration.border_color
295-
) +
296-
"\n" +
297-
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
298-
colorizer.wrap(
299-
"‹-› in expected, not in actual",
300-
SuperDiff.configuration.expected_color
301-
) +
302-
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
303-
"\n" +
304-
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
305-
colorizer.wrap(
306-
"‹+› in actual, not in expected",
307-
SuperDiff.configuration.actual_color
308-
) +
309-
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
310-
"\n" +
311-
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
312-
"‹ › in both expected and actual" +
313-
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
314-
"\n" +
315-
colorizer.wrap(
316-
"└─────────────────────────────────┘",
317-
SuperDiff.configuration.border_color
318-
)
289+
text = colorizer.wrap("Diff:", SuperDiff.configuration.header_color)
290+
291+
if SuperDiff.configuration.key_enabled?
292+
text += "\n\n" +
293+
colorizer.wrap(
294+
"┌ (Key) ──────────────────────────┐",
295+
SuperDiff.configuration.border_color
296+
) +
297+
"\n" +
298+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
299+
colorizer.wrap(
300+
"‹-› in expected, not in actual",
301+
SuperDiff.configuration.expected_color
302+
) +
303+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
304+
"\n" +
305+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
306+
colorizer.wrap(
307+
"‹+› in actual, not in expected",
308+
SuperDiff.configuration.actual_color
309+
) +
310+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
311+
"\n" +
312+
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
313+
"‹ › in both expected and actual" +
314+
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
315+
"\n" +
316+
colorizer.wrap(
317+
"└─────────────────────────────────┘",
318+
SuperDiff.configuration.border_color
319+
)
320+
end
319321

320322
new([[expected, text]])
321323
end

spec/integration/rspec/eq_matcher_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,4 +953,8 @@
953953
it_behaves_like "a matcher that supports elided diffs" do
954954
let(:matcher) { :eq }
955955
end
956+
957+
it_behaves_like "a toggleable key" do
958+
let(:matcher) { :eq }
959+
end
956960
end

spec/support/integration/helpers.rb

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def build_expected_output(
3838
color_enabled:,
3939
snippet:,
4040
expectation:,
41+
key_enabled: true,
4142
newline_before_expectation: false,
4243
indentation: 7,
4344
diff: nil
@@ -64,32 +65,34 @@ def build_expected_output(
6465

6566
white_line "Diff:"
6667

67-
newline
68-
69-
line do
70-
blue "┌ (Key) ──────────────────────────┐"
71-
end
72-
73-
line do
74-
blue "│ "
75-
magenta "‹-› in expected, not in actual"
76-
blue " │"
77-
end
78-
79-
line do
80-
blue "│ "
81-
yellow "‹+› in actual, not in expected"
82-
blue " │"
83-
end
84-
85-
line do
86-
blue "│ "
87-
text "‹ › in both expected and actual"
88-
blue " │"
89-
end
90-
91-
line do
92-
blue "└─────────────────────────────────┘"
68+
if key_enabled
69+
newline
70+
71+
line do
72+
blue "┌ (Key) ──────────────────────────┐"
73+
end
74+
75+
line do
76+
blue "│ "
77+
magenta "‹-› in expected, not in actual"
78+
blue " │"
79+
end
80+
81+
line do
82+
blue "│ "
83+
yellow "‹+› in actual, not in expected"
84+
blue " │"
85+
end
86+
87+
line do
88+
blue "│ "
89+
text "‹ › in both expected and actual"
90+
blue " │"
91+
end
92+
93+
line do
94+
blue "└─────────────────────────────────┘"
95+
end
9396
end
9497

9598
newline

0 commit comments

Comments
 (0)