super_diff 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +65 -26
  3. data/lib/super_diff/active_record/differs/active_record_relation.rb +1 -2
  4. data/lib/super_diff/active_record/differs.rb +1 -1
  5. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_model.rb +5 -13
  6. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_relation.rb +1 -3
  7. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders.rb +2 -2
  8. data/lib/super_diff/active_record/object_inspection.rb +1 -1
  9. data/lib/super_diff/active_record/operation_tree_builders/active_record_model.rb +1 -2
  10. data/lib/super_diff/active_record/operation_tree_builders/active_record_relation.rb +1 -2
  11. data/lib/super_diff/active_record/operation_tree_builders.rb +2 -2
  12. data/lib/super_diff/active_record/operation_tree_flatteners.rb +1 -1
  13. data/lib/super_diff/active_record/operation_trees.rb +1 -1
  14. data/lib/super_diff/active_record.rb +7 -15
  15. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +5 -6
  16. data/lib/super_diff/active_support/differs.rb +1 -1
  17. data/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb +3 -9
  18. data/lib/super_diff/active_support/object_inspection/inspection_tree_builders.rb +1 -1
  19. data/lib/super_diff/active_support/object_inspection.rb +1 -1
  20. data/lib/super_diff/active_support/operation_tree_builders/hash_with_indifferent_access.rb +5 -6
  21. data/lib/super_diff/active_support/operation_tree_builders.rb +1 -1
  22. data/lib/super_diff/active_support/operation_tree_flatteners.rb +1 -1
  23. data/lib/super_diff/active_support/operation_trees.rb +1 -1
  24. data/lib/super_diff/active_support.rb +6 -11
  25. data/lib/super_diff/colorized_document_extensions.rb +2 -4
  26. data/lib/super_diff/configuration.rb +14 -8
  27. data/lib/super_diff/csi/color.rb +4 -4
  28. data/lib/super_diff/csi/colorized_document.rb +12 -16
  29. data/lib/super_diff/csi/document.rb +8 -19
  30. data/lib/super_diff/csi/eight_bit_color.rb +19 -19
  31. data/lib/super_diff/csi/four_bit_color.rb +82 -31
  32. data/lib/super_diff/csi/twenty_four_bit_color.rb +12 -11
  33. data/lib/super_diff/csi/uncolorized_document.rb +2 -6
  34. data/lib/super_diff/csi.rb +10 -14
  35. data/lib/super_diff/diff_formatters/collection.rb +35 -41
  36. data/lib/super_diff/differs/default_object.rb +1 -1
  37. data/lib/super_diff/differs/defaults.rb +1 -1
  38. data/lib/super_diff/differs/main.rb +2 -13
  39. data/lib/super_diff/equality_matchers/array.rb +10 -12
  40. data/lib/super_diff/equality_matchers/base.rb +1 -1
  41. data/lib/super_diff/equality_matchers/default.rb +2 -4
  42. data/lib/super_diff/equality_matchers/defaults.rb +1 -1
  43. data/lib/super_diff/equality_matchers/hash.rb +10 -12
  44. data/lib/super_diff/equality_matchers/multiline_string.rb +11 -13
  45. data/lib/super_diff/equality_matchers/primitive.rb +10 -12
  46. data/lib/super_diff/equality_matchers/singleline_string.rb +10 -12
  47. data/lib/super_diff/equality_matchers.rb +1 -1
  48. data/lib/super_diff/errors.rb +3 -3
  49. data/lib/super_diff/gem_version.rb +3 -3
  50. data/lib/super_diff/helpers.rb +10 -10
  51. data/lib/super_diff/implementation_checks.rb +2 -2
  52. data/lib/super_diff/line.rb +3 -3
  53. data/lib/super_diff/object_inspection/inspection_tree.rb +46 -60
  54. data/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +2 -6
  55. data/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +3 -9
  56. data/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +5 -14
  57. data/lib/super_diff/object_inspection/inspection_tree_builders/defaults.rb +1 -1
  58. data/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +4 -12
  59. data/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb +6 -23
  60. data/lib/super_diff/object_inspection/inspection_tree_builders.rb +9 -9
  61. data/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +8 -20
  62. data/lib/super_diff/object_inspection/nodes/as_prefix_when_rendering_to_lines.rb +1 -5
  63. data/lib/super_diff/object_inspection/nodes/as_prelude_when_rendering_to_lines.rb +1 -5
  64. data/lib/super_diff/object_inspection/nodes/as_single_line.rb +3 -7
  65. data/lib/super_diff/object_inspection/nodes/base.rb +9 -14
  66. data/lib/super_diff/object_inspection/nodes/inspection.rb +26 -34
  67. data/lib/super_diff/object_inspection/nodes/nesting.rb +1 -1
  68. data/lib/super_diff/object_inspection/nodes/only_when.rb +2 -6
  69. data/lib/super_diff/object_inspection/nodes/text.rb +1 -5
  70. data/lib/super_diff/object_inspection/nodes/when_empty.rb +2 -6
  71. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +2 -6
  72. data/lib/super_diff/object_inspection/nodes/when_rendering_to_lines.rb +1 -1
  73. data/lib/super_diff/object_inspection/nodes/when_rendering_to_string.rb +1 -5
  74. data/lib/super_diff/object_inspection/nodes.rb +7 -7
  75. data/lib/super_diff/object_inspection.rb +3 -3
  76. data/lib/super_diff/operation_tree_builders/array.rb +12 -11
  77. data/lib/super_diff/operation_tree_builders/base.rb +17 -15
  78. data/lib/super_diff/operation_tree_builders/custom_object.rb +8 -6
  79. data/lib/super_diff/operation_tree_builders/default_object.rb +29 -21
  80. data/lib/super_diff/operation_tree_builders/hash.rb +23 -24
  81. data/lib/super_diff/operation_tree_builders/main.rb +4 -11
  82. data/lib/super_diff/operation_tree_builders/multiline_string.rb +3 -3
  83. data/lib/super_diff/operation_tree_builders/time_like.rb +1 -11
  84. data/lib/super_diff/operation_tree_builders.rb +1 -1
  85. data/lib/super_diff/operation_tree_flatteners/base.rb +1 -5
  86. data/lib/super_diff/operation_tree_flatteners/collection.rb +13 -16
  87. data/lib/super_diff/operation_tree_flatteners/default_object.rb +1 -2
  88. data/lib/super_diff/operation_tree_flatteners/hash.rb +2 -10
  89. data/lib/super_diff/operation_tree_flatteners/multiline_string.rb +2 -1
  90. data/lib/super_diff/operation_tree_flatteners.rb +3 -3
  91. data/lib/super_diff/operation_trees/base.rb +3 -5
  92. data/lib/super_diff/operation_trees/default_object.rb +1 -3
  93. data/lib/super_diff/operations/binary_operation.rb +2 -2
  94. data/lib/super_diff/operations/unary_operation.rb +1 -1
  95. data/lib/super_diff/rails.rb +2 -1
  96. data/lib/super_diff/recursion_guard.rb +5 -4
  97. data/lib/super_diff/rspec/augmented_matcher.rb +4 -5
  98. data/lib/super_diff/rspec/differ.rb +6 -13
  99. data/lib/super_diff/rspec/differs/collection_containing_exactly.rb +3 -2
  100. data/lib/super_diff/rspec/differs/collection_including.rb +1 -1
  101. data/lib/super_diff/rspec/differs/hash_including.rb +1 -1
  102. data/lib/super_diff/rspec/differs.rb +3 -3
  103. data/lib/super_diff/rspec/matcher_text_builders/base.rb +13 -15
  104. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +5 -7
  105. data/lib/super_diff/rspec/matcher_text_builders/have_predicate.rb +3 -5
  106. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +1 -5
  107. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +2 -6
  108. data/lib/super_diff/rspec/matcher_text_builders.rb +5 -8
  109. data/lib/super_diff/rspec/matcher_text_template.rb +13 -16
  110. data/lib/super_diff/rspec/monkey_patches.rb +122 -116
  111. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb +8 -6
  112. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb +11 -16
  113. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb +2 -1
  114. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb +8 -6
  115. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb +8 -6
  116. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders.rb +9 -9
  117. data/lib/super_diff/rspec/object_inspection.rb +1 -1
  118. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +11 -10
  119. data/lib/super_diff/rspec/operation_tree_builders/collection_including.rb +7 -6
  120. data/lib/super_diff/rspec/operation_tree_builders/hash_including.rb +11 -10
  121. data/lib/super_diff/rspec/operation_tree_builders/object_having_attributes.rb +22 -17
  122. data/lib/super_diff/rspec/operation_tree_builders.rb +4 -4
  123. data/lib/super_diff/rspec.rb +11 -13
  124. data/lib/super_diff/tiered_lines_elider.rb +87 -115
  125. data/lib/super_diff/tiered_lines_formatter.rb +5 -9
  126. data/lib/super_diff/version.rb +1 -1
  127. data/lib/super_diff.rb +4 -9
  128. data/spec/examples.txt +493 -485
  129. data/spec/integration/rails/active_record_spec.rb +3 -1
  130. data/spec/integration/rails/engines_spec.rb +20 -0
  131. data/spec/integration/rails/hash_with_indifferent_access_spec.rb +3 -1
  132. data/spec/integration/rspec/be_falsey_matcher_spec.rb +40 -35
  133. data/spec/integration/rspec/be_matcher_spec.rb +360 -320
  134. data/spec/integration/rspec/be_nil_matcher_spec.rb +38 -34
  135. data/spec/integration/rspec/be_predicate_matcher_spec.rb +376 -359
  136. data/spec/integration/rspec/be_truthy_matcher_spec.rb +38 -33
  137. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +242 -239
  138. data/spec/integration/rspec/eq_matcher_spec.rb +595 -557
  139. data/spec/integration/rspec/have_attributes_matcher_spec.rb +294 -286
  140. data/spec/integration/rspec/have_predicate_matcher_spec.rb +291 -297
  141. data/spec/integration/rspec/include_matcher_spec.rb +317 -215
  142. data/spec/integration/rspec/match_array_matcher_spec.rb +276 -273
  143. data/spec/integration/rspec/match_matcher_spec.rb +847 -834
  144. data/spec/integration/rspec/raise_error_matcher_spec.rb +468 -453
  145. data/spec/integration/rspec/respond_to_matcher_spec.rb +702 -697
  146. data/spec/integration/rspec/third_party_matcher_spec.rb +142 -138
  147. data/spec/integration/rspec/unhandled_errors_spec.rb +88 -84
  148. data/spec/spec_helper.rb +17 -20
  149. data/spec/support/command_runner.rb +34 -57
  150. data/spec/support/integration/helpers.rb +35 -28
  151. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +28 -23
  152. data/spec/support/integration/test_programs/base.rb +12 -17
  153. data/spec/support/integration/test_programs/plain.rb +6 -0
  154. data/spec/support/integration/test_programs/rspec_active_record.rb +4 -1
  155. data/spec/support/integration/test_programs/rspec_active_support.rb +4 -1
  156. data/spec/support/integration/test_programs/rspec_rails.rb +4 -1
  157. data/spec/support/integration/test_programs/rspec_rails_engine.rb +30 -0
  158. data/spec/support/models/active_record/person.rb +6 -4
  159. data/spec/support/models/active_record/shipping_address.rb +9 -10
  160. data/spec/support/models/customer.rb +2 -4
  161. data/spec/support/models/player.rb +3 -6
  162. data/spec/support/models/shipping_address.rb +4 -13
  163. data/spec/support/shared_examples/active_record.rb +232 -214
  164. data/spec/support/shared_examples/active_support.rb +53 -51
  165. data/spec/support/shared_examples/elided_diffs.rb +405 -381
  166. data/spec/support/shared_examples/hash_with_indifferent_access.rb +424 -388
  167. data/spec/support/shared_examples/key.rb +123 -0
  168. data/spec/support/unit/helpers.rb +1 -3
  169. data/spec/support/unit/matchers/match_output.rb +12 -13
  170. data/spec/unit/active_record/object_inspection_spec.rb +222 -206
  171. data/spec/unit/equality_matchers/main_spec.rb +1049 -984
  172. data/spec/unit/helpers_spec.rb +14 -21
  173. data/spec/unit/operation_tree_flatteners/array_spec.rb +557 -524
  174. data/spec/unit/operation_tree_flatteners/custom_object_spec.rb +619 -601
  175. data/spec/unit/operation_tree_flatteners/default_object_spec.rb +619 -621
  176. data/spec/unit/operation_tree_flatteners/hash_spec.rb +595 -556
  177. data/spec/unit/operation_tree_flatteners/multiline_string_spec.rb +102 -92
  178. data/spec/unit/rspec/matchers/be_compared_to_spec.rb +3 -3
  179. data/spec/unit/rspec/matchers/be_predicate_spec.rb +3 -3
  180. data/spec/unit/rspec/matchers/contain_exactly_spec.rb +1 -1
  181. data/spec/unit/rspec/matchers/have_attributes_spec.rb +1 -1
  182. data/spec/unit/rspec/matchers/have_predicate_spec.rb +2 -2
  183. data/spec/unit/rspec/matchers/include_spec.rb +2 -2
  184. data/spec/unit/rspec/matchers/match_array_spec.rb +2 -2
  185. data/spec/unit/rspec/matchers/raise_error_spec.rb +5 -5
  186. data/spec/unit/rspec/matchers/respond_to_spec.rb +23 -16
  187. data/spec/unit/rspec/object_inspection_spec.rb +349 -324
  188. data/spec/unit/super_diff_spec.rb +1542 -1449
  189. data/spec/unit/tiered_lines_elider_spec.rb +3508 -3536
  190. data/spec/unit/tiered_lines_formatter_spec.rb +54 -116
  191. data/super_diff.gemspec +13 -12
  192. metadata +73 -73
  193. data/spec/tmp/warnings_logger/all_warnings.txt +0 -2
