Created
October 9, 2011 20:06
-
-
Save cmur2/1274089 to your computer and use it in GitHub Desktop.
Ruby command line diff based on Diff::LCS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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