Class: GitCrecord::Diff::File

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

Constant Summary

Constants inherited from Difference

Difference::REVERSE_SELECTED_MAP, 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, reverse: false) ⇒ File

Returns a new instance of File.



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

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

Instance Attribute Details

#filename_aObject (readonly)

Returns the value of attribute filename_a.



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

def filename_a
  @filename_a
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/git_crecord/diff/file.rb', line 11

def type
  @type
end

Instance Method Details

#<<(hunk) ⇒ Object



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

def <<(hunk)
  subs << Hunk.new(hunk, reverse: @reverse)
  self
end

#add_hunk_line(line) ⇒ Object



54
55
56
# File 'lib/git_crecord/diff/file.rb', line 54

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

#generate_diffObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/git_crecord/diff/file.rb', line 58

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



28
29
30
31
# File 'lib/git_crecord/diff/file.rb', line 28

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



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

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

#strings(width) ⇒ Object



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

def strings(width)
  result = super
  return result unless expanded

  result += info_string.scan(/.{1,#{content_width(width)}}/)
  result << ''
end

#to_sObject



21
22
23
24
25
26
# File 'lib/git_crecord/diff/file.rb', line 21

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



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

def x_offset
  0
end