@@ -5,16 +5,18 @@ module SuperDiff
5
5
  :extra_differ_classes,
6
6
  :extra_inspection_tree_builder_classes,
7
7
  :extra_operation_tree_builder_classes,
8
- :extra_operation_tree_classes,
8
+ :extra_operation_tree_classes
9
9
  )
10
10
  attr_accessor(
11
11
  :actual_color,
12
12
  :border_color,
13
+ :color_enabled,
13
14
  :diff_elision_enabled,
14
15
  :diff_elision_maximum,
15
16
  :elision_marker_color,
16
17
  :expected_color,
17
18
  :header_color,
19
+ :key_enabled
18
20
  )
19
21
 
20
22
  def initialize(options = {})
@@ -31,6 +33,7 @@ module SuperDiff
31
33
  @extra_operation_tree_builder_classes = [].freeze
32
34
  @extra_operation_tree_classes = [].freeze
33
35
  @header_color = :white
36
+ @key_enabled = true
34
37
 
35
38
  merge!(options)
36
39
  end
@@ -54,6 +57,10 @@ module SuperDiff
54
57
  @diff_elision_enabled
55
58
  end
56
59
 
60
+ def key_enabled?
61
+ @key_enabled
62
+ end
63
+
57
64
  def merge!(configuration_or_options)
