Hash :
e700bce1
Author :
Date :
2011-09-20T11:20:53
[util] Add hb-diff A diff program written in Python that is more suitable for comparing hb-shape output from different backends. Main differences with stock diff: 1. It outputs one line's comparison at a time, as opposed to batching '+' lines and '-' lines. 2. It colors the part of the line that changed, taking word boundaries into consideration. You can pipe the colored output to 'less -r'.
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
#!/usr/bin/python
import sys, re, difflib, os
red_color = green_color = end_color = ""
if 1 or os.isatty (sys.stdout.fileno ()):
red_color = '\033[41;37;1m'
green_color = '\033[42;37;1m'
end_color = '\033[m'
def fancy_diff (l1, l2):
sep = "|<>@+,="
r ="(["+sep+"]?)([^"+sep+"]*)"
ss = [re.findall (r, l) for l in (l1, l2)]
ss = [reduce ((lambda a,b: a+b), s) for s in ss]
ss = [[x+"\n" for x in s] for s in ss]
oo = ["",""]
st = [0,0]
for l in difflib.Differ().compare (*ss):
if l[0] == '?':
continue
if l[0] == ' ':
for i in range(2):
if st[i]:
oo[i] += end_color
st[i] = 0
oo = [o + l[2:] for o in oo]
continue
if l[0] == '-':
if not st[0]:
oo[0] += red_color
st[0] = 1
oo[0] += l[2:]
continue
if l[0] == '+':
if not st[1]:
oo[1] += green_color
st[1] = 1
oo[1] += l[2:]
for i in range(2):
if st[i]:
oo[i] += end_color
st[i] = 0
oo = [o.replace ('\n', '') for o in oo]
if oo[0] == oo[1]:
return ' ' + oo[0] + '\n'
return '-'+oo[0]+'\n'+'+'+oo[1]+'\n'
f1, f2 = (file (f) for f in sys.argv[1:3])
for (l1,l2) in zip (f1, f2):
if l1 == l2:
print " " + l1.strip ()
continue
l1, l2 = l1.strip (), l2.strip ()
sys.stdout.writelines (fancy_diff (l1, l2))