Class: Megatest::PatienceDiff::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/megatest/patience_diff.rb

Overview

Formats a plaintext unified diff.

Instance Method Summary collapse

Constructor Details

#initialize(differ, color) ⇒ Formatter

Returns a new instance of Formatter.



269
270
271
272
# File 'lib/megatest/patience_diff.rb', line 269

def initialize(differ, color)
  @differ = differ
  @color = color
end

Instance Method Details

#render_hunk(a, b, opcodes) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/megatest/patience_diff.rb', line 283

def render_hunk(a, b, opcodes)
  opcodes.flat_map do |(code, a_start, a_end, b_start, b_end)|
    case code
    when :equal
      b[b_start..b_end].map { |line| @color.grey(" #{line}") }
    when :delete
      a[a_start..a_end].map { |line| @color.red("-#{line}") }
    when :insert
      b[b_start..b_end].map { |line| @color.green("+#{line}") }
    end
  end
end

#render_hunk_marker(opcodes) ⇒ Object



274
275
276
277
278
279
280
281
# File 'lib/megatest/patience_diff.rb', line 274

def render_hunk_marker(opcodes)
  a_start = opcodes.first[1] + 1
  a_end = opcodes.last[2] + 2
  b_start = opcodes.first[3] + 1
  b_end = opcodes.last[4] + 2

  @color.magenta(format("@@ -%d,%d +%d,%d @@", a_start, a_end - a_start, b_start, b_end - b_start))
end