class GitCrecord::Diff::File

Attributes

filename_a[R]
type[R]

Public Class Methods

new(filename_a, filename_b, type: :modified, reverse: false) click to toggle source
Calls superclass method GitCrecord::Diff::Difference::new
# 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

Public Instance Methods

<<(hunk) click to toggle source
# File lib/git_crecord/diff/file.rb, line 49
def <<(hunk)
  subs << Hunk.new(hunk, reverse: @reverse)
  self
end
add_hunk_line(line) click to toggle source
# File lib/git_crecord/diff/file.rb, line 54
def add_hunk_line(line)
  subs.last << line
end
empty?() click to toggle source
# File lib/git_crecord/diff/file.rb, line 76
def empty?
  selectable_subs.empty?
end
generate_diff() click to toggle source
# 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_string() click to toggle source
# 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
make_empty(type = 'empty') click to toggle source
# File lib/git_crecord/diff/file.rb, line 72
def make_empty(type = 'empty')
  subs << PseudoLine.new(type)
end
max_height(width) click to toggle source
Calls superclass method GitCrecord::Diff::Difference#max_height
# File lib/git_crecord/diff/file.rb, line 41
def max_height(width)
  super + ((info_string.size - 1).abs / content_width(width)) + 2
end
stage_steps() click to toggle source
# File lib/git_crecord/diff/file.rb, line 80
def stage_steps
  case type
  when :modified then %i[stage]
  when :new then empty? ? %i[add_file_full] : %i[stage]
  when :untracked then empty? ? %i[add_file_full] : %i[add_file stage]
  else raise "unknown file type - #{type.inspect}"
  end
end
strings(width) click to toggle source
Calls superclass method GitCrecord::Diff::Difference#strings
# 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_s() click to toggle source
# File lib/git_crecord/diff/file.rb, line 21
def to_s
  prefix = { modified: 'M', new: 'A', untracked: '?' }.fetch(type)
  return "#{prefix} #{@filename_a}" if @filename_a == @filename_b

  "#{prefix} #{filename_a} -> #{filename_b}"
end
unstage_steps() click to toggle source
# File lib/git_crecord/diff/file.rb, line 89
def unstage_steps
  case type
  when :modified then %i[unstage]
  when :new then %i[unstage]
  else raise "unknown file type - #{type.inspect}"
  end
end
x_offset() click to toggle source
# File lib/git_crecord/diff/file.rb, line 45
def x_offset
  0
end