From e700bce1189465a159a7c3c179f231be224f31cc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 20 Sep 2011 11:20:53 -0400 Subject: [PATCH] [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'. --- util/Makefile.am | 2 ++ util/hb-diff | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 util/hb-diff diff --git a/util/Makefile.am b/util/Makefile.am index cb91381..223a59a 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -57,4 +57,6 @@ bin_PROGRAMS += hb-shape endif endif +EXTRA_DIST += hb-diff + -include $(top_srcdir)/git.mk diff --git a/util/hb-diff b/util/hb-diff new file mode 100755 index 0000000..7b91601 --- /dev/null +++ b/util/hb-diff @@ -0,0 +1,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)) -- 2.7.4