Class: DiffToString

Inherits:
Object
  • Object
show all
Includes:
Style
Defined in:
lib/nandoc/support/diff-to-string.rb

Overview

TODO:

move this to minitest branch

TODO:

this gives different results than diff for some stuff!!??

turn the output of Diff::LCS.diff into a string similar to what would be retured by ‘diff`, optionally make it looks sorta like colorized output from git-diff

poor man’s diff:

file_a, file_b = ARGV.shift(2)
puts DiffToString.files_diff(file_a, file_b)

Defined Under Namespace

Modules: Style Classes: TestCase

Constant Summary

Constants included from Style

Style::Codes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Style

#stylize

Constructor Details

#initializeDiffToString

Returns a new instance of DiffToString.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nandoc/support/diff-to-string.rb', line 41

def initialize
  @add_style = nil
  @add_header    = '%sa%s'
  @change_header = '%sc%s'
  @context       = nil
  @del_header    = '%sd%s'
  @del_style = nil
  @last_range = nil
  @left  = '<'
  @line_no_style = nil
  @right = '>'
  @separator_line = '---'
  @trailing_whitespace_style = nil
end

Instance Attribute Details

#arr1Object

this is awful bleeding



55
56
57
# File 'lib/nandoc/support/diff-to-string.rb', line 55

def arr1
  @arr1
end

#arr2Object

this is awful bleeding



55
56
57
# File 'lib/nandoc/support/diff-to-string.rb', line 55

def arr2
  @arr2
end

Instance Method Details

#arrays_diff(arr1, arr2, opts = {}) ⇒ Object



74
75
76
77
78
79
# File 'lib/nandoc/support/diff-to-string.rb', line 74

def arrays_diff arr1, arr2, opts={}
  diff = Diff::LCS.diff(arr1, arr2)
  @arr1, @arr2 = arr1, arr2
  consume_opts_for_diff(opts)
  diff_to_str diff, opts
end

#context=(mixed) ⇒ Object



56
57
58
59
# File 'lib/nandoc/support/diff-to-string.rb', line 56

def context= mixed
  fail("no #{mixed.inspect}") unless mixed.kind_of?(Fixnum) && mixed >= 0
  @context = mixed == 0 ? nil : mixed
end

#diff(mixed1, mixed2, opts = {}) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/nandoc/support/diff-to-string.rb', line 80

def diff mixed1, mixed2, opts={}
  case (x=[mixed1.class, mixed2.class])
  when [Array,Array];   arrays_diff(mixed1,mixed2,opts)
  when [String,String]; strings_diff(mixed1,mixed2,opts)
  else "no diff strategy for #{x.inspect}"
  end
end

#diff_to_str(diff, opts) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nandoc/support/diff-to-string.rb', line 101

def diff_to_str diff, opts
  consume_opts_for_diff opts
  @out = StringIO.new
  @offset_offset = -1
  diff.each do |chunk|
    context_pre(chunk) if @context
    dels = []
    adds = []
    start_add = last_add = start_del = last_del = nil
    chunk.each do |change|
      case change.action
      when '+'
        start_add ||= change.position + 1
        last_add = change.position + 1
        adds.push change.element
      when '-'
        start_del ||= change.position + 1
        last_del = change.position + 1
        dels.push change.element
      else
        fail("no: #{change.action}")
      end
    end
    if adds.any? && dels.any?
      puts_change_header start_del, last_del, start_add, last_add
    elsif adds.any?
      puts_add_header start_add, last_add
    else
      puts_del_header start_del, last_del
    end
    @offset_offset -= ( dels.size - adds.size )
    dels.each do |del|
      puts_del "#{@left} #{del}"
    end
    if adds.any? && dels.any?
      puts_sep
    end
    adds.each do |add|
      puts_add "#{@right} #{add}"
    end
    context_post(chunk) if @context
  end
  @out.rewind
  @out.read
end

#files_diff(a, b, opts = {:sep=>"\n"}) ⇒ Object



87
88
89
90
91
# File 'lib/nandoc/support/diff-to-string.rb', line 87

def files_diff a, b, opts={:sep=>"\n"}
  str1 = File.read(a)
  str2 = File.read(b)
  strings_diff(str1, str2, opts)
end

#gitlike!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nandoc/support/diff-to-string.rb', line 60

def gitlike!
  common_header = '@@ -%s, +%s @@'
  @add_header =  common_header
  @add_style = [:bold, :green]
  @change_header = common_header
  @del_style = [:bold, :red]
  @del_header = common_header
  @header_style = [:bold, :magenta]
  @left  = '-'
  @right = '+'
  @separator_line = nil
  @trailing_whitespace_style = [:red_bg]
  self
end

#str_to_arr(str, sep) ⇒ Object



98
99
100
# File 'lib/nandoc/support/diff-to-string.rb', line 98

def str_to_arr str, sep
  str.split(sep, -1)
end

#strings_diff(a, b, opts = {}) ⇒ Object



92
93
94
95
96
97
# File 'lib/nandoc/support/diff-to-string.rb', line 92

def strings_diff a, b, opts={}
  opts = opts.merge(:sep=>"\n")
  arr1 = str_to_arr a, opts[:sep]
  arr2 = str_to_arr b, opts[:sep]
  arrays_diff(arr1, arr2, opts)
end