]> granicus.if.org Git - sysstat/commitdiff
Add support for color customization
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 2 Oct 2015 09:49:27 +0000 (11:49 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 2 Oct 2015 09:49:27 +0000 (11:49 +0200)
Add new S_COLORS_SGR environment variable to give the user the
possibility to customize the colors used to display statistics for
iostat, mpstat and sar commands.

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

index aae31dfc8fb9bb8bef57c2cf22b1038edb3fbfb0..1120eb0d31a80e27b7a07248a0a911a9ad02204d 100644 (file)
--- a/common.c
+++ b/common.c
@@ -51,14 +51,14 @@ unsigned int hz;
 unsigned int kb_shift;
 
 /* Colors strings */
-char sc_percent_high[] = C_BOLD_RED;
-char sc_percent_low[] = C_BOLD_BLUE;
-char sc_zero_int_stat[] = C_LIGHT_YELLOW;
-char sc_int_stat[] = C_BOLD_YELLOW;
-char sc_item_name[] = C_LIGHT_GREEN;
-char sc_sa_restart[] = C_LIGHT_RED;
-char sc_sa_comment[] = C_LIGHT_CYAN;
-char sc_normal[] = C_NORMAL;
+char sc_percent_high[MAX_SGR_LEN] = C_BOLD_RED;
+char sc_percent_low[MAX_SGR_LEN] = C_BOLD_BLUE;
+char sc_zero_int_stat[MAX_SGR_LEN] = C_LIGHT_YELLOW;
+char sc_int_stat[MAX_SGR_LEN] = C_BOLD_YELLOW;
+char sc_item_name[MAX_SGR_LEN] = C_LIGHT_GREEN;
+char sc_sa_restart[MAX_SGR_LEN] = C_LIGHT_RED;
+char sc_sa_comment[MAX_SGR_LEN] = C_LIGHT_CYAN;
+char sc_normal[MAX_SGR_LEN] = C_NORMAL;
 
 /* Type of persistent device names used in sar and iostat */
 char persistent_name_type[MAX_FILE_LEN];
@@ -938,29 +938,67 @@ char *get_pretty_name_from_persistent(char *persistent)
  */
 void init_colors(void)
 {
-       char *e;
-
-       /* Read environment variable value */
-       if (((e = getenv(ENV_COLORS)) != NULL) && !strcmp(e, C_ALWAYS))
-               /* Variable set to "always" */
-               return;
+       char *e, *p;
+       int len;
 
-       if (e && strcmp(e, C_NEVER) && isatty(STDOUT_FILENO))
+       /* Read S_COLORS environment variable */
+       if (((e = getenv(ENV_COLORS)) == NULL) ||
+           !strcmp(e, C_NEVER) ||
+           (strcmp(e, C_ALWAYS) && !isatty(STDOUT_FILENO))) {
                /*
-                * Variable set to "auto" (or any other value different
-                * from "never") and stdout is a terminal.
+                * Environment variable not set, or set to "never",
+                * or set to "auto" and stdout is not a terminal:
+                * 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, "");
+
+               return;
+       }
+
+       /* Read S_COLORS_SGR environment variable */
+       if ((e = getenv(ENV_COLORS_SGR)) == NULL)
+               /* Environment variable not set */
                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, "");
+       for (p = strtok(e, ":"); p; p =strtok(NULL, ":")) {
+
+               len = strlen(p);
+               if ((len > 7) || (len < 3) || (*(p + 1) != '=') ||
+                   (strspn(p + 2, ";0123456789") != (len - 2)))
+                       /* Ignore malformed codes */
+                       continue;
+
+               switch (*p) {
+                       case 'H':
+                               snprintf(sc_percent_high, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'M':
+                               snprintf(sc_percent_low, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'Z':
+                               snprintf(sc_zero_int_stat, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'N':
+                               snprintf(sc_int_stat, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'I':
+                               snprintf(sc_item_name, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'C':
+                               snprintf(sc_sa_comment, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case 'R':
+                               snprintf(sc_sa_restart, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+               }
+       }
 }
 
 /*
index aa282b6264bc25e2886db1a98082e42832487869..8af7f921cb03baa07b0e65d9e1c11d00f3ca157b 100644 (file)
--- a/common.h
+++ b/common.h
@@ -85,6 +85,7 @@
 #define ENV_TIME_FMT           "S_TIME_FORMAT"
 #define ENV_TIME_DEFTM         "S_TIME_DEF_TIME"
 #define ENV_COLORS             "S_COLORS"
+#define ENV_COLORS_SGR         "S_COLORS_SGR"
 
 #define C_NEVER                        "never"
 #define C_ALWAYS               "always"
@@ -177,6 +178,8 @@ extern char persistent_name_type[MAX_FILE_LEN];
 #define PERCENT_LIMIT_HIGH     75.0
 #define PERCENT_LIMIT_LOW      50.0
 
+#define MAX_SGR_LEN    16
+
 #define IS_INT         0
 #define IS_STR         1
 #define IS_RESTART     2