|
| 1 | +import typing |
| 2 | +from math import log10, ceil |
| 3 | +import difflib |
| 4 | +from RichConsole import RichStr, groups, rsjoin |
| 5 | + |
| 6 | + |
| 7 | +def getMaxWidth(column: typing.List[str]) -> int: |
| 8 | + return max(len(str(e)) for e in column) |
| 9 | + |
| 10 | + |
| 11 | +def deletedStyle(l: str) -> RichStr: |
| 12 | + return groups.CrossedOut.crossedOut(groups.Fore.green(l)) |
| 13 | + |
| 14 | + |
| 15 | +StyleT = typing.Callable[[str], typing.Union[str, RichStr]] |
| 16 | +StylePairT = typing.Tuple[StyleT, StyleT] |
| 17 | + |
| 18 | +StringConverterT = typing.Callable[[typing.Any], str] |
| 19 | + |
| 20 | +def dummy(x): |
| 21 | + return x |
| 22 | + |
| 23 | +StyleDictT = typing.Dict[str, typing.Union[StyleT, StylePairT]] |
| 24 | +defaultStyle = { |
| 25 | + "equal": (dummy, dummy), |
| 26 | + "replace": (groups.Fore.lightblueEx, groups.Fore.lightblueEx), |
| 27 | + "insert": (groups.Back.lightyellowEx, groups.Fore.lightgreenEx), |
| 28 | + "delete": (deletedStyle, groups.Back.lightredEx), |
| 29 | + "lineNo": groups.Fore.lightcyanEx |
| 30 | +} |
| 31 | + |
| 32 | +rangeOrSlice = typing.Union[slice, range] |
| 33 | + |
| 34 | + |
| 35 | +def azip(a: typing.Any, ar: rangeOrSlice, b: typing.Any, br: rangeOrSlice, substitute: typing.Any = None) -> typing.Iterator[typing.Any]: |
| 36 | + #ar.stop = typing.cast(int, ar.stop) |
| 37 | + #ar.start = typing.cast(int, ar.start) |
| 38 | + #br.stop = typing.cast(int, br.stop) |
| 39 | + #br.start = typing.cast(int, br.start) |
| 40 | + |
| 41 | + al = ar.stop - ar.start |
| 42 | + bl = br.stop - br.start |
| 43 | + |
| 44 | + d = al - bl |
| 45 | + comm = min(al, bl) |
| 46 | + yield from zip(a[ar.start : ar.start + comm], range(ar.start, ar.start + comm), b[br.start : br.start + comm], range(br.start, br.start + comm)) |
| 47 | + |
| 48 | + if d > 0: |
| 49 | + yield from zip(a[ar.start + comm : ar.stop], range(ar.start + comm, ar.stop), [substitute] * d, [None] * d) |
| 50 | + elif d < 0: |
| 51 | + yield from zip([substitute] * (-d), [None] * (-d), b[br.start + comm : br.stop], range(br.start + comm, br.stop)) |
| 52 | + |
| 53 | + |
| 54 | +def genPadding(l: int, padder: str = " ") -> str: |
| 55 | + return padder * l |
| 56 | + |
| 57 | + |
| 58 | +LineNumberT = typing.Union[str, int] |
| 59 | + |
| 60 | + |
| 61 | +def genNum(n: LineNumberT, numWidth: int) -> str: |
| 62 | + r = str(n) |
| 63 | + return genPadding(numWidth - len(r), "0") + r |
| 64 | + |
| 65 | + |
| 66 | +def generateRichStrLineDiff(al: str, aln: LineNumberT, bl: str, bln: LineNumberT, opStyle: StylePairT, lineNoStyle: StyleT, maxW: typing.Tuple[int, int], numWidth: int, strConverter: StringConverterT) -> RichStr: |
| 67 | + if al is None: |
| 68 | + al = "" |
| 69 | + if bl is None: |
| 70 | + bl = "" |
| 71 | + al = strConverter(al) |
| 72 | + bl = strConverter(bl) |
| 73 | + |
| 74 | + if aln is None: |
| 75 | + aln = genPadding(numWidth) |
| 76 | + |
| 77 | + if bln is None: |
| 78 | + bln = genPadding(numWidth) |
| 79 | + |
| 80 | + return rsjoin(" ", ( |
| 81 | + lineNoStyle(genNum(aln, numWidth)), |
| 82 | + opStyle[0](al), |
| 83 | + genPadding(maxW[0] - len(al)), |
| 84 | + lineNoStyle(genNum(bln, numWidth)), |
| 85 | + opStyle[1](bl) |
| 86 | + )) |
| 87 | + |
| 88 | + |
| 89 | +OpcodeT = typing.Tuple[str, int, int, int, int] |
| 90 | + |
| 91 | +def sideBySideDiff(a: typing.List[str], b: typing.List[str], *, style: typing.Optional[StyleDictT] = None, strConverter: typing.Optional[StringConverterT] = None, names: typing.Optional[typing.Tuple[str, str]] = None, matcherClass: typing.Optional[typing.Type[difflib.SequenceMatcher]]=None, opcodes: typing.Optional[typing.Iterable[OpcodeT]]=None) -> typing.Iterator[RichStr]: |
| 92 | + style_ = type(defaultStyle)(defaultStyle) |
| 93 | + if style is not None: |
| 94 | + style_.update(style) |
| 95 | + if strConverter is None: |
| 96 | + strConverter = str |
| 97 | + |
| 98 | + lns = style_["lineNo"] |
| 99 | + |
| 100 | + |
| 101 | + if opcodes is None: |
| 102 | + if matcherClass is None: |
| 103 | + matcherClass = difflib.SequenceMatcher |
| 104 | + m = matcherClass(None, a, b) |
| 105 | + opcodes = m.get_opcodes() |
| 106 | + |
| 107 | + maxW = (getMaxWidth(a), getMaxWidth(b)) |
| 108 | + numMaxWidth = ceil(log10(max(len(a), len(b)))) |
| 109 | + |
| 110 | + if names is not None: |
| 111 | + maxW = typing.cast(typing.Tuple[int, int], tuple(max(szs) for szs in zip(maxW, (len(n) for n in names)))) |
| 112 | + secondHeaderPadding = 1 + numMaxWidth + maxW[0] # space between first line and its number # padding |
| 113 | + yield " ".join((genPadding(numMaxWidth), names[0], genPadding(secondHeaderPadding - len(names[0])), names[1])) |
| 114 | + |
| 115 | + for oc, aS, aE, bS, bE in opcodes: |
| 116 | + opStyle = style_[oc] |
| 117 | + if oc == "equal": |
| 118 | + for al, aln, bl, bln in zip(a[aS:aE], range(aS, aE), b[bS:bE], range(bS, bE)): |
| 119 | + yield generateRichStrLineDiff(al, aln, bl, bln, opStyle, lns, maxW, numMaxWidth, strConverter) |
| 120 | + |
| 121 | + elif oc == "replace": |
| 122 | + for al, aln, bl, bln in azip(a, range(aS, aE), b, range(bS, bE), None): |
| 123 | + yield generateRichStrLineDiff(al, aln, bl, bln, opStyle, lns, maxW, numMaxWidth, strConverter) |
| 124 | + elif oc == "insert": |
| 125 | + for bl, bln in zip(b[bS:bE], range(bS, bE)): |
| 126 | + yield generateRichStrLineDiff(genPadding(maxW[0]), genPadding(numMaxWidth), bl, bln, opStyle, lns, maxW, numMaxWidth, strConverter) |
| 127 | + elif oc == "delete": |
| 128 | + for al, aln in zip(a[aS:aE], range(aS, aE)): |
| 129 | + yield generateRichStrLineDiff(al, aln, genPadding(maxW[1]), genPadding(numMaxWidth), opStyle, lns, maxW, numMaxWidth, strConverter) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + from pathlib import Path |
| 134 | + import sys |
| 135 | + with Path(sys.argv[1]).open("rt", encoding="utf-8") as f0: |
| 136 | + with Path(sys.argv[2]).open("rt", encoding="utf-8") as f1: |
| 137 | + for l in sideBySideDiff(f0.read().splitlines(), f1.read().splitlines()): |
| 138 | + print(l) |
0 commit comments