Patchwork [4,of,8] grep: build list of all columns regardless of display options

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 5, 2016, 8:07 p.m.
Message ID <0418cdf67efb3c2face1.1473106072@waste.org>
Download mbox | patch
Permalink /patch/16555/
State Not Applicable
Headers show

Comments

Yuya Nishihara - Sept. 5, 2016, 8:07 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1471499526 -32400
#      Thu Aug 18 14:52:06 2016 +0900
# Node ID 0418cdf67efb3c2face162a91eaa5d1def7193f9
# Parent  37d838e8eb0b40bfb6bfdc902cacdb3d62a0ce60
grep: build list of all columns regardless of display options

These columns should always be available in JSON or template outputs. The
"change" column is excluded because it has no useful data unless --all is
specified.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4397,20 +4397,23 @@ 
         else:
             iter = [('', l) for l in states]
         for change, l in iter:
-            cols = [(fn, 'filename'), (str(rev), 'rev')]
-
-            if opts.get('line_number'):
-                cols.append((str(l.linenum), 'linenumber'))
+            cols = [
+                ('filename', fn, True),
+                ('rev', str(rev), True),
+                ('linenumber', str(l.linenum), opts.get('line_number')),
+            ]
             if opts.get('all'):
-                cols.append((change, 'change'))
-            if opts.get('user'):
-                cols.append((ui.shortuser(ctx.user()), 'user'))
-            if opts.get('date'):
-                cols.append((datefunc(ctx.date()), 'date'))
-            for col, field in cols[:-1]:
-                ui.write(col, label='grep.%s' % field)
-                ui.write(sep, label='grep.sep')
-            ui.write(cols[-1][0], label='grep.%s' % cols[-1][1])
+                cols.append(('change', change, True))
+            cols.extend([
+                ('user', ui.shortuser(ctx.user()), opts.get('user')),
+                ('date', datefunc(ctx.date()), opts.get('date')),
+            ])
+            lastcol = next(name for name, data, cond in reversed(cols) if cond)
+            for name, data, cond in cols:
+                if cond:
+                    ui.write(data, label='grep.%s' % name)
+                if cond and name != lastcol:
+                    ui.write(sep, label='grep.sep')
             if not opts.get('files_with_matches'):
                 ui.write(sep, label='grep.sep')
                 if not opts.get('text') and binary():