Class: CuffSert::DiffWithContext

Inherits:
Object
  • Object
show all
Defined in:
lib/cuffsert/rendering.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx_size: 3, color: false) ⇒ DiffWithContext

Returns a new instance of DiffWithContext.



290
291
292
293
# File 'lib/cuffsert/rendering.rb', line 290

def initialize(ctx_size: 3, color: false)
  @ctx_size = ctx_size
  @color = color
end

Instance Method Details

#generate_for(left, right) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/cuffsert/rendering.rb', line 295

def generate_for(left, right)
  buf = StringIO.new
  lineno_size = left.size.to_s.size
  diff = Hashdiff.best_diff(left, right, array_path: true)
  with_context(diff, left) do |ch, lineno, line|
    if ch == '!'
      change = "...\n"
    elsif ch == '-'
      change = sprintf("%s %#{lineno_size}d %s\n", ch, lineno, line)
      change = change.colorize(:red) if @color
    elsif ch == '+'
      change = sprintf("%s #{' ' * lineno_size} %s\n", ch, line)
      change = change.colorize(:green) if @color
    else
      change = sprintf("  %#{lineno_size}d %s\n", lineno, line)
    end
    buf << change
  end
  buf.string
end