]> granicus.if.org Git - sysstat/commitdiff
Define S_COLORS possible values
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 27 Sep 2015 13:54:10 +0000 (15:54 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sun, 27 Sep 2015 13:54:10 +0000 (15:54 +0200)
Sysstat now takes into account the following values for S_COLOR
environment variable:
"never": Don't use colorization
"always": Always use colorization
"auto" (or any other value different from "never" and "always"): Use
colorization only if stdout is a terminal.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
common.c
common.h

index 81e9dab21143197b034bec0f4b82183191541d25..aae31dfc8fb9bb8bef57c2cf22b1038edb3fbfb0 100644 (file)
--- a/common.c
+++ b/common.c
@@ -938,21 +938,29 @@ char *get_pretty_name_from_persistent(char *persistent)
  */
 void init_colors(void)
 {
+       char *e;
+
        /* Read environment variable value */
-       if (!getenv(ENV_COLORS) || !isatty(STDOUT_FILENO)) {
+       if (((e = getenv(ENV_COLORS)) != NULL) && !strcmp(e, C_ALWAYS))
+               /* Variable set to "always" */
+               return;
+
+       if (e && strcmp(e, C_NEVER) && isatty(STDOUT_FILENO))
                /*
-                * Environment variable is not set or stdout is not a terminal:
-                * Unset color strings.
+                * Variable set to "auto" (or any other value different
+                * from "never") and stdout is a terminal.
                 */
-               strcpy(sc_percent_high, "");
-               strcpy(sc_percent_low, "");
-               strcpy(sc_zero_int_stat, "");
-               strcpy(sc_int_stat, "");
-               strcpy(sc_item_name, "");
-               strcpy(sc_sa_comment, "");
-               strcpy(sc_sa_restart, "");
-               strcpy(sc_normal, "");
-       }
+               return;
+
+       /* Other cases: Unset color strings */
+       strcpy(sc_percent_high, "");
+       strcpy(sc_percent_low, "");
+       strcpy(sc_zero_int_stat, "");
+       strcpy(sc_int_stat, "");
+       strcpy(sc_item_name, "");
+       strcpy(sc_sa_comment, "");
+       strcpy(sc_sa_restart, "");
+       strcpy(sc_normal, "");
 }
 
 /*
index b49b622078994e2bb9b41305caec66f975eaaaf6..aa282b6264bc25e2886db1a98082e42832487869 100644 (file)
--- a/common.h
+++ b/common.h
@@ -86,6 +86,9 @@
 #define ENV_TIME_DEFTM         "S_TIME_DEF_TIME"
 #define ENV_COLORS             "S_COLORS"
 
+#define C_NEVER                        "never"
+#define C_ALWAYS               "always"
+
 #define DIGITS                 "0123456789"
 
 /*