Class: WCC::DiffItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ DiffItem

Returns a new instance of DiffItem.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wcc/diff.rb', line 8

def initialize(line)
  # parse line
  if line.start_with?('+++')
    @status = :new
    @text = line.substring(3)
  elsif line.start_with?('---')
    @status = :old
    @text = line.substring(3)
  elsif line.start_with?('@@')
    @status = :range
    @text = line.substring(2)
  elsif line.start_with?('+')
    @status = :ins
    @text = line.substring(1)
  elsif line.start_with?('-')
    @status = :del
    @text = line.substring(1)
  else
    @status = :other
    @text = line.rstrip
    @text = ' ' if @text.empty?
  end
  @text.gsub!(/\n/, '')
  @hilite = nil
end

Instance Attribute Details

#hiliteObject

Returns the value of attribute hilite.



6
7
8
# File 'lib/wcc/diff.rb', line 6

def hilite
  @hilite
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/wcc/diff.rb', line 5

def status
  @status
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/wcc/diff.rb', line 5

def text
  @text
end

Instance Method Details

#html_hilite_text(css_klass = 'hilite') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wcc/diff.rb', line 34

def html_hilite_text(css_klass = 'hilite')
  return @text if @hilite.nil?
  
  i = 0
  new_text = ''
  in_span = false
  @text.chars.to_a.each do |c|
    if @hilite.include?(i)
      if not in_span
        new_text += "<span class=\"#{css_klass}\">"
        in_span = true
      end
      new_text += (c == ' ' ? '&nbsp;' : c)
    else
      if in_span
        new_text += "</span>"
        in_span = false
      end
      new_text += c
    end
    i += 1
  end
  new_text += "</span>" if in_span
  new_text
end

#rcharString

Returns a representing character for the kind of this diff item.

Returns:

  • (String)

    single rep char



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wcc/diff.rb', line 62

def rchar
  case status
  when :new
    'N'
  when :old
    'O'
  when :range
    '@'
  when :ins
    'i'
  when :del
    'd'
  when :other
    '_'
  end
end

#to_sString

Returns an unified diff line without trailing newline.

Returns:

  • (String)

    unified diff line



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wcc/diff.rb', line 81

def to_s
  case status
  when :new
    '+++'+text
  when :old
    '---'+text
  when :range
    '@@'+text
  when :ins
    '+'+text
  when :del
    '-'+text
  when :other
    ' '+text
  end
end