Class: Machinery::Ui::DiffWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/diff_widget.rb

Instance Method Summary collapse

Constructor Details

#initialize(diff_text) ⇒ DiffWidget

Returns a new instance of DiffWidget.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/diff_widget.rb', line 4

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

Instance Method Details

#widgetObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/diff_widget.rb', line 17

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