grep: build list of all columns regardless of display options
authorYuya Nishihara <yuya@tcha.org>
Thu, 18 Aug 2016 14:52:06 +0900
changeset 29857 0418cdf67efb
parent 29856 37d838e8eb0b
child 29858 33461139c31c
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.
mercurial/commands.py
--- a/mercurial/commands.py	Thu Aug 18 14:23:29 2016 +0900
+++ b/mercurial/commands.py	Thu Aug 18 14:52:06 2016 +0900
@@ -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():