Sha256: a81f8116d65529485148b1a55446bf5a50e782ceefb3a32e75bf3e4f0550fbb6

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require_relative 'difference'
require_relative 'hunk'
require_relative '../ui/color'

module GitCrecord
  module Diff
    class File < Difference
      attr_reader :filename_a
      attr_reader :type

      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

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

        "#{prefix} #{filename_a} -> #{filename_b}"
      end

      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

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

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

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

      def x_offset
        0
      end

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

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

      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

      alias prefix_style style
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git-crecord-1.1.1 lib/git_crecord/diff/file.rb
git-crecord-1.1.0 lib/git_crecord/diff/file.rb