git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mark Lodato <lodatom@gmail.com>
To: git@vger.kernel.org
Cc: Mark Lodato <lodatom@gmail.com>
Subject: [PATCH 5/5] grep: Colorize selected, context, and function lines
Date: Fri, 26 Feb 2010 23:57:50 -0500	[thread overview]
Message-ID: <1267246670-19118-6-git-send-email-lodatom@gmail.com> (raw)
In-Reply-To: <1267246670-19118-1-git-send-email-lodatom@gmail.com>

Colorize non-matching text of selected lines, context lines, and
function name lines.  The default for all three is no color, but they
can be configured using color.grep.<slot>.  The first two are similar
to the corresponding options in GNU grep, except that GNU grep applies
the color to the entire line, not just non-matching text.

Signed-off-by: Mark Lodato <lodatom@gmail.com>
---
To me, the biggest benefit is the function line color.  I don't find the other
two useful, but they were trivial to implement.

 Documentation/config.txt |    6 ++++++
 builtin-grep.c           |    9 +++++++++
 grep.c                   |   11 +++++++++--
 grep.h                   |    3 +++
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 154bc02..999b1bd 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -692,12 +692,18 @@ color.grep.<slot>::
 	part of the line to use the specified color, and is one of
 +
 --
+`context`:::
+	non-matching text in context lines (when using `-A`, `-B`, or `-C`)
 `filename`:::
 	filename prefix (when not using `-h`)
+`function`:::
+	function name lines (when using `-p`)
 `linenumber`:::
 	line number prefix (when using `-n`)
 `match`:::
 	matching text
+`selected`:::
+	non-matching text in selected lines
 `separator`:::
 	separators between fields on a line (`:`, `-`, and `=`)
 	and between hunks (`--`)
diff --git a/builtin-grep.c b/builtin-grep.c
index 43b952b..2ae25c0 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -299,12 +299,18 @@ static int grep_config(const char *var, const char *value, void *cb)
 
 	if (!strcmp(var, "color.grep"))
 		opt->color = git_config_colorbool(var, value, -1);
+	else if (!strcmp(var, "color.grep.context"))
+		color = opt->color_context;
 	else if (!strcmp(var, "color.grep.filename"))
 		color = opt->color_filename;
+	else if (!strcmp(var, "color.grep.function"))
+		color = opt->color_function;
 	else if (!strcmp(var, "color.grep.linenumber"))
 		color = opt->color_lineno;
 	else if (!strcmp(var, "color.grep.match"))
 		color = opt->color_match;
+	else if (!strcmp(var, "color.grep.selected"))
+		color = opt->color_selected;
 	else if (!strcmp(var, "color.grep.separator"))
 		color = opt->color_sep;
 	else
@@ -879,9 +885,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	opt.regflags = REG_NEWLINE;
 	opt.max_depth = -1;
 
+	strcpy(opt.color_context, "");
 	strcpy(opt.color_filename, "");
+	strcpy(opt.color_function, "");
 	strcpy(opt.color_lineno, "");
 	strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
+	strcpy(opt.color_selected, "");
 	strcpy(opt.color_sep, GIT_COLOR_CYAN);
 	opt.color = -1;
 	git_config(grep_config, &opt);
diff --git a/grep.c b/grep.c
index 132798d..adc93b0 100644
--- a/grep.c
+++ b/grep.c
@@ -531,6 +531,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		      const char *name, unsigned lno, char sign)
 {
 	int rest = eol - bol;
+	char *line_color = NULL;
 
 	if (opt->pre_context || opt->post_context) {
 		if (opt->last_shown == 0) {
@@ -559,12 +560,18 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		int ch = *eol;
 		int eflags = 0;
 
+		if (sign == ':')
+			line_color = opt->color_selected;
+		else if (sign == '-')
+			line_color = opt->color_context;
+		else if (sign == '=')
+			line_color = opt->color_function;
 		*eol = '\0';
 		while (next_match(opt, bol, eol, ctx, &match, eflags)) {
 			if (match.rm_so == match.rm_eo)
 				break;
 
-			opt->output(opt, bol, match.rm_so);
+			output_color(opt, bol, match.rm_so, line_color);
 			output_color(opt, bol + match.rm_so,
 				     (int)(match.rm_eo - match.rm_so),
 				     opt->color_match);
@@ -574,7 +581,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		}
 		*eol = ch;
 	}
-	opt->output(opt, bol, rest);
+	output_color(opt, bol, rest, line_color);
 	opt->output(opt, "\n", 1);
 }
 
diff --git a/grep.h b/grep.h
index 36919ee..2c4bdac 100644
--- a/grep.h
+++ b/grep.h
@@ -84,9 +84,12 @@ struct grep_opt {
 	int color;
 	int max_depth;
 	int funcname;
+	char color_context[COLOR_MAXLEN];
 	char color_filename[COLOR_MAXLEN];
+	char color_function[COLOR_MAXLEN];
 	char color_lineno[COLOR_MAXLEN];
 	char color_match[COLOR_MAXLEN];
+	char color_selected[COLOR_MAXLEN];
 	char color_sep[COLOR_MAXLEN];
 	int regflags;
 	unsigned pre_context;
-- 
1.7.0

      parent reply	other threads:[~2010-02-27  4:54 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-27  4:57 [PATCH 0/5] color enhancements, particularly for grep Mark Lodato
2010-02-27  4:57 ` [PATCH 1/5] Allow explicit ANSI codes for colors Mark Lodato
2010-02-27  8:51   ` Jeff King
2010-02-27 18:24     ` Mark Lodato
2010-02-27 21:21       ` Junio C Hamano
2010-02-28  2:56         ` [PATCH] color: allow multiple attributes Junio C Hamano
2010-02-28 12:20           ` Jeff King
2010-02-28 18:16             ` Junio C Hamano
2010-02-28 18:33               ` Jeff King
2010-02-27  4:57 ` [PATCH 2/5] Add GIT_COLOR_BOLD_* and GIT_COLOR_BG_* Mark Lodato
2010-02-27  4:57 ` [PATCH 3/5] Remove reference to GREP_COLORS from documentation Mark Lodato
2010-02-27  4:57 ` [PATCH 4/5] grep: Colorize filename, line number, and separator Mark Lodato
2010-02-27 11:43   ` René Scharfe
2010-02-28 20:14     ` Mark Lodato
2010-02-28 22:26       ` Michael Witten
2010-03-02  1:49         ` Mark Lodato
2010-03-02  6:43           ` Michael Witten
2010-03-03  4:26             ` Mark Lodato
2010-03-03  4:49               ` Miles Bader
2010-02-27 11:53   ` René Scharfe
2010-02-27 17:08     ` Junio C Hamano
2010-02-28 20:15       ` Mark Lodato
2010-02-28 19:29   ` Junio C Hamano
2010-02-28 20:39     ` Mark Lodato
2010-02-27  4:57 ` Mark Lodato [this message]

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=1267246670-19118-6-git-send-email-lodatom@gmail.com \
    --to=lodatom@gmail.com \
    --cc=git@vger.kernel.org \
    /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).