Sha256: 3e9a65770beec83d6535c183d024d50581edbb304b12c76c2537c9404d50e3dd

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

class DiffWidget
  def initialize(diff_text)
    diff = Machinery.scrub(diff_text)
    @lines = diff.lines[2..-1]
    @widget = {
      file: diff[/--- a(.*)/, 1],
      additions: @lines.select { |l| l.start_with?("+") }.length,
      deletions: @lines.select { |l| l.start_with?("-") }.length
    }

    @original_line_number = 0
    @new_line_number = 0
  end

  def widget
    @widget[:lines] = @lines.map do |line|
      line = ERB::Util.html_escape(line.chomp).gsub("\\", "\").gsub("\t", " " * 8)
      case line
      when /^@.*/
        entry = header_entry(line)
        @original_line_number = line[/-(\d+)/, 1].to_i
        @new_line_number = line[/\+(\d+)/, 1].to_i
      when /^ .*/, ""
        entry = common_entry(line)
        @new_line_number += 1
        @original_line_number += 1
      when /^\+.*/
        entry = addition_entry(line)
        @new_line_number += 1
      when /^\-.*/
        entry = deletion_entry(line)
        @original_line_number += 1
      end

      entry
    end
    @widget
  end

  private

  def header_entry(line)
    {
      type: "header",
      content: line
    }
  end

  def common_entry(line)
    {
      type: "common",
      new_line_number: @new_line_number,
      original_line_number: @original_line_number,
      content: line[1..-1]
    }
  end

  def addition_entry(line)
    {
      type: "addition",
      new_line_number: @new_line_number,
      content: line[1..-1]
    }
  end

  def deletion_entry(line)
    {
      type: "deletion",
      original_line_number: @original_line_number,
      content: line[1..-1]
    }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
machinery-tool-1.22.1 lib/diff_widget.rb
machinery-tool-1.22.0 lib/diff_widget.rb