git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Koppe <andy.koppe@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, glencbz@gmail.com, phillip.wood123@gmail.com,
	Andy Koppe <andy.koppe@gmail.com>
Subject: [PATCH v4 3/8] decorate: refactor format_decorations()
Date: Sun, 20 Aug 2023 09:53:31 +0100	[thread overview]
Message-ID: <20230820085336.8615-4-andy.koppe@gmail.com> (raw)
In-Reply-To: <20230820085336.8615-1-andy.koppe@gmail.com>

Rename the format_decorations_extended function to format_decorations
and drop the format_decorations wrapper macro. Pass the prefix, suffix
and separator strings as a single 'struct format_decorations' pointer
argument instead of separate arguments. Use default values defined in
the function when either the struct pointer or any of the struct fields
are NULL. This is to ease extension with additional options.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
---
 log-tree.c | 23 +++++++++++++++++------
 log-tree.h | 15 ++++++++-------
 pretty.c   |  6 ++++--
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/log-tree.c b/log-tree.c
index 208c69cbb7..cd12c26c29 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -303,14 +303,12 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
 
 /*
  * The caller makes sure there is no funny color before calling.
- * format_decorations_extended makes sure the same after return.
+ * format_decorations ensures the same after return.
  */
-void format_decorations_extended(struct strbuf *sb,
+void format_decorations(struct strbuf *sb,
 			const struct commit *commit,
 			int use_color,
-			const char *prefix,
-			const char *separator,
-			const char *suffix)
+			const struct decoration_options *opts)
 {
 	const struct name_decoration *decoration;
 	const struct name_decoration *current_and_HEAD;
@@ -319,10 +317,23 @@ void format_decorations_extended(struct strbuf *sb,
 	const char *color_reset =
 		decorate_get_color(use_color, DECORATION_NONE);
 
+	const char *prefix = " (";
+	const char *suffix = ")";
+	const char *separator = ", ";
+
 	decoration = get_name_decoration(&commit->object);
 	if (!decoration)
 		return;
 
+	if (opts) {
+		if (opts->prefix)
+			prefix = opts->prefix;
+		if (opts->suffix)
+			suffix = opts->suffix;
+		if (opts->separator)
+			separator = opts->separator;
+	}
+
 	current_and_HEAD = current_pointed_by_HEAD(decoration);
 	while (decoration) {
 		/*
@@ -370,7 +381,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 	}
 	if (!opt->show_decorations)
 		return;
-	format_decorations(&sb, commit, opt->diffopt.use_color);
+	format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
 	fputs(sb.buf, opt->diffopt.file);
 	strbuf_release(&sb);
 }
diff --git a/log-tree.h b/log-tree.h
index bdb6432815..14898de8ac 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -13,17 +13,18 @@ struct decoration_filter {
 	struct string_list *exclude_ref_config_pattern;
 };
 
+struct decoration_options {
+	char *prefix;
+	char *suffix;
+	char *separator;
+};
+
 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
 int log_tree_diff_flush(struct rev_info *);
 int log_tree_commit(struct rev_info *, struct commit *);
 void show_log(struct rev_info *opt);
-void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
-			     int use_color,
-			     const char *prefix,
-			     const char *separator,
-			     const char *suffix);
-#define format_decorations(strbuf, commit, color) \
-			     format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
+void format_decorations(struct strbuf *sb, const struct commit *commit,
+			int use_color, const struct decoration_options *opts);
 void show_decorations(struct rev_info *opt, struct commit *commit);
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 			     const char **extra_headers_p,
diff --git a/pretty.c b/pretty.c
index 718530bbab..24fb82a5a2 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1537,10 +1537,12 @@ 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_decorations(sb, commit, c->auto_color);
+		format_decorations(sb, commit, c->auto_color, NULL);
 		return 1;
 	case 'D':
-		format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
+		format_decorations(sb, commit, c->auto_color,
+			&(struct decoration_options){.prefix = "",
+						     .suffix = ""});
 		return 1;
 	case 'S':		/* tag/branch like --source */
 		if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
-- 
2.42.0-rc2


  parent reply	other threads:[~2023-08-20  9:00 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-15 10:37 [PATCH] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-07-15 16:07 ` [PATCH v2] " Andy Koppe
2023-07-17 23:10   ` Junio C Hamano
2023-07-18  1:05     ` Junio C Hamano
2023-08-11 18:50       ` Andy Koppe
2023-07-19 18:16   ` Glen Choo
2023-07-23 16:25     ` Phillip Wood
2023-08-11 19:04       ` Andy Koppe
2023-08-11 20:38         ` Junio C Hamano
2023-08-11 22:06           ` Andy Koppe
2023-08-12  1:16             ` Junio C Hamano
2023-08-11 18:59     ` Andy Koppe
2023-08-15 18:13       ` Junio C Hamano
2023-08-15 18:28         ` Andy Koppe
2023-08-15 19:01           ` Junio C Hamano
2023-08-15 19:29             ` main != master at github.com/git/git Andy Koppe
2023-08-15 22:16               ` Taylor Blau
2023-08-16  2:24                 ` Jeff King
2023-08-16 13:30                   ` rsbecker
2023-08-18  0:35                     ` Junio C Hamano
2023-08-21 14:56                       ` Johannes Schindelin
2023-08-21 16:17                         ` Junio C Hamano
2023-08-22  0:31                           ` [PATCH] ci: avoid building from the same commit in parallel Junio C Hamano
2023-08-22  4:36                             ` Junio C Hamano
2023-08-22  4:48                               ` Johannes Schindelin
2023-08-22 15:31                                 ` Junio C Hamano
2023-08-23  8:42                                   ` Johannes Schindelin
2023-08-23 16:08                                     ` Junio C Hamano
2023-08-23 16:10                                     ` Junio C Hamano
2023-08-25 12:56                                       ` Johannes Schindelin
2023-08-10 21:16   ` [PATCH v3 1/7] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-10 21:16     ` [PATCH v3 2/7] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-10 21:16     ` [PATCH v3 3/7] decorate: refactor format_decorations() Andy Koppe
2023-08-10 21:16     ` [PATCH v3 4/7] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-10 21:16     ` [PATCH v3 5/7] decorate: color each token separately Andy Koppe
2023-08-10 21:16     ` [PATCH v3 6/7] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-10 21:16     ` [PATCH v3 7/7] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-16  4:23     ` [PATCH v3 1/7] pretty-formats: define "literal formatting code" Junio C Hamano
2023-08-20  8:53     ` [PATCH v4 0/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20  8:53       ` [PATCH v4 1/8] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-20  8:53       ` [PATCH v4 2/8] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-20  8:53       ` Andy Koppe [this message]
2023-08-20  8:53       ` [PATCH v4 4/8] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-20  8:53       ` [PATCH v4 5/8] decorate: color each token separately Andy Koppe
2023-08-20  8:53       ` [PATCH v4 6/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20  8:53       ` [PATCH v4 7/8] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-20  8:53       ` [PATCH v4 8/8] decorate: use commit color for HEAD arrow Andy Koppe
2023-08-20 18:50       ` [PATCH v5 0/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20 18:50         ` [PATCH v5 1/8] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-20 18:50         ` [PATCH v5 2/8] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-20 18:50         ` [PATCH v5 3/8] decorate: refactor format_decorations() Andy Koppe
2023-08-20 18:50         ` [PATCH v5 4/8] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-20 18:50         ` [PATCH v5 5/8] decorate: color each token separately Andy Koppe
2023-08-20 18:50         ` [PATCH v5 6/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20 18:50         ` [PATCH v5 7/8] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-20 18:50         ` [PATCH v5 8/8] decorate: use commit color for HEAD arrow Andy Koppe
2023-08-29 21:59         ` [PATCH v5 0/8] pretty: add %(decorate[:<options>]) format Junio C Hamano
2023-09-01 21:33           ` Andy Koppe
2023-08-21 19:01       ` [PATCH v4 " Junio C Hamano

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

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

  git send-email \
    --in-reply-to=20230820085336.8615-4-andy.koppe@gmail.com \
    --to=andy.koppe@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=glencbz@gmail.com \
    --cc=phillip.wood123@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.
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 NNTP newsgroup(s).