58
65
  options =
59
66
  if configuration_or_options.is_a?(self.class)
@@ -62,9 +69,7 @@ module SuperDiff
62
69
  configuration_or_options
63
70
  end
64
71
 
65
- options.each do |key, value|
66
- instance_variable_set("@#{key}", value)
67
- end
72
+ options.each { |key, value| instance_variable_set("@#{key}", value) }
68
73
 
69
74
  updated
70
75
  end
@@ -79,7 +84,7 @@ module SuperDiff
79
84
  end
80
85
  alias_method(
81
86
  :add_extra_diff_formatter_class,
82
- :add_extra_diff_formatter_classes,
87
+ :add_extra_diff_formatter_classes
83
88
  )
84
89
 
85
90
  def add_extra_differ_classes(*classes)
@@ -93,7 +98,7 @@ module SuperDiff
93
98
  end
94
99
  alias_method(
95
100
  :add_extra_inspection_tree_builder_class,
96
- :add_extra_inspection_tree_builder_classes,
101
+ :add_extra_inspection_tree_builder_classes
97
102
  )
98
103
 
99
104
  def add_extra_operation_tree_builder_classes(*classes)
@@ -102,7 +107,7 @@ module SuperDiff
102
107
  end
103
108
  alias_method(
104
109
  :add_extra_operation_tree_builder_class,
105
- :add_extra_operation_tree_builder_classes,
110
+ :add_extra_operation_tree_builder_classes
106
111
  )
