File indexing completed on 2025-01-19 03:59:54
0001 from io import StringIO 0002 from difflib import SequenceMatcher 0003 from ..exporters import prettyprint 0004 0005 0006 def difflines_str(a, b, widtha=None, widthb=None): 0007 lines_a = a.splitlines() 0008 lines_b = b.splitlines() 0009 ia = 0 0010 ib = 0 0011 0012 if widtha is None: 0013 widtha = max(map(len, lines_a)) 0014 if widthb is None: 0015 widthb = max(map(len, lines_b)) 0016 0017 for ja, jb, size in SequenceMatcher(None, lines_a, lines_b, False).get_matching_blocks(): 0018 sideprinter(lines_a[ia:ja], lines_b[ib:jb], widtha, widthb, "\x1b[31m>", "=", "<\x1b[m") 0019 ia = ja+size 0020 ib = jb+size 0021 sideprinter(lines_a[ja:ia], lines_b[jb:ib], widtha, widthb, "\x1b[m ", "|", " \x1b[m") 0022 0023 0024 def sideprinter(left, right, widtha=40, widthb=40, prefix="", infix=" | ", suffix=""): 0025 if len(left) > len(right): 0026 right += [""] * (len(left) - len(right)) 0027 else: 0028 left += [""] * (len(right) - len(left)) 0029 for l, r in zip(left, right): 0030 print("".join([prefix, l[:widtha].ljust(widtha), infix, r[:widthb].ljust(widthb), suffix])) 0031 0032 0033 def difflines(a, b, widtha=None, widthb=None): 0034 ioa = StringIO() 0035 prettyprint(a, ioa) 0036 iob = StringIO() 0037 prettyprint(b, iob) 0038 difflines_str(ioa.getvalue(), iob.getvalue(), widtha, widthb)