Class: TTY::File::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/file/differ.rb

Instance Method Summary collapse

Constructor Details

#initialize(string_a, string_b, options = {}) ⇒ Differ

Create a Differ



13
14
15
16
17
18
# File 'lib/tty/file/differ.rb', line 13

def initialize(string_a, string_b, options = {})
  @string_a      = string_a
  @string_b      = string_b
  @format        = options.fetch(:format, :unified)
  @context_lines = options.fetch(:context_lines, 3)
end

Instance Method Details

#callString

Find character difference between two strings

Returns:

  • (String)

    the difference between content or empty if no difference found



27
28
29
30
31
32
# File 'lib/tty/file/differ.rb', line 27

def call
  diffs  = Diff::LCS.diff(string_a_lines, string_b_lines)
  return '' if diffs.empty?
  hunks  = extract_hunks(diffs)
  format_hunks(hunks)
end