]> granicus.if.org Git - procps-ng/commitdiff
Add comments and limit to standard colors
authorNipunn Koorapati <nipunn@dropbox.com>
Mon, 13 Jul 2020 00:59:29 +0000 (00:59 +0000)
committerCraig Small <csmall@dropbear.xyz>
Tue, 22 Dec 2020 03:10:11 +0000 (14:10 +1100)
watch.c

diff --git a/watch.c b/watch.c
index 4ec4324127132de5790c7ca46f0dfbfe0c4088b3..0430651a5b3a2503067bc93f5d717b5e1722b421 100644 (file)
--- a/watch.c
+++ b/watch.c
@@ -138,23 +138,34 @@ static void init_ansi_colors(void)
 }
 
 
-static int color_escape_sequence(char** escape_sequence) {
+static int process_ansi_color_escape_sequence(char** escape_sequence) {
+    // process SGR ANSI color escape sequence
+    // Eg 8-bit
+    // 38;5;⟨n⟩  (set fg color to n)
+    // 48;5;⟨n⟩  (set bg color to n)
+    //
+    // Eg 24-bit (not yet implemented)
+    // ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color
+    // ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
     int num;
 
     if ((*escape_sequence)[0] != ';')
         return 0; /* not understood */
 
     if ((*escape_sequence)[1] == '5') {
-        // 8 bit!
+        // 8 bit! ANSI specifies a predefined set of 256 colors here.
         if ((*escape_sequence)[2] != ';')
             return 0; /* not understood */
         num = strtol((*escape_sequence) + 3, escape_sequence, 10);
         if (num >= 0 && num <= 7) {
+            // 0-7 are standard colors  same as SGR 30-37
             return num + 1;
-        } else if (num >= 8 && num <= 15) {
-            // Bright intensity colors. Show them as normal for simplicty of impl
-            return num - 8 + 1;
         }
+
+        // Remainder aren't yet implemented
+        // 8- 15:  high intensity colors (as in ESC [ 90–97 m)
+        // 16-231:  6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
+        // 232-255:  grayscale from black to white in 24 steps
     }
 
     return 0; /* not understood */
@@ -209,8 +220,20 @@ static int set_ansi_attribute(const int attrib, char** escape_sequence)
        case 27:        /* unset inversed */
                attributes &= ~A_REVERSE;
                break;
-    case 39:
-        fg_col = 0;
+    case 38:
+        fg_col = process_ansi_color_escape_sequence(escape_sequence);
+        if (fg_col == 0) {
+            return 0; /* not understood */
+        }
+        break;
+       case 39:
+               fg_col = 0;
+               break;
+    case 48:
+        bg_col = process_ansi_color_escape_sequence(escape_sequence);
+        if (bg_col == 0) {
+            return 0; /* not understood */
+        }
         break;
     case 49:
         bg_col = 0;
@@ -550,7 +573,7 @@ static int run_command(char *restrict command, char **restrict command_argv)
        for (y = show_title; y < height; y++) {
                int eolseen = 0, tabpending = 0, tabwaspending = 0;
                if (flags & WATCH_COLOR)
-                       set_ansi_attribute(-1);
+                       set_ansi_attribute(-1, NULL);
 #ifdef WITH_WATCH8BIT
                wint_t carry = WEOF;
 #endif
@@ -563,7 +586,7 @@ static int run_command(char *restrict command, char **restrict command_argv)
                        int attr = 0;
 
                        if (tabwaspending && (flags & WATCH_COLOR))
-                               set_ansi_attribute(-1);
+                               set_ansi_attribute(-1, NULL);
                        tabwaspending = 0;
 
                        if (!eolseen) {