From 8819d372e5df93389bd72d2281a9119e8d402366 Mon Sep 17 00:00:00 2001 From: Matti Hamalainen Date: Mon, 24 Jan 2022 23:02:48 +0200 Subject: pytracediff: add per-line difference highlighting for blocks Highlight differing _lines_ in the differing blocks, with somewhat different ANSI colors. Signed-off-by: Matti Hamalainen Acked-by: Mike Blumenkrantz Reviewed-by: Dylan Baker Part-of: --- src/gallium/tools/trace/pytracediff.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/gallium/tools') diff --git a/src/gallium/tools/trace/pytracediff.py b/src/gallium/tools/trace/pytracediff.py index 64f2722302d..2e7888cf947 100755 --- a/src/gallium/tools/trace/pytracediff.py +++ b/src/gallium/tools/trace/pytracediff.py @@ -43,8 +43,10 @@ PKK_ANSI_ESC = '\33[' PKK_ANSI_NORMAL = '0m' PKK_ANSI_RED = '31m' PKK_ANSI_GREEN = '32m' +PKK_ANSI_YELLOW = '33m' PKK_ANSI_PURPLE = '35m' PKK_ANSI_BOLD = '1m' +PKK_ANSI_ITALIC = '3m' ### @@ -234,9 +236,16 @@ def pkk_parse_trace(filename, options, state): return parser.call_stack -def pkk_get_line(data, nline, indent, width): +def pkk_get_line(data, nline): if nline < len(data): - tmp = indent + data[nline] + return data[nline] + else: + return None + + +def pkk_format_line(line, indent, width): + if line is not None: + tmp = indent + line if len(tmp) > width: return tmp[0:(width - 3)] + "..." else: @@ -343,8 +352,7 @@ if __name__ == "__main__": ansi2 = "" elif tag == "replace": sep = ">" - ansi1 = PKK_ANSI_ESC + PKK_ANSI_BOLD - ansi2 = PKK_ANSI_ESC + PKK_ANSI_BOLD + ansi1 = ansi2 = PKK_ANSI_ESC + PKK_ANSI_BOLD else: pkk_fatal(f"Internal error, unsupported difflib.SequenceMatcher operation '{tag}'.") @@ -394,11 +402,21 @@ if __name__ == "__main__": else: indent = "" + line1 = pkk_get_line(data1, nline) + line2 = pkk_get_line(data2, nline) + + # Highlight differing lines if not plain + if not options.plain and line1 != line2: + if tag == "insert" or tag == "delete": + ansi1 = ansi1 + PKK_ANSI_ESC + PKK_ANSI_BOLD + elif tag == "replace": + ansi1 = ansi2 = ansi1 + PKK_ANSI_ESC + PKK_ANSI_YELLOW + # Output line print(colfmt.format( - ansi1, pkk_get_line(data1, nline, indent, colwidth), ansiend, + ansi1, pkk_format_line(line1, indent, colwidth), ansiend, ansisep, sep, ansiend, - ansi2, pkk_get_line(data2, nline, indent, colwidth), ansiend). + ansi2, pkk_format_line(line2, indent, colwidth), ansiend). rstrip()) nline += 1 -- cgit v1.2.3