allow overriding color option in environment
Subsystems (3)
PATCH 1/1 allow overriding color option in environment
For ip, tc, and bridge command introduce a new way to enable automatic colorization via environment variable. The default is what ever is in the config.
Example:
$ IP_COLOR=auto ip -br show addr
The idea is that distributions can ship with the same default as before "none" but that users can override if needed without resorting to aliasing every command.
--- bridge/bridge.c | 2 +- include/color.h | 1 + ip/ip.c | 2 +- lib/color.c | 36 +++++++++++++++++++++++++++--------- man/man8/bridge.8 | 7 +++++++ man/man8/ip.8 | 14 +++++++++----- man/man8/tc.8 | 6 ++++++ tc/tc.c | 2 +- 8 files changed, 53 insertions(+), 17 deletions(-)
bridge/bridge.c +1 -1
@@ -102,7 +102,7 @@ static int batch(const char *name)int main (int argc,char **argv) { -int color =CONF_COLOR ; +int color =default_color ("BRIDGE_COLOR" );while (argc >1 ) {const char *opt = argv[1 ];
include/color.h +1 -0
@@ -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, ...);
ip/ip.c +1 -1
@@ -168,7 +168,7 @@ int main(int argc, char **argv)const char *libbpf_version;char *batch_file =NULL ;char *basename; -int color =CONF_COLOR ; +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.
lib/color.c +27 -9
@@ -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) +{ +char *name; +int val; +size_t i; + + name =getenv (env); +if (name &&match_color_value (name, &val)) +return val; + +return CONF_COLOR ; +} +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 )
man/man8/bridge.8 +7 -0
@@ -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).
man/man8/ip.8 +9 -5
@@ -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"
man/man8/tc.8 +6 -0
@@ -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.
tc/tc.c +1 -1
@@ -253,7 +253,7 @@ int main(int argc, char **argv) {const char *libbpf_version;char *batch_file =NULL ; -int color =CONF_COLOR ; +int color =default_color ("TC_COLOR" );int ret;while (argc >1 ) {
Signature · 1 line
2.39.2
Quoted 11 lines · Stephen Hemminger
For ip, tc, and bridge command introduce a new way to enable automatic colorization via environment variable. The default is what ever is in the config.
Example:
$ IP_COLOR=auto ip -br show addrThe idea is that distributions can ship with the same default as before "none" but that users can override if needed without resorting to aliasing every command.
I'm OK with this, but can we please use a single environment variable to control color output for all iproute tools? I can't see why anyone would want to have color output on by default for ip and not for tc and bridge...
Quoted 55 lines
Signed-off-by Stephen Hemminger <[email protected]>--- bridge/bridge.c | 2 +- include/color.h | 1 + ip/ip.c | 2 +- lib/color.c | 36 +++++++++++++++++++++++++++--------- man/man8/bridge.8 | 7 +++++++ man/man8/ip.8 | 14 +++++++++----- man/man8/tc.8 | 6 ++++++ tc/tc.c | 2 +- 8 files changed, 53 insertions(+), 17 deletions(-)bridge/bridge.c +1
-1@@ -102,7 +102,7 @@ static int batch(const char *name)int main (int argc,char **argv) { -int color =CONF_COLOR ; +int color =default_color ("BRIDGE_COLOR" );while (argc >1 ) {const char *opt = argv[1 ];include/color.h +1
-0@@ -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, ...);ip/ip.c +1
-1@@ -168,7 +168,7 @@ int main(int argc, char **argv)const char *libbpf_version;char *batch_file =NULL ;char *basename; -int color =CONF_COLOR ; +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. lib/color.c +0
-0
lib/color.c bool check_enable_color(int color, int json) · line 98
Quoted 6 lines
lib/color.c +3
-0@@ -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" ))
This actually produces an unexpected result.
IP_COLOR= ip address
results in colorized output, while it should be colorless, in my opinion. However this works ok when this code path is exercised by the '--color' option, as with:
ip -c address
users actually expect to have color on the output.
Quoted 6 lines
lib/color.c +6
-0+ *val =COLOR_OPT_ALWAYS ; + else if (!strcmp(arg, "auto")) + *val =COLOR_OPT_AUTO ; + else if (!strcmp(arg, "never")) + *val =COLOR_OPT_NEVER ; + else
lib/color.c bool check_enable_color(int color, int json) · line 114
Quoted 10 lines
lib/color.c +10
-0+return false; +return true; +} + +int default_color (const char *env) +{ +char *name; +int val; +size_t i; +
You may want to get rid of the i var above:
color.c: In function ‘default_color’:
color.c:113:16: warning: unused variable ‘i’ [-Wunused-variable]
113 | size_t i;
| ^
Quoted 103 lines
lib/color.c +8
-9+ name =getenv (env); +if (name &&match_color_value (name, &val)) +return val; + +return CONF_COLOR ; +} +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 )man/man8/bridge.8 +7
-0@@ -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).man/man8/ip.8 +9
-5@@ -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"man/man8/tc.8 +6
-0@@ -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.tc/tc.c +1
-1@@ -253,7 +253,7 @@ int main(int argc, char **argv) {const char *libbpf_version;char *batch_file =NULL ; -int color =CONF_COLOR ; +int color =default_color ("TC_COLOR" );int ret;while (argc >1 ) {Signature · 1 line
2.39.2