git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] for-each-ref: introduce %C(...) for color

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 31, 2013, 20:50 UTC
Message-ID
<xmqq38nh411p.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1383212774-5232-2-git-send-email-artagnon@gmail.com>
Ramkumar Ramachandra <artagnon@gmail.com> writes:
> 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)

So far, every magic for-each-ref takes were of form %(...); was there a reason why this had to be %C(...), not %(color=blah), or something more in-line with the existing other magic?

Show 85 quoted lines
>
> 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..6fa4464 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(...)`, with names
> +	described in color.branch.*.
>  
>  <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..6da2903 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);
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 3 of 15 in “Minor f-e-r enhacements”
  1. 0/3 Minor f-e-r enhacementsRamkumar Ramachandra, Oct 31, 2013
  2. 1/3 for-each-ref: introduce %C(...) for colorRamkumar Ramachandra, Oct 31, 2013
  3. Junio C HamanoOct 31, 2013
  4. Ramkumar RamachandraNov 1, 2013
  5. Junio C HamanoNov 1, 2013
  6. Ramkumar RamachandraNov 2, 2013
  7. Junio C HamanoNov 4, 2013
  8. Ramkumar RamachandraNov 7, 2013
  9. Junio C HamanoNov 7, 2013
  10. Ramkumar RamachandraNov 8, 2013
  11. Junio C HamanoNov 8, 2013
  12. Ramkumar RamachandraNov 12, 2013
  13. 2/3 for-each-ref: introduce %(HEAD) asterisk markerRamkumar Ramachandra, Oct 31, 2013
  14. 3/3 for-each-ref: introduce %(upstream:track[short])Ramkumar Ramachandra, Oct 31, 2013
  15. Junio C HamanoNov 1, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.