107
112
 
108
113
  def add_extra_operation_tree_classes(*classes)
@@ -111,7 +116,7 @@ module SuperDiff
111
116
  end
112
117
  alias_method(
113
118
  :add_extra_operation_tree_class,
114
- :add_extra_operation_tree_classes,
119
+ :add_extra_operation_tree_classes
115
120
  )
116
121
 
117
122
  def to_h
@@ -131,6 +136,7 @@ module SuperDiff
131
136
  extra_operation_tree_builder_classes.dup,
132
137
  extra_operation_tree_classes: extra_operation_tree_classes.dup,
133
138
  header_color: header_color,
139
+ key_enabled: key_enabled?
134
140
  }
135
141
  end
136
142
 
@@ -48,13 +48,13 @@ module SuperDiff
48
48
  attr_reader :layer
49
49
 
50
50
  def interpret_layer!(layer)
51
- if [:foreground, :background].include?(layer)
51
+ if %i[foreground background].include?(layer)
52
52
  layer
53
53
  else
54
54
  raise ArgumentError.new(
55
- "Invalid layer #{layer.inspect}. " +
56
- "Layer must be :foreground or :background.",
57
- )
55
+ "Invalid layer #{layer.inspect}. " +
56
+ "Layer must be :foreground or :background."
57
+ )
58
58
  end
