changeset 41739:8ab42ccb68fe

grep: give different labels to + and - symbols I find it more useful to give different colours to plus and minus, but it's difficult to do so if the default output uses the same label for both. The following augments the names of columns with some extra labels, akin to the diff.inserted and diff.deleted labels for the diff command. This is done by adding an extra label field to the columns tuples.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Fri, 15 Feb 2019 15:24:04 -0500
parents c70bdd222dcd
children ee77a6dd8fb8
files mercurial/commands.py tests/test-grep.t
diffstat 2 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Mon Feb 18 00:27:25 2019 +0900
+++ b/mercurial/commands.py	Fri Feb 15 15:24:04 2019 -0500
@@ -2885,21 +2885,25 @@
             fm.plain(uipathfn(fn), label='grep.filename')
 
             cols = [
-                ('rev', '%d', rev, not plaingrep),
-                ('linenumber', '%d', l.linenum, opts.get('line_number')),
+                ('rev', '%d', rev, not plaingrep, ''),
+                ('linenumber', '%d', l.linenum, opts.get('line_number'), ''),
             ]
             if diff:
-                cols.append(('change', '%s', change, True))
+                cols.append(
+                    ('change', '%s', change, True,
+                     'grep.inserted ' if change == '+' else 'grep.deleted ')
+                )
             cols.extend([
-                ('user', '%s', formatuser(ctx.user()), opts.get('user')),
+                ('user', '%s', formatuser(ctx.user()), opts.get('user'), ''),
                 ('date', '%s', fm.formatdate(ctx.date(), datefmt),
-                 opts.get('date')),
+                 opts.get('date'), ''),
             ])
-            for name, fmt, data, cond in cols:
+            for name, fmt, data, cond, extra_label in cols:
                 if cond:
                     fm.plain(sep, label='grep.sep')
                 field = fieldnamemap.get(name, name)
-                fm.condwrite(cond, field, fmt, data, label='grep.%s' % name)
+                label = extra_label + ('grep.%s' % name)
+                fm.condwrite(cond, field, fmt, data, label=label)
             if not opts.get('files_with_matches'):
                 fm.plain(sep, label='grep.sep')
                 if not opts.get('text') and binary():
--- a/tests/test-grep.t	Mon Feb 18 00:27:25 2019 +0900
+++ b/tests/test-grep.t	Fri Feb 15 15:24:04 2019 -0500
@@ -299,6 +299,10 @@
   color:3:+:orange
   color:2:-:orange
   color:1:+:orange
+  $ hg grep --diff orange --color=debug
+  [grep.filename|color][grep.sep|:][grep.rev|3][grep.sep|:][grep.inserted grep.change|+][grep.sep|:][grep.match|orange]
+  [grep.filename|color][grep.sep|:][grep.rev|2][grep.sep|:][grep.deleted grep.change|-][grep.sep|:][grep.match|orange]
+  [grep.filename|color][grep.sep|:][grep.rev|1][grep.sep|:][grep.inserted grep.change|+][grep.sep|:][grep.match|orange]
 
   $ hg grep --diff orange
   color:3:+:orange