@@ -102,7 +102,7 @@ static int batch(const char *name)
int
main(int argc, char **argv)
{
- int color = CONF_COLOR;
+ int color = default_color();
while (argc > 1) {
const char *opt = argv[1];
@@ -20,6 +20,7 @@ enum color_opt {
COLOR_OPT_ALWAYS = 2
};
+int default_color(void);
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, ...);
@@ -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();
/* to run vrf exec without root, capabilities might be set, drop them
* if not needed as the first thing.
@@ -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(void)
+{
+ const char *name;
+ int val;
+
+ name = getenv("IPROUTE_COLORS");
+ if (name && *name && match_color_value(name, &val))
+ return val;
+
+ /* default is from config */
+ 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)
@@ -315,10 +315,14 @@ color output is enabled regardless of stdout state. If parameter is
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
+precedence.
+The default color setting is
+.B never
+but can be overridden by the
+.B IPROUTE_COLORS
+environment variable. This flag is ignored if
.B \-json
is also given.
-
.TP
.BR "\-j", " \-json"
Output results in JavaScript Object Notation (JSON).
@@ -193,15 +193,17 @@ 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
+precedence.The default color setting is
+.B never
+but can be overridden by the
+.B IPROUTE_COLORS
+environment variable. 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 color palette used can be adjusted with
+.B COLORFGBG
+environment variable.
.TP
.BR "\-t" , " \-timestamp"
@@ -801,10 +801,14 @@ color output is enabled regardless of stdout state. If parameter is
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
+precedence.
+The default color setting is
+.B never
+but can be overridden by the
+.B IPROUTE_COLORS
+environment variable. This flag is ignored if
.B \-json
is also given.
-
.TP
.BR "\-j", " \-json"
Display results in JSON format.
@@ -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();
int ret;
while (argc > 1) {
For ip, tc, and bridge command introduce IPROUTE_COLORS to enable automatic colorization via environment variable. Similar to how grep handles color flag. Example: $ IPROUTE_COLORS=auto ip -br addr Signed-off-by: Stephen Hemminger <stephen@networkplumber.org> --- bridge/bridge.c | 2 +- include/color.h | 1 + ip/ip.c | 2 +- lib/color.c | 36 +++++++++++++++++++++++++++--------- man/man8/bridge.8 | 8 ++++++-- man/man8/ip.8 | 14 ++++++++------ man/man8/tc.8 | 8 ++++++-- tc/tc.c | 2 +- 8 files changed, 51 insertions(+), 22 deletions(-)