]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
Make colored output configurable
authorPhil Sutter <phil@nwl.cc>
Fri, 17 Aug 2018 16:38:45 +0000 (18:38 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Mon, 20 Aug 2018 15:54:06 +0000 (08:54 -0700)
Allow for -color={never,auto,always} to have colored output disabled,
enabled only if stdout is a terminal or enabled regardless of stdout
state.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: David Ahern <dsahern@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
bridge/bridge.c
include/color.h
ip/ip.c
lib/color.c
man/man8/bridge.8
man/man8/ip.8
man/man8/tc.8
tc/tc.c

index b3cab717ead30f2b42a5eda60e541a69263e6321..663a35b2b2e46cd186e96e641fd818699f329ba2 100644 (file)
@@ -173,8 +173,7 @@ main(int argc, char **argv)
                        NEXT_ARG();
                        if (netns_switch(argv[1]))
                                exit(-1);
-               } else if (matches(opt, "-color") == 0) {
-                       ++color;
+               } else if (matches_color(opt, &color)) {
                } else if (matches(opt, "-compressvlans") == 0) {
                        ++compress_vlans;
                } else if (matches(opt, "-force") == 0) {
index 4f2c918db7e4351698e14d664e5d5fef035c6b94..a22a00c2277e0f1db17d98b1adb16dd71d250502 100644 (file)
@@ -2,6 +2,8 @@
 #ifndef __COLOR_H__
 #define __COLOR_H__ 1
 
+#include <stdbool.h>
+
 enum color_attr {
        COLOR_IFNAME,
        COLOR_MAC,
@@ -12,8 +14,15 @@ enum color_attr {
        COLOR_NONE
 };
 
+enum color_opt {
+       COLOR_OPT_NEVER = 0,
+       COLOR_OPT_AUTO = 1,
+       COLOR_OPT_ALWAYS = 2
+};
+
 void enable_color(void);
 int check_enable_color(int color, int json);
+bool matches_color(const char *arg, int *val);
 void set_color_palette(void);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
 enum color_attr ifa_family_color(__u8 ifa_family);
diff --git a/ip/ip.c b/ip/ip.c
index 72e858eed50d56647db8e79e40feb8b1d66ada34..58c643df8a366a36c82aebde0564b79ffd3571e7 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -283,8 +283,7 @@ int main(int argc, char **argv)
                                exit(-1);
                        }
                        rcvbuf = size;
-               } else if (matches(opt, "-color") == 0) {
-                       ++color;
+               } else if (matches_color(opt, &color)) {
                } else if (matches(opt, "-help") == 0) {
                        usage();
                } else if (matches(opt, "-netns") == 0) {
index edf96e5c6ecd7e6dca27b922a8e39a63eb6beaf3..9c9023587748fac2744fa6760e42e87bbcfd9d45 100644 (file)
@@ -3,11 +3,13 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <linux/if.h>
 
 #include "color.h"
+#include "utils.h"
 
 enum color {
        C_RED,
@@ -79,13 +81,42 @@ void enable_color(void)
 
 int check_enable_color(int color, int json)
 {
-       if (color && !json) {
+       if (json || color == COLOR_OPT_NEVER)
+               return 1;
+
+       if (color == COLOR_OPT_ALWAYS || isatty(fileno(stdout))) {
                enable_color();
                return 0;
        }
        return 1;
 }
 
+bool matches_color(const char *arg, int *val)
+{
+       char *dup, *p;
+
+       if (!val)
+               return false;
+
+       dup = strdupa(arg);
+       p = strchrnul(dup, '=');
+       if (*p)
+               *(p++) = '\0';
+
+       if (matches(dup, "-color"))
+               return false;
+
+       if (*p == '\0' || !strcmp(p, "always"))
+               *val = COLOR_OPT_ALWAYS;
+       else if (!strcmp(p, "auto"))
+               *val = COLOR_OPT_AUTO;
+       else if (!strcmp(p, "never"))
+               *val = COLOR_OPT_NEVER;
+       else
+               return false;
+       return true;
+}
+
 void set_color_palette(void)
 {
        char *p = getenv("COLORFGBG");
index 1d10cb2b6a72c6d654cb52251999cf32892a5f40..53cd3d0a3d9330da75651d04c3fcb5e83502249f 100644 (file)
@@ -172,8 +172,17 @@ If there were any errors during execution of the commands, the application
 return code will be non zero.
 
 .TP
-.BR "\-c" , " -color"
-Use color output.
+.BR \-c [ color ][ = { always | auto | never }
+Configure color output. If parameter is omitted or
+.BR always ,
+color output is enabled regardless of stdout state. If parameter is
+.BR auto ,
+stdout is checked to be a terminal before enabling color output. If parameter is
+.BR never ,
+color output is disabled. If specified multiple times, the last one takes
+precedence. This flag is ignored if
+.B \-json
+is also given.
 
 .TP
 .BR "\-j", " \-json"
index 0087d18b74706870c16684d4976eb9d9eb790632..1d358879ec39c0eb9b1a3e8fc70f10b98e90e14e 100644 (file)
@@ -187,8 +187,17 @@ to
 executes specified command over all objects, it depends if command supports this option.
 
 .TP
-.BR "\-c" , " -color"
-Use color output.
+.BR \-c [ color ][ = { always | auto | never }
+Configure color output. If parameter is omitted or
+.BR always ,
+color output is enabled regardless of stdout state. If parameter is
+.BR auto ,
+stdout is checked to be a terminal before enabling color output. If parameter is
+.BR never ,
+color output is disabled. If specified multiple times, the last one takes
+precedence. This flag is ignored if
+.B \-json
+is also given.
 
 .TP
 .BR "\-t" , " \-timestamp"
index fd33f9b2f180697ba3ad2d5d4b18775029344440..f98398a34bd3e2cc934d07621b785f34bd788cb1 100644 (file)
@@ -755,8 +755,17 @@ option was specified. Classes can be filtered only by
 option.
 
 .TP
-.BR "\ -color"
-Use color output.
+.BR \-c [ color ][ = { always | auto | never }
+Configure color output. If parameter is omitted or
+.BR always ,
+color output is enabled regardless of stdout state. If parameter is
+.BR auto ,
+stdout is checked to be a terminal before enabling color output. If parameter is
+.BR never ,
+color output is disabled. If specified multiple times, the last one takes
+precedence. This flag is ignored if
+.B \-json
+is also given.
 
 .TP
 .BR "\-j", " \-json"
diff --git a/tc/tc.c b/tc/tc.c
index 4c7a128c8103e289bd0c3bb189ab9bc707c8f7e6..4b28e9b182c1d7e4ddd5d6cd0e87acbb5a32e185 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -496,8 +496,7 @@ int main(int argc, char **argv)
                                matches(argv[1], "-conf") == 0) {
                        NEXT_ARG();
                        conf_file = argv[1];
-               } else if (matches(argv[1], "-color") == 0) {
-                       ++color;
+               } else if (matches_color(argv[1], &color)) {
                } else if (matches(argv[1], "-timestamp") == 0) {
                        timestamp++;
                } else if (matches(argv[1], "-tshort") == 0) {