public
Last active

  • Download Gist
differ.rb
Ruby
1 2 3 4 5 6 7 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 33 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#diff parsing to side by side differences
 
class Differ
attr_reader :rows
LINE_REGEX = /^((?:---|[><\\]))/
CONTROL_REGEX = /(\d+(?:,\d+)?)([acd])(\d+(?:,\d+)?)/
def initialize(left_file, right_file)
@rows = []
@left_file = left_file
@right_file = right_file
@curr_control = {}
@raw_file = File.open(@left_file).read.split("\n")
@raw_file.unshift("") #diffs are 1 indexed
@index = 0
get_diff
while(@diff.length > 0 && @index <= @raw_file.length)
build
end
end
private
def get_diff
@diff = %x(diff #{@left_file} #{@right_file} --speed-large-files --ignore-all-space --ignore-blank-lines).split("\n")
end
def build
control_string
advance
diff_string
end
def control_string
return if !@diff.first.match(CONTROL_REGEX)
p "control"
@index = !@curr_control[:from].nil? ?
@curr_control[:from].last.to_i+1 : 0
@diff_line = @diff.shift
matches = @diff_line.match(CONTROL_REGEX)
@curr_control = {:from => matches[1].split(","),
:to => matches[3].split(","),
:control => matches[2]}
end
def diff_string
return if !@diff.first.match(LINE_REGEX)
@curr_diff = [[],[]]
while(!@diff.first.nil? && @diff.first.match(LINE_REGEX))
p "diff"
@diff_line = @diff.shift
case @diff_line[0,1]
when "<" then @curr_diff[0] << @diff_line[2, @diff_line.length - 1]
when ">" then @curr_diff[1] << @diff_line[2, @diff_line.length - 1]
end
end
 
end_range = @curr_diff[0].length > @curr_diff[1].length ?
@curr_diff[0].length : @curr_diff[1].length
(0..end_range-1).each do |index|
@rows << [@curr_diff[0][index] || "", @curr_diff[1][index] || "", @curr_control[:control]]
end
end
def advance
while(@index < @curr_control[:from].first.to_i)
p "advance"
insert_step
end
end
def insert_step
@rows << [@raw_file[@index], @raw_file[@index], ""]
@index = @index + 1
end
class << self
def run(directory=".")
Differ.new(Dir["#{directory}/*.txt"][0], Dir["#{directory}/*.txt"][1])
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.