git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Jonathan Nieder <jrnieder@gmail.com>
Subject: [PATCH 1/3] for-each-ref: introduce %C(...) for color
Date: Fri, 27 Sep 2013 17:40:26 +0530	[thread overview]
Message-ID: <1380283828-25420-2-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1380283828-25420-1-git-send-email-artagnon@gmail.com>

Enhance 'git for-each-ref' with color formatting options.  You can now
use the following format in for-each-ref:

  %C(green)%(refname:short)%C(reset)

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 Documentation/git-for-each-ref.txt |  4 +++-
 builtin/for-each-ref.c             | 23 +++++++++++++++++++----
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index f2e08d1..078a116 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -45,7 +45,9 @@ OPTIONS
 	It also interpolates `%%` to `%`, and `%xx` where `xx`
 	are hex digits interpolates to character with hex code
 	`xx`; for example `%00` interpolates to `\0` (NUL),
-	`%09` to `\t` (TAB) and `%0a` to `\n` (LF).
+	`%09` to `\t` (TAB) and `%0a` to `\n` (LF). Additionally,
+	colors can be specified using `%C(colorname)`. Use
+	`%C(reset)` to reset the color.
 
 <pattern>...::
 	If one or more patterns are given, only refs are shown that
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 1d4083c..a1ca186 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -9,6 +9,7 @@
 #include "quote.h"
 #include "parse-options.h"
 #include "remote.h"
+#include "color.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -155,10 +156,13 @@ static const char *find_next(const char *cp)
 	while (*cp) {
 		if (*cp == '%') {
 			/*
+			 * %C( is the start of a color;
 			 * %( is the start of an atom;
 			 * %% is a quoted per-cent.
 			 */
-			if (cp[1] == '(')
+			if (cp[1] == 'C' && cp[2] == '(')
+				return cp;
+			else if (cp[1] == '(')
 				return cp;
 			else if (cp[1] == '%')
 				cp++; /* skip over two % */
@@ -180,8 +184,11 @@ static int verify_format(const char *format)
 		const char *ep = strchr(sp, ')');
 		if (!ep)
 			return error("malformed format string %s", sp);
-		/* sp points at "%(" and ep points at the closing ")" */
-		parse_atom(sp + 2, ep);
+		/* Ignore color specifications: %C(
+		 * sp points at "%(" and ep points at the closing ")"
+		 */
+		if (prefixcmp(sp, "%C("))
+			parse_atom(sp + 2, ep);
 		cp = ep + 1;
 	}
 	return 0;
@@ -933,12 +940,20 @@ static void emit(const char *cp, const char *ep)
 static void show_ref(struct refinfo *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
+	char color[COLOR_MAXLEN];
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		ep = strchr(sp, ')');
 		if (cp < sp)
 			emit(cp, sp);
-		print_value(info, parse_atom(sp + 2, ep), quote_style);
+
+		/* Do we have a color specification? */
+		if (!prefixcmp(sp, "%C("))
+			color_parse_mem(sp + 3, ep - sp - 3, "--format", color);
+		else {
+			printf("%s", color);
+			print_value(info, parse_atom(sp + 2, ep), quote_style);
+		}
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-- 
1.8.4.478.g55109e3

  reply	other threads:[~2013-09-27 12:17 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-27 12:10 [PATCH 0/3] Juggling between hot branches Ramkumar Ramachandra
2013-09-27 12:10 ` Ramkumar Ramachandra [this message]
2013-09-27 13:16   ` [PATCH 1/3] for-each-ref: introduce %C(...) for color Phil Hord
2013-09-27 12:10 ` [PATCH 2/3] for-each-ref: introduce %(HEAD) asterisk marker Ramkumar Ramachandra
2013-09-27 12:10 ` [PATCH 3/3] for-each-ref: introduce %(upstream:track[short]) Ramkumar Ramachandra
2013-09-27 14:03   ` Phil Hord
2013-09-27 14:27     ` Ramkumar Ramachandra
2013-09-27 14:25   ` Johannes Sixt
2013-09-27 14:33     ` Ramkumar Ramachandra
2013-09-27 22:18     ` Jonathan Nieder
2013-09-27 16:06   ` Philip Oakley
2013-09-27 16:10     ` Ramkumar Ramachandra
2013-09-27 17:07       ` Philip Oakley
  -- strict thread matches above, loose matches on Subject: below --
2013-10-31  9:46 [PATCH (resend) 0/3] Minor f-e-r enhacements Ramkumar Ramachandra
2013-10-31  9:46 ` [PATCH 1/3] for-each-ref: introduce %C(...) for color Ramkumar Ramachandra
2013-10-31 20:50   ` Junio C Hamano
2013-11-01  8:37     ` Ramkumar Ramachandra
2013-11-01 15:05       ` Junio C Hamano
2013-11-02  6:02         ` Ramkumar Ramachandra
2013-11-04 18:17           ` Junio C Hamano
2013-11-07  6:36             ` Ramkumar Ramachandra
2013-11-07 18:02               ` Junio C Hamano
2013-11-08 12:14                 ` Ramkumar Ramachandra
2013-11-08 17:30                   ` Junio C Hamano
2013-11-12  3:38                     ` Ramkumar Ramachandra
2013-05-24 14:19 [PATCH v2 0/3] Towards a useable git-branch Ramkumar Ramachandra
2013-05-24 14:19 ` [PATCH 1/3] for-each-ref: introduce %C(...) for color Ramkumar Ramachandra
2013-05-24 20:56   ` Antoine Pelisse
2013-05-25 11:50     ` Ramkumar Ramachandra
2013-05-25 12:20       ` John Keeping
2013-05-25 12:54         ` Ramkumar Ramachandra
2013-05-25 12:35       ` Antoine Pelisse
2013-05-24 23:41   ` David Aguilar
2013-05-25 11:51     ` Ramkumar Ramachandra
2013-05-25  6:29   ` Eric Sunshine

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=1380283828-25420-2-git-send-email-artagnon@gmail.com \
    --to=artagnon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    /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).