Class: GitCrecord::Diff::File

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

Constant Summary

Constants inherited from Difference

Difference::SELECTED_MAP, Difference::SELECTION_MARKER_WIDTH

Instance Attribute Summary collapse

Attributes inherited from Difference

#expanded, #subs, #y1, #y2

Instance Method Summary collapse

Methods inherited from Difference

#content_width, #prefix, #print, #selectable?, #selectable_subs, #selected, #selected=, #style

Constructor Details

#initialize(filename_a, filename_b, type: :modified) ⇒ File

Returns a new instance of File.



11
12
13
14
15
16
17
# File 'lib/git_crecord/diff/file.rb', line 11

def initialize(filename_a, filename_b, type: :modified)
  @filename_a = filename_a
  @filename_b = filename_b
  @type = type
  @expanded = false
  super()
end

Instance Attribute Details

#filename_aObject (readonly)

Returns the value of attribute filename_a.



8
9
10
# File 'lib/git_crecord/diff/file.rb', line 8

def filename_a
  @filename_a
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/git_crecord/diff/file.rb', line 9

def type
  @type
end

Instance Method Details

#<<(hunk) ⇒ Object



45
46
47
48
# File 'lib/git_crecord/diff/file.rb', line 45

def <<(hunk)
  subs << Hunk.new(hunk)
  self
end

#add_hunk_line(line) ⇒ Object



50
51
52
# File 'lib/git_crecord/diff/file.rb', line 50

def add_hunk_line(line)
  subs.last << line
end

#generate_diffObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/git_crecord/diff/file.rb', line 54

def generate_diff
  return unless selected
  [
    "diff --git a/#{@filename_a} b/#{@filename_b}",
    "--- a/#{@filename_a}",
    "+++ b/#{@filename_b}",
    *subs.map(&:generate_diff).compact,
    ''
  ].join("\n")
end

#info_stringObject



25
26
27
28
# File 'lib/git_crecord/diff/file.rb', line 25

def info_string
  line_count = subs.reduce(0){ |a, e| e.selectable_subs.size + a }
  "  #{subs.size} hunk(s), #{line_count} line(s) changed"
end

#max_height(width) ⇒ Object



37
38
39
# File 'lib/git_crecord/diff/file.rb', line 37

def max_height(width)
  super + ((info_string.size - 1).abs / content_width(width)) + 2
end

#strings(width) ⇒ Object



30
31
32
33
34
35
# File 'lib/git_crecord/diff/file.rb', line 30

def strings(width)
  result = super
  return result unless expanded
  result += info_string.scan(/.{1,#{content_width(width)}}/)
  result << ''
end

#to_sObject



19
20
21
22
23
# File 'lib/git_crecord/diff/file.rb', line 19

def to_s
  prefix = {modified: 'M', untracked: '?'}.fetch(type)
  return "#{prefix} #{@filename_a}" if @filename_a == @filename_b
  "#{prefix} #{filename_a} -> #{filename_b}"
end

#x_offsetObject



41
42
43
# File 'lib/git_crecord/diff/file.rb', line 41

def x_offset
  0
end