[PATCH 1/3] for-each-ref: introduce %C(...) for color
From: Ramkumar Ramachandra <hidden>
Date: 2016-06-15 22:57:26
Subsystem:
the rest · Maintainer:
Linus Torvalds
Since 'git branch' misses important options like --sort, --count, and --format that are present in 'git for-each-ref'. Until we are in a position to fix 'git branch', let us enhance the 'git for-each-ref' format so it can atleast colorize output. You can use the following format in for-each-ref: %C(green)%(refname:short)%C(reset) To display refs in green. Future patches will attempt to extend the format even more to get useful output. Signed-off-by: Ramkumar Ramachandra <redacted> --- builtin/for-each-ref.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 7f059c3..1563b25 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,12 @@ 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;
@@ -928,12 +936,22 @@ 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.3.rc3.2.g99b8f3f.dirty