pytracediff: implement pager ('less') invocation internally

In order to get rid of the ntracediff.sh wrapper script, implement
invocation of 'less' internally, if the stdout is determined to
be a tty. Otherwise just print out normally.

Signed-off-by: Matti Hamalainen <ccr@tnsp.org>
Acked-by: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17135>
This commit is contained in:
Matti Hamalainen 2022-06-20 14:20:09 +03:00 committed by Matti Hämäläinen
parent 95fc0e1b7c
commit aab5d176b8
1 changed files with 127 additions and 93 deletions

View File

@ -26,12 +26,14 @@
##########################################################################
from parse import *
import os
import sys
import re
import signal
import functools
import argparse
import difflib
import subprocess
assert sys.version_info >= (3, 6), 'Python >= 3.6 required'
@ -54,6 +56,8 @@ PKK_ANSI_ITALIC = '3m'
###
def pkk_fatal(msg):
print(f"ERROR: {msg}", file=sys.stderr)
if outpipe is not None:
outpipe.terminate()
sys.exit(1)
@ -61,8 +65,17 @@ def pkk_info(msg):
print(msg, file=sys.stderr)
def pkk_output(outpipe, msg):
if outpipe is not None:
print(msg, file=outpipe.stdin)
else:
print(msg)
def pkk_signal_handler(signal, frame):
print("\nQuitting due to SIGINT / Ctrl+C!")
if outpipe is not None:
outpipe.terminate()
sys.exit(1)
@ -259,6 +272,16 @@ def pkk_format_line(line, indent, width):
### Main program starts
###
if __name__ == "__main__":
### Check if output is a terminal
outpipe = None
redirect = False
try:
defwidth = os.get_terminal_size().columns
redirect = True
except OSError:
defwidth = 80
signal.signal(signal.SIGINT, pkk_signal_handler)
### Parse arguments
@ -307,7 +330,7 @@ if __name__ == "__main__":
optparser.add_argument("-w", "--width",
dest="output_width",
type=functools.partial(pkk_arg_range, vmin=16, vmax=512), default=80,
type=functools.partial(pkk_arg_range, vmin=16, vmax=512), default=defwidth,
metavar="N",
help="output width (default: %(default)s)")
@ -327,6 +350,11 @@ if __name__ == "__main__":
print("The files are identical.")
sys.exit(0)
### Redirect output to 'less' if stdout is a tty
try:
if redirect:
outpipe = subprocess.Popen(["less", "-S", "-R"], stdin=subprocess.PIPE, encoding="utf8")
### Output results
pkk_info("Outputting diff ...")
colwidth = int((options.output_width - 3) / 2)
@ -340,7 +368,7 @@ if __name__ == "__main__":
show_args = False
if options.suppress_common:
if tag != prevtag:
print("[...]")
pkk_output(outpipe, "[...]")
continue
sep = "|"
@ -427,7 +455,7 @@ if __name__ == "__main__":
ansi1 = ansi2 = ansi1 + PKK_ANSI_ESC + PKK_ANSI_YELLOW
# Output line
print(colfmt.format(
pkk_output(outpipe, colfmt.format(
ansi1, pkk_format_line(line1, indent, colwidth), ansiend,
ansisep, sep, ansiend,
ansi2, pkk_format_line(line2, indent, colwidth), ansiend).
@ -436,6 +464,12 @@ if __name__ == "__main__":
nline += 1
if tag == "equal" and options.suppress_common:
print("[...]")
pkk_output(outpipe, "[...]")
prevtag = tag
except Exception as e:
pkk_fatal(str(e))
if outpipe is not None:
outpipe.communicate()