Class: TTY::File::Differ

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

Instance Method Summary collapse

Constructor Details

#initialize(format: :unified, context_lines: 3) ⇒ Differ

Create a Differ



12
13
14
15
# File 'lib/tty/file/differ.rb', line 12

def initialize(format: :unified, context_lines: 3)
  @format = format
  @context_lines = context_lines
end

Instance Method Details

#add_charObject

Diff add char



37
38
39
40
41
42
43
44
45
46
# File 'lib/tty/file/differ.rb', line 37

def add_char
  case @format
  when :old
    ">"
  when :unified
    "+"
  else
    "*"
  end
end

#call(string_a, string_b) ⇒ String

Find character difference between two strings

Returns:

  • (String)

    the difference between content or empty if no difference found



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

def call(string_a, string_b)
  string_a_lines = convert_to_lines(string_a)
  string_b_lines = convert_to_lines(string_b)
  diffs = Diff::LCS.diff(string_a_lines, string_b_lines)
  return "" if diffs.empty?

  hunks = extract_hunks(diffs, string_a_lines, string_b_lines)
  format_hunks(hunks)
end

#delete_charObject

Diff delete char



51
52
53
54
55
56
57
58
59
60
# File 'lib/tty/file/differ.rb', line 51

def delete_char
  case @format
  when :old
    "<"
  when :unified
    "-"
  else
    "*"
  end
end