Patchwork [1,of,3,V2] grep: give different labels to + and - symbols

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date Feb. 19, 2019, 1:03 a.m.
Message ID <14bb51f7e85c68b31a96.1550538220@chloe>
Download mbox | patch
Permalink /patch/38815/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - Feb. 19, 2019, 1:03 a.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1550262244 18000
#      Fri Feb 15 15:24:04 2019 -0500
# Node ID 14bb51f7e85c68b31a96ea6447f7439ac2d87e18
# Parent  37b33c34bf4f890857b5e8728febbc82a99368a5
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.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2885,21 +2885,25 @@  def grep(ui, repo, pattern, *pats, **opt
             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():
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -299,6 +299,10 @@  Test wdir
   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