Skip to content

Instantly share code, notes, and snippets.

@cmur2
Created October 9, 2011 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmur2/1274089 to your computer and use it in GitHub Desktop.
Save cmur2/1274089 to your computer and use it in GitHub Desktop.
Ruby command line diff based on Diff::LCS
#!/usr/bin/ruby -Ku
DEBUG = false
require 'rubygems'
require 'diff/lcs'
class OO
def green; "\e[42m\e[1;37m" end
def red; "\e[41m\e[1;37m" end
def rst; "\e[0m" end
def initialize(a,b)
@a = a.chars.to_a
@ha = @a.map { false }
@b = b.chars.to_a
@hb = @b.map { false }
end
def match(e)
# good to know...
puts "ERROR" if e.action != '='
puts "M: #{e.inspect}" if DEBUG
end
def discard_a(e)
puts "ERROR" if e.action != '-'
@ha[e.old_position] = true
puts "A: #{e.inspect}" if DEBUG
end
def discard_b(e)
puts "ERROR" if e.action != '+'
@hb[e.new_position] = true
puts "B: #{e.inspect}" if DEBUG
end
def change(e)
puts "ERROR" if e.action != '!'
@ha[e.old_position] = true
@hb[e.new_position] = true
puts "C: #{e.inspect}" if DEBUG
end
def debug
i = 0
@a.each do |a|
print red,a,rst if @ha[i]
print a if not @ha[i]
i += 1
end
puts
i = 0
@b.each do |b|
print green,b,rst if @hb[i]
print b if not @hb[i]
i += 1
end
puts
end
end
a = ARGV[0] || "old string"
b = ARGV[1] || "new string"
o = OO.new(a,b)
Diff::LCS.traverse_balanced a, b, o
o.debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment