Class: GitCrecord::Diff::Hunk

Inherits:
Difference show all
Defined in:
lib/git_crecord/diff/hunk.rb

Constant Summary

Constants inherited from Difference

Difference::SELECTED_MAP, Difference::SELECTION_MARKER_WIDTH

Instance Attribute Summary

Attributes inherited from Difference

#expanded, #subs, #y1, #y2

Instance Method Summary collapse

Methods inherited from Difference

#content_width, #max_height, #prefix, #prefix_style, #print, #selectable?, #selectable_subs, #selected, #selected=, #strings, #style

Constructor Details

#initialize(head) ⇒ Hunk

Returns a new instance of Hunk.



8
9
10
11
12
# File 'lib/git_crecord/diff/hunk.rb', line 8

def initialize(head)
  @head = head
  @expanded = true
  super()
end

Instance Method Details

#<<(line) ⇒ Object



22
23
24
25
# File 'lib/git_crecord/diff/hunk.rb', line 22

def <<(line)
  subs << Line.new(line)
  self
end

#generate_diffObject



27
28
29
30
# File 'lib/git_crecord/diff/hunk.rb', line 27

def generate_diff
  return nil unless selected
  [generate_header, *subs.map(&:generate_diff).compact].join("\n")
end

#generate_headerObject



32
33
34
35
36
37
38
39
40
# File 'lib/git_crecord/diff/hunk.rb', line 32

def generate_header
  old_start, old_count, new_start, new_count = parse_header
  selectable_subs.each do |sub|
    next if sub.selected
    new_count -= 1 if sub.add?
    new_count += 1 if sub.del?
  end
  "@@ -#{old_start},#{old_count} +#{new_start},#{new_count} @@"
end

#parse_headerObject



42
43
44
45
46
# File 'lib/git_crecord/diff/hunk.rb', line 42

def parse_header
  match = @head.match(/@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@/)
  raise "mismatching hunk-header - '#{@head}'" if match.nil?
  [match[1], match[3] || 1, match[4], match[6] || 1].map(&:to_i)
end

#to_sObject



14
15
16
# File 'lib/git_crecord/diff/hunk.rb', line 14

def to_s
  @head
end

#x_offsetObject



18
19
20
# File 'lib/git_crecord/diff/hunk.rb', line 18

def x_offset
  3
end