59
59
  end
60
60
  end
@@ -11,9 +11,7 @@ module SuperDiff
11
11
  def colorize_block(colors, opts, &block)
12
12
  color_sequence = build_initial_color_sequence_from(colors, opts)
13
13
 
14
- if color_sequences_open_in_parent.any?
15
- add_part(Csi.reset_sequence)
16
- end
14
+ add_part(Csi.reset_sequence) if color_sequences_open_in_parent.any?
17
15
 
18
16
  add_part(color_sequence)
19
17
  color_sequences_open_in_parent << color_sequence
@@ -34,15 +32,11 @@ module SuperDiff
34
32
  contents.each do |content|
35
33
  if content.is_a?(self.class)
36
34
  content.each do |part|
37
- if part.is_a?(ColorSequenceBlock)
38
- add_part(Csi.reset_sequence)
39
- end
35
+ add_part(Csi.reset_sequence) if part.is_a?(ColorSequenceBlock)
40
36
 
41
37
  add_part(part)
42
38
 
43
- if part.is_a?(ResetSequence)
44
- add_part(color_sequence)
45
- end
39
+ add_part(color_sequence) if part.is_a?(ResetSequence)
46
40
  end
47
41
  else
48
42
  add_part(content)
@@ -57,15 +51,17 @@ module SuperDiff
57
51
  attr_reader :color_sequences_open_in_parent
58
52
 
59
53
  def build_initial_color_sequence_from(colors, opts)
60
- ColorSequenceBlock.new(colors).tap do |sequence|
61
- if opts[:fg]
62
- sequence << Color.resolve(opts[:fg], layer: :foreground)
63
- end
54
+ ColorSequenceBlock
55
+ .new(colors)
56
+ .tap do |sequence|
57
+ if opts[:fg]
58
+ sequence << Color.resolve(opts[:fg], layer: :foreground)
59
+ end
64
60
 
65
- if opts[:bg]
66
- sequence << Color.resolve(opts[:bg], layer: :background)
61
+ if opts[:bg]
62
+ sequence << Color.resolve(opts[:bg], layer: :background)
63
+ end
67
64
  end
68
- end
69
65
  end
70
66
  end
71
67
  end
@@ -7,9 +7,7 @@ module SuperDiff
7
7
  @parts = []
8
8
  @indentation_stack = []
9
9
 
10
- if block
11
- evaluate_block(&block)
12
- end
10
+ evaluate_block(&block) if block
13
11
  end
14
12
 
15
13
  def each(&block)
@@ -21,9 +19,8 @@ module SuperDiff
21
19
  end
22
20
 
23
21
  def colorize(*args, **opts, &block)
24
- contents, colors = args.partition do |arg|
25
- arg.is_a?(String) || arg.is_a?(self.class)
26
- end
22
+ contents, colors =
23
+ args.partition { |arg| arg.is_a?(String) || arg.is_a?(self.class) }
27
24
 
28
25
  if colors[0].is_a?(Symbol)
29
26
  if colors[0] == :colorize
@@ -47,13 +44,9 @@ module SuperDiff
47
44
  if block
48
45
  evaluate_block(&block)
49
46
  elsif contents.any?
50
- contents.each do |part|
51
- add_part(part)
52
- end
47
+ contents.each { |part| add_part(part) }
53
48
  else
54
- raise ArgumentError.new(
55
- "Must have something to add to the document!",
56
- )
49
+ raise ArgumentError.new("Must have something to add to the document!")
57
50
  end
58
51
  end
59
52
  alias_method :plain, :text
@@ -68,8 +61,8 @@ module SuperDiff
68
61
  text(*contents)
69
62
  else
70
63
  raise ArgumentError.new(
71
- "Must have something to add to the document!",
72
- )
64
+ "Must have something to add to the document!"
65
+ )
73
66
  end
74
67
  end
75
68
 
@@ -155,11 +148,7 @@ module SuperDiff
155
148
  end
156
149
 
157
150
  def wrapper
