grep: give different labels to + and - symbols
authorJordi Gutiérrez Hermoso <jordigh@octave.org>
Fri, 15 Feb 2019 15:24:04 -0500
changeset 41739 8ab42ccb68fe
parent 41738 c70bdd222dcd
child 41740 ee77a6dd8fb8
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.
mercurial/commands.py
tests/test-grep.t
--- 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