Supported keywords are "error", "warning", "hint" and "success".

TODO:
 * make the coloring optional? What variable to use?
 * doc for the coloring option.
 * how to test?

Signed-off-by: Han-Wen Nienhuys <han...@google.com>
Change-Id: I090412a1288bc2caef0916447e28c2d0199da47d
---
 sideband.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 60 insertions(+), 9 deletions(-)

diff --git a/sideband.c b/sideband.c
index 325bf0e97..c8b7cb6dd 100644
--- a/sideband.c
+++ b/sideband.c
@@ -1,6 +1,53 @@
 #include "cache.h"
 #include "pkt-line.h"
 #include "sideband.h"
+#include "color.h"
+
+/*
+ * Optionally highlight some keywords in remote output if they appear at the
+ * start of the line.
+ */
+void emit_sideband(struct strbuf *dest, const char *src, int n) {
+        // NOSUBMIT - maybe use transport.color property?
+        int want_color = want_color_stderr(GIT_COLOR_AUTO);
+
+        if (!want_color) {
+                strbuf_add(dest, src, n);
+                return;
+        }
+
+        struct kwtable {
+                const char* keyword;
+                const char* color;
+        } keywords[] = {
+                {"hint", GIT_COLOR_YELLOW},
+                {"warning", GIT_COLOR_BOLD_YELLOW},
+                {"success", GIT_COLOR_BOLD_GREEN},
+                {"error", GIT_COLOR_BOLD_RED},
+                {},
+        };
+
+        while (isspace(*src)) {
+                strbuf_addch(dest, *src);
+                src++;
+                n--;
+        }
+
+        for (struct kwtable* p = keywords; p->keyword; p++) {
+                int len = strlen(p->keyword);
+                if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {
+                        strbuf_addstr(dest, p->color);
+                        strbuf_add(dest, src, len);
+                        strbuf_addstr(dest, GIT_COLOR_RESET);
+                        n -= len;
+                        src += len;
+                        break;
+                }
+        }
+
+        strbuf_add(dest, src, n);
+}
+
 
 /*
  * Receive multiplexed output stream over git native protocol.
@@ -48,8 +95,10 @@ int recv_sideband(const char *me, int in_stream, int out)
                len--;
                switch (band) {
                case 3:
-                       strbuf_addf(&outbuf, "%s%s%s", outbuf.len ? "\n" : "",
-                                   DISPLAY_PREFIX, buf + 1);
+                       strbuf_addf(&outbuf, "%s%s", outbuf.len ? "\n" : "",
+                                   DISPLAY_PREFIX);
+                        emit_sideband(&outbuf, buf+1, len);
+
                        retval = SIDEBAND_REMOTE_ERROR;
                        break;
                case 2:
@@ -69,20 +118,22 @@ int recv_sideband(const char *me, int in_stream, int out)
                                if (!outbuf.len)
                                        strbuf_addstr(&outbuf, DISPLAY_PREFIX);
                                if (linelen > 0) {
-                                       strbuf_addf(&outbuf, "%.*s%s%c",
-                                                   linelen, b, suffix, *brk);
-                               } else {
-                                       strbuf_addch(&outbuf, *brk);
+                                        emit_sideband(&outbuf, b, linelen);
+                                        strbuf_addstr(&outbuf, suffix);
                                }
+
+                                strbuf_addch(&outbuf, *brk);
                                xwrite(2, outbuf.buf, outbuf.len);
                                strbuf_reset(&outbuf);
 
                                b = brk + 1;
                        }
 
-                       if (*b)
-                               strbuf_addf(&outbuf, "%s%s", outbuf.len ?
-                                           "" : DISPLAY_PREFIX, b);
+                       if (*b) {
+                               strbuf_addstr(&outbuf, outbuf.len ?
+                                           "" : DISPLAY_PREFIX);
+                                emit_sideband(&outbuf, b, strlen(b));
+                        }
                        break;
                case 1:
                        write_or_die(out, buf + 1, len);
-- 
2.18.0.233.g985f88cf7e-goog

Reply via email to