158
- if for_line?
159
- :line
160
- else
161
- :text
162
- end
151
+ for_line? ? :line : :text
163
152
  end
164
153
  end
165
154
 
@@ -19,18 +19,18 @@ module SuperDiff
19
19
  bright_blue: 12,
20
20
  bright_magenta: 13,
21
21
  bright_cyan: 14,
22
- bright_white: 15,
22
+ bright_white: 15
23
23
  }.freeze
24
24
  STARTING_INDICES = {
25
25
  standard: 0,
26
26
  high_intensity: 8,
27
- grayscale: 232,
27
+ grayscale: 232
28
28
  }.freeze
29
29
  VALID_PAIR_TYPES = STARTING_INDICES.keys
30
30
  VALID_PAIR_INDEX_RANGES = {
31
31
  standard: 0..7,
32
32
  high_intensity: 0..7,
33
- grayscale: 0..23,
33
+ grayscale: 0..23
34
34
  }.freeze
35
35
  LEADING_CODES_BY_LAYER = { foreground: 38, background: 48 }.freeze
36
36
  LAYERS_BY_LEADING_CODE = LEADING_CODES_BY_LAYER.invert.freeze
@@ -93,10 +93,10 @@ module SuperDiff
93
93
  def interpret_triplet!(red:, green:, blue:)
94
94
  if invalid_triplet?(red, green, blue)
95
95
  raise ArgumentError.new(
96
- "(#{red},#{green},#{blue}) is not a valid color " +
97
- "specification. All components must be between " +
98
- "#{VALID_COMPONENT_RANGE.begin} and #{VALID_COMPONENT_RANGE.end}.",
99
- )
96
+ "(#{red},#{green},#{blue}) is not a valid color " +
97
+ "specification. All components must be between " +
98
+ "#{VALID_COMPONENT_RANGE.begin} and #{VALID_COMPONENT_RANGE.end}."
99
+ )
100
100
  end
101
101
 
102
102
  16 + 36 * red + 6 * green + blue
@@ -110,7 +110,7 @@ module SuperDiff
110
110
  if !VALID_CODES_BY_NAME.include?(name)
111
111
  message =
112
112
  "#{name.inspect} is not a valid color name.\n" +
113
- "Valid names are:\n"
113
+ "Valid names are:\n"
114
114
 
115
115
  VALID_CODES_BY_NAME.keys.each do |valid_name|
116
116
  message << "- #{valid_name}\n"
@@ -125,19 +125,19 @@ module SuperDiff
125
125
  def interpret_pair!(type:, index:)
126
126
  if !VALID_PAIR_TYPES.include?(type)
127
127
  raise ArgumentError.new(
128
- "Given pair did not have a valid type. " +
129
- "Type must be one of: #{VALID_PAIR_TYPES}",
130
- )
128
+ "Given pair did not have a valid type. " +
129
+ "Type must be one of: #{VALID_PAIR_TYPES}"
130
+ )
131
131
  end
132
132
 
133
133
  valid_range = VALID_PAIR_INDEX_RANGES[type]
134
134
 
135
135
  if !valid_range.cover?(index)
136
136
  raise ArgumentError.new(
137
- "Given pair did not have a valid index. " +
138
- "For #{type}, index must be between #{valid_range.begin} and " +
139
- "#{valid_range.end}.",
140
- )
137
+ "Given pair did not have a valid index. " +
138
+ "For #{type}, index must be between #{valid_range.begin} and " +
139
+ "#{valid_range.end}."
140
+ )
141
141
  end
142
142
 
143
143
  STARTING_INDICES[type] + index
@@ -149,7 +149,7 @@ module SuperDiff
149
149
  if match
150
150
  {
151
151
  layer: interpret_layer!(match[1]),
152
- code: interpret_code!(match[2].to_i),
152
+ code: interpret_code!(match[2].to_i)
153
153
  }
154
154
  end
155
155
  end
@@ -165,9 +165,9 @@ module SuperDiff
165
165
  def interpret_code!(code)
166
166
  if !VALID_CODE_RANGE.cover?(code)
167
167
  raise ArgumentError.new(
168
- "#{code.inspect} is not a valid color code " +
169
- "(must be between 0 and 255).",
170
- )
168
+ "#{code.inspect} is not a valid color code " +
169
+ "(must be between 0 and 255)."
170
+ )
171
171
  end
172
172
 
173
173
  code
@@ -1,31 +1,86 @@
1
1
  module SuperDiff
