[PATCH 6 of 9] grep: refactor column printing to a function

Idan Kamara idankk86 at gmail.com
Sun Oct 14 15:54:23 CDT 2012


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1350238954 -7200
# Node ID 1c796e1364f2563ce655901a400a995f6f1d2230
# Parent  a83911a11e922e7b941e1c1dd6c66a6654a6b069
grep: refactor column printing to a function

This will be useful later on when we want to use a different separator and
line number (context lines).

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3012,25 +3012,30 @@
             flog = getfile(fn)
             return util.binary(flog.read(ctx.filenode(fn)))
 
-        if opts.get('all'):
-            iter = difflinestates(pstates, states)
-        else:
-            iter = [('', l) for l in states]
-        for change, l in iter:
+        def printcols(sep, linenum):
             cols = []
             if not opts.get('no_filename'):
                 cols.append((fn, 'grep.filename'))
             cols.append((str(rev), 'grep.rev'))
-            before, match, after = None, None, None
-
             if opts.get('line_number'):
-                cols.append((str(l.linenum), 'grep.linenumber'))
+                cols.append((str(linenum), 'grep.linenumber'))
             if opts.get('all'):
                 cols.append((change, 'grep.change'))
             if opts.get('user'):
                 cols.append((ui.shortuser(ctx.user()), 'grep.user'))
             if opts.get('date'):
                 cols.append((datefunc(ctx.date()), 'grep.date'))
+            for col, label in cols[:-1]:
+                ui.write(col, label=label)
+                ui.write(sep, label='grep.sep')
+            ui.write(cols[-1][0], label=cols[-1][1])
+
+        if opts.get('all'):
+            iter = difflinestates(pstates, states)
+        else:
+            iter = [('', l) for l in states]
+        for change, l in iter:
+            before, match, after = None, None, None
             if opts.get('files_with_matches'):
                 c = (fn, rev)
                 if c in filerevmatches:
@@ -3040,10 +3045,7 @@
                 before = l.line[:l.colstart]
                 match = l.line[l.colstart:l.colend]
                 after = l.line[l.colend:]
-            for col, label in cols[:-1]:
-                ui.write(col, label=label)
-                ui.write(sep, label='grep.sep')
-            ui.write(cols[-1][0], label=cols[-1][1])
+            printcols(sep, l.linenum)
             if before is not None:
                 ui.write(sep, label='grep.sep')
                 if not opts.get('text') and binary():


More information about the Mercurial-devel mailing list