Class: Kronk::Diff::Output

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

Overview

Renders diff outputs.

Defined Under Namespace

Classes: Section

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Output

Create a new Kronk::Diff::Output instance. Options supported are:

:context

Integer - Number of context lines around diffs; default 3

:diff_format

String/Object - Formatter for the diff; default AsciiFormat

:join_char

String - Vharacter to join diff sections with; default n

:labels

Array - Left and right names to display; default %wright

:show_lines

Boolean - Show lines in diff; default false



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kronk/diff/output.rb', line 146

def initialize opts={}
  @format =
    self.class.formatter(opts[:format] || Kronk.config[:diff_format]) ||
    AsciiFormat

  @context = Kronk.config[:context]
  @context = opts[:context] if opts[:context] || opts[:context] == false

  @join_ch = opts[:join_char] || "\n"

  @labels      = Array(opts[:labels])
  @labels[0] ||= "left"
  @labels[1] ||= "right"

  @show_lines = opts[:show_lines] || Kronk.config[:show_lines]
  @section    = false
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



134
135
136
# File 'lib/kronk/diff/output.rb', line 134

def context
  @context
end

#formatObject

Returns the value of attribute format.



134
135
136
# File 'lib/kronk/diff/output.rb', line 134

def format
  @format
end

#join_chObject

Returns the value of attribute join_ch.



134
135
136
# File 'lib/kronk/diff/output.rb', line 134

def join_ch
  @join_ch
end

#labelsObject

Returns the value of attribute labels.



134
135
136
# File 'lib/kronk/diff/output.rb', line 134

def labels
  @labels
end

#show_linesObject

Returns the value of attribute show_lines.



134
135
136
# File 'lib/kronk/diff/output.rb', line 134

def show_lines
  @show_lines
end

Class Method Details

.formatter(name) ⇒ Object

Returns a formatter from a symbol or string. Returns nil if not found.



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kronk/diff/output.rb', line 121

def self.formatter name
  return unless name

  return name        if Class === name
  return AsciiFormat if name.to_s =~ /^ascii(_diff)?$/
  return ColorFormat if name.to_s =~ /^color(_diff)?$/
  Kronk.find_const name

rescue NameError
  raise Kronk::Error, "No such formatter: #{name.inspect}"
end

Instance Method Details

#end_section?(i, diff_ary) ⇒ Boolean

Determine if index i is the end of a diff section to render, including surrounding context.

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/kronk/diff/output.rb', line 188

def end_section? i, diff_ary
  @section &&
    (i >= diff_ary.length ||
     !section?(i, diff_ary) && @context && @section.context >= @context)
end

#line_col_width(diff_ary) ⇒ Object

Determine the width of the line number column from a diff Array.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/kronk/diff/output.rb', line 198

def line_col_width diff_ary
  lines1, lines2 = diff_ary.inject([0,0]) do |prev, obj|
    if Array === obj
      [prev[0] + obj[0].length, prev[1] + obj[1].length]
    else
      [prev[0] + 1, prev[1] + 1]
    end
  end

  (lines1 > lines2 ? lines1 : lines2).to_s.length
end

#render(diff_ary, meta = []) ⇒ Object

Render a diff String from a diff Array, with optional metadata.

The meta argument must be an Array of 2 Arrays, one for each side of the diff.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/kronk/diff/output.rb', line 217

def render diff_ary, meta=[]
  output = []
  output << @format.head(*@labels)

  line1 = line2 = 0
  lwidth = line_col_width diff_ary if @show_lines

  diff_ary.each_with_index do |item, i|
    @section = Section.new @format, lwidth, line1, line2 if
      start_section? i, diff_ary

    @section.add item, meta[i] if @section

    line1 += Array === item ? item[0].length : 1
    line2 += Array === item ? item[1].length : 1

    if end_section?(i+1, diff_ary)
      output.concat @section.render
      @section = false
    end
  end

  output.join(@join_ch)
end

#section?(i, diff_ary) ⇒ Boolean

Determine if index i is a part of a section to render, including surrounding context.

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/kronk/diff/output.rb', line 169

def section? i, diff_ary
   !@context ||
    !!diff_ary[i,@context+1].to_a.find{|da| Array === da}
end

#start_section?(i, diff_ary) ⇒ Boolean

Determine if index i is the beginning of a diff section to render, including surrounding context.

Returns:

  • (Boolean)


179
180
181
# File 'lib/kronk/diff/output.rb', line 179

def start_section? i, diff_ary
  !@section && section?(i, diff_ary)
end