]> git.cworth.org Git - apitrace/commitdiff
tracedump: Add tri-state --color option (auto, always, or never)
authorCarl Worth <cworth@cworth.org>
Thu, 20 Oct 2011 22:22:09 +0000 (15:22 -0700)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Mon, 24 Oct 2011 12:28:13 +0000 (13:28 +0100)
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 <jose.r.fonseca@gmail.com>
scripts/tracediff.sh
tracedump.cpp

index 4449d758ebdc62a0ceebccf1fa2d8f081ff8c123..61292346bbbf4115c08e85e594833fe8e23e5fd3 100755 (executable)
@@ -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]\+ //' \
index c210a0c9dd097119076f38642398c2673087283b..3b3ed0aa4482a283f3dec218877d9245daef9a90 100644 (file)
 #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;