2
2
  module Csi
3
3
  class FourBitColor < Color
4
- VALID_TYPES = [:foreground, :background].freeze
4
+ VALID_TYPES = %i[foreground background].freeze
5
5
  VALID_CODES_BY_NAME = {
6
- black: { foreground: 30, background: 40 },
7
- red: { foreground: 31, background: 41 },
8
- green: { foreground: 32, background: 42 },
9
- yellow: { foreground: 33, background: 43 },
10
- blue: { foreground: 34, background: 44 },
11
- magenta: { foreground: 35, background: 45 },
12
- cyan: { foreground: 36, background: 46 },
13
- white: { foreground: 37, background: 47 },
14
- bright_black: { foreground: 90, background: 100 },
15
- bright_red: { foreground: 91, background: 101 },
16
- bright_green: { foreground: 92, background: 102 },
17
- bright_yellow: { foreground: 93, background: 103 },
18
- bright_blue: { foreground: 94, background: 104 },
19
- bright_magenta: { foreground: 95, background: 105 },
20
- bright_cyan: { foreground: 96, background: 106 },
21
- bright_white: { foreground: 97, background: 107 },
6
+ black: {
7
+ foreground: 30,
8
+ background: 40
9
+ },
10
+ red: {
11
+ foreground: 31,
12
+ background: 41
13
+ },
14
+ green: {
15
+ foreground: 32,
16
+ background: 42
17
+ },
18
+ yellow: {
19
+ foreground: 33,
20
+ background: 43
21
+ },
22
+ blue: {
23
+ foreground: 34,
24
+ background: 44
25
+ },
26
+ magenta: {
27
+ foreground: 35,
28
+ background: 45
29
+ },
30
+ cyan: {
31
+ foreground: 36,
32
+ background: 46
33
+ },
34
+ white: {
35
+ foreground: 37,
36
+ background: 47
37
+ },
38
+ bright_black: {
39
+ foreground: 90,
40
+ background: 100
41
+ },
42
+ bright_red: {
43
+ foreground: 91,
44
+ background: 101
45
+ },
46
+ bright_green: {
47
+ foreground: 92,
48
+ background: 102
49
+ },
50
+ bright_yellow: {
51
+ foreground: 93,
52
+ background: 103
53
+ },
54
+ bright_blue: {
55
+ foreground: 94,
56
+ background: 104
57
+ },
58
+ bright_magenta: {
59
+ foreground: 95,
60
+ background: 105
61
+ },
62
+ bright_cyan: {
63
+ foreground: 96,
64
+ background: 106
65
+ },
66
+ bright_white: {
67
+ foreground: 97,
68
+ background: 107
69
+ }
22
70
  }.freeze
23
- COLORS_BY_CODE = VALID_CODES_BY_NAME.reduce({}) do |hash, (name, value)|
24
- hash.merge(
25
- value[:foreground] => { name: name, layer: :foreground },
26
- value[:background] => { name: name, layer: :background },
27
- )
28
- end
71
+ COLORS_BY_CODE =
72
+ VALID_CODES_BY_NAME.reduce({}) do |hash, (name, value)|
73
+ hash.merge(
74
+ value[:foreground] => {
75
+ name: name,
76
+ layer: :foreground
77
+ },
78
+ value[:background] => {
79
+ name: name,
80
+ layer: :background
81
+ }
82
+ )
83
+ end
29
84
  VALID_NAMES = VALID_CODES_BY_NAME.keys
30
85
  VALID_CODE_RANGES = [30..37, 40..47, 90..97, 100..107].freeze
31
86
  OPENING_REGEX = /\e\[(\d+)m/.freeze
@@ -72,11 +127,9 @@ module SuperDiff
72
127
  if !VALID_NAMES.include?(name)
73
128
  message =
74
129
  "#{name.inspect} is not a valid color name.\n" +
75
- "Valid names are:\n"
130
+ "Valid names are:\n"
76
131
 
77
- VALID_NAMES.each do |valid_name|
78
- message << "- #{valid_name}"
79
- end
132
+ VALID_NAMES.each { |valid_name| message << "- #{valid_name}" }
80
133
 
81
134
  raise ArgumentError.new(message)
82
135
  end
@@ -87,16 +140,14 @@ module SuperDiff
87
140
  def interpret_sequence!(sequence)
88
141
  match = sequence.match(OPENING_REGEX)
89
142
 
90
- if match
91
- interpret_code!(match[1].to_i)
92
- end
143
+ interpret_code!(match[1].to_i) if match
93
144
  end
94
145
 
95
146
  def interpret_code!(code)
96
147
  if VALID_CODE_RANGES.none? { |range| range.cover?(code) }
97
148
  message =
98
149
  "#{code.inspect} is not a valid color code.\n" +
99
- "Valid codes are:\n"
150
+ "Valid codes are:\n"
100
151
 
101
152
  VALID_CODE_RANGES.each do |range|
102
153
  message << "- #{range.begin} through #{range.end}\n"
@@ -28,7 +28,7 @@ module SuperDiff
28
28
  SERIAL_CODE,
29
29
  triplet.red,
30
30
  triplet.blue,
31
- triplet.green,
31
+ triplet.green
32
32
  ].join(";") + "m"
