From: Carl Worth Date: Thu, 20 Oct 2011 22:22:09 +0000 (-0700) Subject: tracedump: Add tri-state --color option (auto, always, or never) X-Git-Url: https://git.cworth.org/git?p=apitrace;a=commitdiff_plain;h=e525e6f8733c35f5b9d7b6da1ce1e56a7d70f5ba tracedump: Add tri-state --color option (auto, always, or never) This follows a pattern similar to that provided by other tools, (such as grep). The default auto mode colorizes if stdout is a tty. The always and never modes explicit enable or disable colorizing. The old --no-color and --no-colour options are deprecated, (no longer documented but still supported to avoid any regressions of scripts). Signed-off-by: José Fonseca --- diff --git a/scripts/tracediff.sh b/scripts/tracediff.sh index 4449d75..6129234 100755 --- a/scripts/tracediff.sh +++ b/scripts/tracediff.sh @@ -31,7 +31,7 @@ TRACEDUMP=${TRACEDUMP:-`dirname "$0"`/../tracedump} $TRACEDUMP stripdump () { - $TRACEDUMP --no-color "$1" \ + $TRACEDUMP --color=never "$1" \ | sed \ -e 's/\r$//g' \ -e 's/^[0-9]\+ //' \ diff --git a/tracedump.cpp b/tracedump.cpp index c210a0c..3b3ed0a 100644 --- a/tracedump.cpp +++ b/tracedump.cpp @@ -34,16 +34,24 @@ #include "trace_parser.hpp" -static bool color = true; +enum ColorOption { + COLOR_OPTION_NEVER = 0, + COLOR_OPTION_ALWAYS = 1, + COLOR_OPTION_AUTO = -1 +}; + +static ColorOption color = COLOR_OPTION_AUTO; static void usage(void) { std::cout << - "Usage: tracedump [OPTION] [TRACE...]\n" + "Usage: tracedump [OPTION] [TRACE]...\n" "Dump TRACE to standard output.\n" "\n" - " --no-color no colored syntax highlightint\n" - " --no-colour alias for --no-color\n" + " --help display this help and exit\n" + " --color[=WHEN]\n" + " --colour[=WHEN] colored syntax highlighting;\n" + " WHEN is 'always', 'never', or 'auto'\n" ; } @@ -61,9 +69,22 @@ int main(int argc, char **argv) if (!strcmp(arg, "--")) { break; - } else if (!strcmp(arg, "--no-color") || + } else if (!strcmp(arg, "--help")) { + usage(); + return 0; + } else if (!strcmp(arg, "--color=auto") || + !strcmp(arg, "--colour=auto")) { + color = COLOR_OPTION_AUTO; + } else if (!strcmp(arg, "--color") || + !strcmp(arg, "--colour") || + !strcmp(arg, "--color=always") || + !strcmp(arg, "--colour=always")) { + color = COLOR_OPTION_ALWAYS; + } else if (!strcmp(arg, "--color=never") || + !strcmp(arg, "--colour=never") || + !strcmp(arg, "--no-color") || !strcmp(arg, "--no-colour")) { - color = false; + color = COLOR_OPTION_NEVER; } else { std::cerr << "error: unknown option " << arg << "\n"; usage(); @@ -71,6 +92,14 @@ int main(int argc, char **argv) } } + if (color == COLOR_OPTION_AUTO) { +#ifdef _WIN32 + color = COLOR_OPTION_ALWAYS; +#else + color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER; +#endif + } + for (; i < argc; ++i) { Trace::Parser p;