Class: Kronk::Diff::Output::Section

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

Overview

Represents one diff section to render (starts with @@-line,len +line,len@@)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, line_num_width = nil, lindex = 0, rindex = 0) ⇒ Section

Create a new Section to render, with a formatter, lines column width, and start indexes for left and right side.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kronk/diff/output.rb', line 20

def initialize format, line_num_width=nil, lindex=0, rindex=0
  @format  = format
  @cwidth  = line_num_width
  @lindex  = lindex
  @rindex  = rindex
  @llen    = 0
  @rlen    = 0
  @lmeta   = nil
  @rmeta   = nil
  @lines   = []
  @context = 0
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def context
  @context
end

#formatObject

Returns the value of attribute format.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def format
  @format
end

#lindexObject

Returns the value of attribute lindex.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def lindex
  @lindex
end

#llenObject

Returns the value of attribute llen.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def llen
  @llen
end

#lmetaObject

Returns the value of attribute lmeta.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def lmeta
  @lmeta
end

#rindexObject

Returns the value of attribute rindex.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def rindex
  @rindex
end

#rlenObject

Returns the value of attribute rlen.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def rlen
  @rlen
end

#rmetaObject

Returns the value of attribute rmeta.



13
14
15
# File 'lib/kronk/diff/output.rb', line 13

def rmeta
  @rmeta
end

Instance Method Details

#add(obj, meta = nil) ⇒ Object

Append a line or diff section to the section. If obj is a String, common section is assumed, if obj is an Array, a diff section is assumed.

Metadata is optional but must be an Array of 2 items if given.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kronk/diff/output.rb', line 41

def add obj, meta=nil
  @lmeta, @rmeta = meta if meta && !@lmeta && !@rmeta
  @lmeta = ary_to_path @lmeta if Array === @rmeta
  @rmeta = ary_to_path @rmeta if Array === @rmeta

  if String === obj
    add_common obj

  elsif Array === obj
    left, right = obj
    left.each{|o| add_left o }
    right.each{|o| add_right o }
  end
end

#ary_to_path(ary) ⇒ Object

Create a Path String from an Array. Used when the meta data given for either side of the diff is an Array.



61
62
63
# File 'lib/kronk/diff/output.rb', line 61

def ary_to_path ary
  "#{Path::DCH}#{Path.join ary}"
end

#renderObject

Build the section String output once all lines and meta has been added.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kronk/diff/output.rb', line 70

def render
  cleft  = "#{@lindex+1},#{@llen}"
  cright = "#{@rindex+1},#{@rlen}"

  if @lmeta != @rmeta && @lmeta && @rmeta
    cleft  << " " << @lmeta
    cright << " " << @rmeta
  else
    info = @lmeta || @rmeta
  end

  [@format.context(cleft, cright, info), *@lines]
end