33
33
  end
34
34
 
@@ -37,7 +37,7 @@ module SuperDiff
37
37
  red: triplet.red,
38
38
  green: triplet.green,
39
39
  blue: triplet.blue,
40
- layer: :foreground,
40
+ layer: :foreground
41
41
  )
42
42
  end
43
43
 
@@ -55,11 +55,12 @@ module SuperDiff
55
55
  if match
56
56
  {
57
57
  layer: interpret_layer!(match[1]),
58
- triplet: interpret_triplet!(
59
- red: match[2].to_i,
60
- green: match[3].to_i,
61
- blue: match[4].to_i,
62
- ),
58
+ triplet:
59
+ interpret_triplet!(
60
+ red: match[2].to_i,
61
+ green: match[3].to_i,
62
+ blue: match[4].to_i
63
+ )
63
64
  }
64
65
  end
65
66
  end
@@ -67,10 +68,10 @@ module SuperDiff
67
68
  def interpret_triplet!(red:, green:, blue:)
68
69
  if invalid_triplet?(red, green, blue)
69
70
  raise ArgumentError.new(
70
- "(#{red},#{green},#{blue}) is not a valid color " +
71
- "specification. All components must be between " +
72
- "#{VALID_COMPONENT_RANGE.begin} and #{VALID_COMPONENT_RANGE.end}.",
73
- )
71
+ "(#{red},#{green},#{blue}) is not a valid color " +
72
+ "specification. All components must be between " +
73
+ "#{VALID_COMPONENT_RANGE.begin} and #{VALID_COMPONENT_RANGE.end}."
74
+ )
74
75
  end
75
76
 
76
77
  Triplet.new(red: red, green: green, blue: blue)
@@ -10,9 +10,7 @@ module SuperDiff
10
10
  def colorize_inline(contents, *)
11
11
  contents.each do |content|
12
12
  if content.is_a?(self.class)
13
- content.each do |part|
14
- add_part(part)
15
- end
13
+ content.each { |part| add_part(part) }
16
14
  else
17
15
  add_part(content)
18
16
  end
@@ -20,9 +18,7 @@ module SuperDiff
20
18
  end
21
19
 
22
20
  def add_part(part)
23
- if !part.is_a?(ResetSequence) && !part.is_a?(ColorSequenceBlock)
24
- super
25
- end
21
+ super if !part.is_a?(ResetSequence) && !part.is_a?(ColorSequenceBlock)
26
22
  end
27
23
  end
28
24
  end
@@ -41,21 +41,17 @@ module SuperDiff
41
41
  end
42
42
 
43
43
  def self.inspect_colors_in(text)
44
- [FourBitColor, EightBitColor, TwentyFourBitColor].
45
- reduce(text) do |str, klass|
46
- klass.sub_colorized_areas_in(str) do |area, color|
47
- color_block = colorize("◼︎", color.to_foreground)
48
-
49
- layer_indicator =
50
- if color.foreground?
51
- "(fg)"
52
- else
53
- "(bg)"
54
- end
55
-
56
- "#{color_block} #{layer_indicator} ❮#{area}❯"
57
- end
44
+ [FourBitColor, EightBitColor, TwentyFourBitColor].reduce(
45
+ text
46
+ ) do |str, klass|
47
+ klass.sub_colorized_areas_in(str) do |area, color|
48
+ color_block = colorize("◼︎", color.to_foreground)
49
+
50
+ layer_indicator = (color.foreground? ? "(fg)" : "(bg)")
51
+
52
+ "#{color_block} #{layer_indicator} ❮#{area}❯"
58
53
  end
54
+ end
59
55
  end
60
56
 
61
57
  self.color_enabled = false