git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Han-Wen Nienhuys <hanwen@google.com>
To: git@vger.kernel.org
Cc: Han-Wen Nienhuys <hanwen@google.com>
Subject: [PATCH] RFC Highlight keywords in remote sideband output.
Date: Thu, 26 Jul 2018 19:17:51 +0200	[thread overview]
Message-ID: <20180726171751.178616-1-hanwen@google.com> (raw)

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 <hanwen@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	other threads:[~2018-07-26 17:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-26 17:17 Han-Wen Nienhuys [this message]
2018-07-26 17:53 ` [PATCH] RFC Highlight keywords in remote sideband output Stefan Beller
2018-07-26 18:50 ` Junio C Hamano
2018-07-30 12:24   ` Han-Wen Nienhuys

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180726171751.178616-1-hanwen@google.com \
    --to=hanwen@google.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).