From ce98e98c5993ec654138c76a9445e16197e799c4 Mon Sep 17 00:00:00 2001 From: Julianne Swinoga Date: Fri, 10 Mar 2023 22:10:21 -0500 Subject: [PATCH] Add --color option to file_parser.py to force color output --- tracex_parser/file_parser.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tracex_parser/file_parser.py b/tracex_parser/file_parser.py index 60a7d23..51cfa0b 100755 --- a/tracex_parser/file_parser.py +++ b/tracex_parser/file_parser.py @@ -15,7 +15,8 @@ help='Path to the input trx file(s) that contains TraceX event data') parser.add_argument('-v', '--verbose', action='count', default=0, help='Set the verbosity of logging') -parser.add_argument('-n', '--nocolor', action='store_true', help='Do not add color to the output') +parser.add_argument('-n', '--nocolor', action='store_true', help='Never color the output') +parser.add_argument('-c', '--color', action='store_true', help='Always color the output') def get_endian_str(buf: bytes) -> Tuple[str, int]: @@ -211,6 +212,18 @@ def main(): signal(SIGPIPE, SIG_DFL) # set up colours - have_colours = sys.stdout.isatty() and not args.nocolor + # Truth table for what we want. + # Precedence is: color, nocolor, tty + # tty | nocolor | color | output + # ============================== + # 0 | 0 | 0 | 0 + # 0 | 0 | 1 | 1 + # 0 | 1 | 0 | 1 + # 0 | 1 | 1 | 1 + # 1 | 0 | 0 | 1 + # 1 | 0 | 1 | 1 + # 1 | 1 | 0 | 0 + # 1 | 1 | 1 | 1 + have_colours = (sys.stdout.isatty() and not args.nocolor) or args.color colour = TextColour(have_colours) main()