]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
allow overriding color option in environment
authorStephen Hemminger <stephen@networkplumber.org>
Fri, 15 Sep 2023 16:41:17 +0000 (09:41 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 15 Sep 2023 16:41:17 +0000 (09:41 -0700)
For ip, tc, and bridge command introduce a new way to enable
automatic colorization via environment variable.

Example:
  $ IP_COLOR=auto ip -br show addr

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 704be50c70b30e6d1e19b18b7b85c0981e6224ff..f9a245cb3670858f39064ce7a3a487a4862ca9fb 100644 (file)
@@ -23,7 +23,6 @@ int preferred_family = AF_UNSPEC;
 int oneline;
 int show_stats;
 int show_details;
-static int color;
 int compress_vlans;
 int json;
 int timestamp;
@@ -103,6 +102,8 @@ static int batch(const char *name)
 int
 main(int argc, char **argv)
 {
+       int color = default_color("BRIDGE_COLOR");
+
        while (argc > 1) {
                const char *opt = argv[1];
 
index 17ec56f3d7b4716ad085fb46a66aa13c66106480..8eea534f38e1d43392e13a190b0a21950daa3bd5 100644 (file)
@@ -20,6 +20,7 @@ enum color_opt {
        COLOR_OPT_ALWAYS = 2
 };
 
+int default_color(const char *argv0);
 bool check_enable_color(int color, int json);
 bool matches_color(const char *arg, int *val);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
diff --git a/ip/ip.c b/ip/ip.c
index 8c046ef1df1438b63c764c7b38b13f31f9d6c3dd..e15d5fe52d92532b3b9206fde6ac1eeb0b88d9d5 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -168,7 +168,7 @@ int main(int argc, char **argv)
        const char *libbpf_version;
        char *batch_file = NULL;
        char *basename;
-       int color = 0;
+       int color = default_color("IP_COLOR");
 
        /* to run vrf exec without root, capabilities might be set, drop them
         * if not needed as the first thing.
index 59976847295c5927816d525fefe6efdfbd4bc2de..9262fc51c1f2e8f4e46f1d75c123a79fc85e843b 100644 (file)
@@ -93,6 +93,32 @@ bool check_enable_color(int color, int json)
        return false;
 }
 
+static bool match_color_value(const char *arg, int *val)
+{
+       if (*arg == '\0' || !strcmp(arg, "always"))
+               *val = COLOR_OPT_ALWAYS;
+       else if (!strcmp(arg, "auto"))
+               *val = COLOR_OPT_AUTO;
+       else if (!strcmp(arg, "never"))
+               *val = COLOR_OPT_NEVER;
+       else
+               return false;
+       return true;
+}
+
+int default_color(const char *env)
+{
+       int val = COLOR_OPT_NEVER;
+       char *name;
+       size_t i;
+
+       name = getenv(env);
+       if (name && match_color_value(name, &val))
+               return val;
+
+       return COLOR_OPT_NEVER;
+}
+
 bool matches_color(const char *arg, int *val)
 {
        char *dup, *p;
@@ -108,15 +134,7 @@ bool matches_color(const char *arg, int *val)
        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;
+       return match_color_value(p, val);
 }
 
 static void set_color_palette(void)
index c52c9331e2c25dac50c35eb8ec1d8598951f0f68..58bb1ddbd26a858cac10b6081092e0bbc14a1982 100644 (file)
@@ -319,6 +319,13 @@ precedence. This flag is ignored if
 .B \-json
 is also given.
 
+
+The default color setting is
+.B never
+but can be overridden by the
+.B BRIDGE_COLOR
+environment variable.
+
 .TP
 .BR "\-j", " \-json"
 Output results in JavaScript Object Notation (JSON).
index 72227d44fd30a6b6e286e0aff58dcb7da0404218..df572f47d96d2f8b9c56eb0b3dd9af30d34ec39d 100644 (file)
@@ -197,11 +197,15 @@ precedence. This flag is ignored if
 .B \-json
 is also given.
 
-Used color palette can be influenced by
-.BR COLORFGBG
-environment variable
-(see
-.BR ENVIRONMENT ).
+The default color setting is
+.B never
+but can be overridden by the
+.B IP_COLOR
+environment variable.
+
+The color palette used can be adjusted with
+.B COLORFGBG
+environment variable.
 
 .TP
 .BR "\-t" , " \-timestamp"
index d436d46472af16f6908ce76b6d961c0e72bf4da3..39ac6dcd1631e938db6670c2e3e81ae682449277 100644 (file)
@@ -805,6 +805,12 @@ precedence. This flag is ignored if
 .B \-json
 is also given.
 
+The default color setting is
+.B never
+but can be overridden by the
+.B TC_COLOR
+environment variable.
+
 .TP
 .BR "\-j", " \-json"
 Display results in JSON format.
diff --git a/tc/tc.c b/tc/tc.c
index 258205004611c4a131b7370272d2cdf2c8ad42e2..b7cd60d68a382e9bf1f981cc0e5476cd822433ed 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -35,7 +35,6 @@ int use_iec;
 int force;
 bool use_names;
 int json;
-int color;
 int oneline;
 int brief;
 
@@ -254,6 +253,7 @@ int main(int argc, char **argv)
 {
        const char *libbpf_version;
        char *batch_file = NULL;
+       int color = default_color("TC_COLOR");
        int ret;
 
        while (argc > 1) {