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

[PATCH v2 09/12] pretty: add %C(auto) for auto-coloring on the next placeholder

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 30, 2013, 09:35 UTC
Message-ID
<1364636112-15065-10-git-send-email-pclouds@gmail.com>
In-Reply-To
<1364636112-15065-1-git-send-email-pclouds@gmail.com>

This is not simply convenient over %C(auto,xxx). Some placeholders (actually only one, %d) do multi coloring and we can't emit a multiple colors with %C(auto,xxx).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/pretty-formats.txt |  3 ++-
 pretty.c                         | 15 +++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 66345d1..8734224 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -154,7 +154,8 @@ The placeholders are:
   adding `auto,` at the beginning will emit color only when colors are
   enabled for log output (by `color.diff`, `color.ui`, or `--color`, and
   respecting the `auto` settings of the former if we are going to a
-  terminal)
+  terminal). `auto` alone (i.e. `%C(auto)`) will turn on auto coloring
+  on the following placeholder.
 - '%m': left, right or boundary mark
 - '%n': newline
 - '%%': a raw '%'
diff --git a/pretty.c b/pretty.c
index 3f4809a..c333fd6 100644
--- a/pretty.c
+++ b/pretty.c
@@ -774,6 +774,7 @@ struct format_commit_context {
 	char *message;
 	char *commit_encoding;
 	size_t width, indent1, indent2;
+	int auto_color;
 
 	/* These offsets are relative to the start of the commit message. */
 	struct chunk author;
@@ -1011,7 +1012,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	const struct commit *commit = c->commit;
 	const char *msg = c->message;
 	struct commit_list *p;
-	int h1, h2;
+	int h1, h2, use_color;
 
 	/* these are independent of the commit */
 	switch (placeholder[0]) {
@@ -1023,6 +1024,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 
 			if (!end)
 				return 0;
+			if (!prefixcmp(begin, "auto)")) {
+				c->auto_color = 1;
+				return end - placeholder + 1;
+			}
 			if (!prefixcmp(begin, "auto,")) {
 				if (!want_color(c->pretty_ctx->color))
 					return end - placeholder + 1;
@@ -1090,16 +1095,22 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	/* these depend on the commit */
 	if (!commit->object.parsed)
 		parse_object(commit->object.sha1);
+	use_color = c->auto_color;
+	c->auto_color = 0;
 
 	switch (placeholder[0]) {
 	case 'H':		/* commit hash */
+		strbuf_addstr(sb, diff_get_color(use_color, DIFF_COMMIT));
 		strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
+		strbuf_addstr(sb, diff_get_color(use_color, DIFF_RESET));
 		return 1;
 	case 'h':		/* abbreviated commit hash */
+		strbuf_addstr(sb, diff_get_color(use_color, DIFF_COMMIT));
 		if (add_again(sb, &c->abbrev_commit_hash))
 			return 1;
 		strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
 						     c->pretty_ctx->abbrev));
+		strbuf_addstr(sb, diff_get_color(use_color, DIFF_RESET));
 		c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
 		return 1;
 	case 'T':		/* tree hash */
@@ -1136,7 +1147,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		strbuf_addstr(sb, get_revision_mark(NULL, commit));
 		return 1;
 	case 'd':
-		format_decoration(sb, commit, 0);
+		format_decoration(sb, commit, use_color);
 		return 1;
 	case 'g':		/* reflog info */
 		switch(placeholder[1]) {
-- 
1.8.2.83.gc99314b
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 37 of 83 in “Layout control placeholders for pretty format”
  1. 00/12 Layout control placeholders for pretty formatNguyễn Thái Ngọc Duy, Mar 16, 2013
  2. 01/12 pretty-formats.txt: wrap long linesNguyễn Thái Ngọc Duy, Mar 16, 2013
  3. 02/12 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Mar 16, 2013
  4. 03/12 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Mar 16, 2013
  5. 04/12 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Mar 16, 2013
  6. 05/12 pretty: save commit encoding from logmsg_reencode if the caller needs itNguyễn Thái Ngọc Duy, Mar 16, 2013
  7. Eric SunshineMar 17, 2013
  8. 06/12 pretty: get the correct encoding for --pretty:format=%eNguyễn Thái Ngọc Duy, Mar 16, 2013
  9. 07/12 utf8: keep NULs in reencode_string()Nguyễn Thái Ngọc Duy, Mar 16, 2013
  10. 08/12 pretty: two phase conversion for non utf-8 commitsNguyễn Thái Ngọc Duy, Mar 16, 2013
  11. 09/12 pretty: add %C(auto) for auto-coloring on the next placeholderNguyễn Thái Ngọc Duy, Mar 16, 2013
  12. Eric SunshineMar 17, 2013
  13. 10/12 pretty: support padding placeholders, %< %> and %><Nguyễn Thái Ngọc Duy, Mar 16, 2013
  14. Eric SunshineMar 17, 2013
  15. 11/12 pretty: support truncating in %>, %< and %><Nguyễn Thái Ngọc Duy, Mar 16, 2013
  16. Paul CampbellMar 16, 2013
  17. 12/12 pretty: support %>> that steal trailing spacesNguyễn Thái Ngọc Duy, Mar 16, 2013
  18. Eric SunshineMar 17, 2013
  19. Duy NguyenMar 30, 2013
  20. 00/12 Layout control placeholders for pretty formatNguyễn Thái Ngọc Duy, Mar 30, 2013
  21. 01/12 pretty-formats.txt: wrap long linesNguyễn Thái Ngọc Duy, Mar 30, 2013
  22. 02/12 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Mar 30, 2013
  23. Junio C HamanoApr 1, 2013
  24. Jakub NarębskiApr 5, 2013
  25. Duy NguyenApr 12, 2013
  26. Duy NguyenApr 12, 2013
  27. 03/12 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Mar 30, 2013
  28. 04/12 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Mar 30, 2013
  29. Junio C HamanoApr 1, 2013
  30. 05/12 pretty: save commit encoding from logmsg_reencode if the caller needs itNguyễn Thái Ngọc Duy, Mar 30, 2013
  31. Junio C HamanoApr 1, 2013
  32. 06/12 pretty: get the correct encoding for --pretty:format=%eNguyễn Thái Ngọc Duy, Mar 30, 2013
  33. 07/12 utf8: keep NULs in reencode_string()Nguyễn Thái Ngọc Duy, Mar 30, 2013
  34. Torsten BögershausenMar 30, 2013
  35. Duy NguyenMar 31, 2013
  36. 08/12 pretty: two phase conversion for non utf-8 commitsNguyễn Thái Ngọc Duy, Mar 30, 2013
  37. 09/12 pretty: add %C(auto) for auto-coloring on the next placeholderNguyễn Thái Ngọc Duy, Mar 30, 2013
  38. Junio C HamanoApr 1, 2013
  39. Duy NguyenApr 5, 2013
  40. Junio C HamanoApr 5, 2013
  41. Duy NguyenApr 15, 2013
  42. 10/12 pretty: support padding placeholders, %< %> and %><Nguyễn Thái Ngọc Duy, Mar 30, 2013
  43. 11/12 pretty: support truncating in %>, %< and %><Nguyễn Thái Ngọc Duy, Mar 30, 2013
  44. 12/12 pretty: support %>> that steal trailing spacesNguyễn Thái Ngọc Duy, Mar 30, 2013
  45. Junio C HamanoApr 1, 2013
  46. 00/13 nd/pretty-formatsNguyễn Thái Ngọc Duy, Apr 16, 2013
  47. 01/13 pretty: save commit encoding from logmsg_reencode if the caller needs itNguyễn Thái Ngọc Duy, Apr 16, 2013
  48. 02/13 pretty: get the correct encoding for --pretty:format=%eNguyễn Thái Ngọc Duy, Apr 16, 2013
  49. 03/13 pretty-formats.txt: wrap long linesNguyễn Thái Ngọc Duy, Apr 16, 2013
  50. 04/13 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Apr 16, 2013
  51. 05/13 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Apr 16, 2013
  52. 06/13 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Apr 16, 2013
  53. 07/13 utf8.c: add reencode_string_len() that can handle NULs in stringNguyễn Thái Ngọc Duy, Apr 16, 2013
  54. Duy NguyenApr 16, 2013
  55. Junio C HamanoApr 18, 2013
  56. 08/13 pretty: two phase conversion for non utf-8 commitsNguyễn Thái Ngọc Duy, Apr 16, 2013
  57. 09/13 pretty: split color parsing into a separate functionNguyễn Thái Ngọc Duy, Apr 16, 2013
  58. 10/13 pretty: add %C(auto) for auto-coloringNguyễn Thái Ngọc Duy, Apr 16, 2013
  59. Junio C HamanoApr 16, 2013
  60. Duy NguyenApr 17, 2013
  61. Junio C HamanoApr 17, 2013
  62. Junio C HamanoApr 16, 2013
  63. 11/13 pretty: support padding placeholders, %< %> and %><Nguyễn Thái Ngọc Duy, Apr 16, 2013
  64. Junio C HamanoApr 16, 2013
  65. Junio C HamanoApr 16, 2013
  66. Duy NguyenApr 17, 2013
  67. 12/13 pretty: support truncating in %>, %< and %><Nguyễn Thái Ngọc Duy, Apr 16, 2013
  68. 13/13 pretty: support %>> that steal trailing spacesNguyễn Thái Ngọc Duy, Apr 16, 2013
  69. 00/13 nd/pretty-formatsNguyễn Thái Ngọc Duy, Apr 18, 2013
  70. 01/13 pretty: save commit encoding from logmsg_reencode if the caller needs itNguyễn Thái Ngọc Duy, Apr 18, 2013
  71. 02/13 pretty: get the correct encoding for --pretty:format=%eNguyễn Thái Ngọc Duy, Apr 18, 2013
  72. 03/13 pretty-formats.txt: wrap long linesNguyễn Thái Ngọc Duy, Apr 18, 2013
  73. 04/13 pretty: share code between format_decoration and show_decorationsNguyễn Thái Ngọc Duy, Apr 18, 2013
  74. 05/13 utf8.c: move display_mode_esc_sequence_len() for use by other functionsNguyễn Thái Ngọc Duy, Apr 18, 2013
  75. 06/13 utf8.c: add utf8_strnwidth() with the ability to skip ansi sequencesNguyễn Thái Ngọc Duy, Apr 18, 2013
  76. 07/13 utf8.c: add reencode_string_len() that can handle NULs in stringNguyễn Thái Ngọc Duy, Apr 18, 2013
  77. 08/13 pretty: two phase conversion for non utf-8 commitsNguyễn Thái Ngọc Duy, Apr 18, 2013
  78. 09/13 pretty: split color parsing into a separate functionNguyễn Thái Ngọc Duy, Apr 18, 2013
  79. 10/13 pretty: add %C(auto) for auto-coloringNguyễn Thái Ngọc Duy, Apr 18, 2013
  80. 11/13 pretty: support padding placeholders, %< %> and %><Nguyễn Thái Ngọc Duy, Apr 18, 2013
  81. 12/13 pretty: support truncating in %>, %< and %><Nguyễn Thái Ngọc Duy, Apr 18, 2013
  82. 13/13 pretty: support %>> that steal trailing spacesNguyễn Thái Ngọc Duy, Apr 18, 2013
  83. Torsten BögershausenApr 16, 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.