git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Hamza Mahfooz <someguy@effective-light.com>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
	Eric Sunshine <sunshine@sunshineco.com>
Subject: Re: [PATCH v8 2/2] pretty: colorize pattern matches in commit messages
Date: Mon, 27 Sep 2021 17:03:34 -0700	[thread overview]
Message-ID: <xmqqilylmuqx.fsf@gitster.g> (raw)
In-Reply-To: 20210924155000.1259649-2-someguy@effective-light.com

Hamza Mahfooz <someguy@effective-light.com> writes:

> +static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
> +				   const char *line, size_t linelen,
> +				   int color, enum grep_context ctx,
> +				   enum grep_header_field field)
> +{
> +	const char *buf, *eol, *line_color, *match_color;
> +	regmatch_t match;
> +	int eflags = 0;
> +
> +	buf = line;
> +	eol = buf + linelen;
> +
> +	if (!opt || !want_color(color) || opt->invert)
> +		goto end;
> +
> +	line_color = opt->colors[GREP_COLOR_SELECTED];
> +	match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
> +
> +	while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {

So, this expects <buf, eol> to be only payload without "author " or
timestamp, ...

>  	} else {
> -		strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
> -			    (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, "    ",
> -			    (int)namelen, namebuf, (int)maillen, mailbuf);
> +		struct strbuf id = STRBUF_INIT;
> +		enum grep_header_field field = GREP_HEADER_FIELD_MAX;
> +		struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
> +
> +		if (!strcmp(what, "Author"))
> +			field = GREP_HEADER_AUTHOR;
> +		else if (!strcmp(what, "Commit"))
> +			field = GREP_HEADER_COMMITTER;
> +
> +		strbuf_addf(sb, "%s: ", what);
> +		if (pp->fmt == CMIT_FMT_FULLER)
> +			strbuf_addchars(sb, ' ', 4);
> +
> +		strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
> +			    (int)maillen, mailbuf);
> +
> +		append_line_with_color(sb, opt, id.buf, id.len, pp->color,
> +				       GREP_CONTEXT_HEAD, field);

... which is exactly what this caller is doing.

Very nice.

  reply	other threads:[~2021-09-28  0:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 15:49 [PATCH v8 1/2] grep: refactor next_match() and match_one_pattern() for external use Hamza Mahfooz
2021-09-24 15:50 ` [PATCH v8 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
2021-09-28  0:03   ` Junio C Hamano [this message]
2021-09-24 19:23 ` [PATCH v8 1/2] grep: refactor next_match() and match_one_pattern() for external use 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=xmqqilylmuqx.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=someguy@effective-light.com \
    --cc=sunshine@sunshineco.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).