[PATCH 4 of 7] annotate: build format string separately from annotation data

Yuya Nishihara yuya at tcha.org
Wed Sep 17 10:11:25 CDT 2014


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1409283412 -7200
#      Fri Aug 29 05:36:52 2014 +0200
# Node ID 9cb724891a846ba60086467e2a2a304eab9ef865
# Parent  0bddd08bf12d7e2c4ad8f7fa2bd8c87dcfaab0a4
annotate: build format string separately from annotation data

This prepares for porting to generic templater API.

Note that we cannot use '%*s' to pad white spaces because it doesn't take
into account character widths, as described in 4f5a6df2af92.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -313,6 +313,7 @@ def annotate(ui, repo, *pats, **opts):
 
         lines = fctx.annotate(follow=follow, linenumber=linenumber,
                               diffopts=diffopts)
+        formats = []
         pieces = []
 
         for f, sep in funcmap:
@@ -320,11 +321,13 @@ def annotate(ui, repo, *pats, **opts):
             if l:
                 sized = [(x, encoding.colwidth(x)) for x in l]
                 ml = max([w for x, w in sized])
-                pieces.append(["%s%s%s" % (sep, ' ' * (ml - w), x)
-                               for x, w in sized])
-
-        for p, l in zip(zip(*pieces), lines):
-            ui.write("%s: %s" % ("".join(p), l[1]))
+                formats.append([sep + ' ' * (ml - w) + '%s'
+                                for x, w in sized])
+                pieces.append(l)
+
+        for f, p, l in zip(zip(*formats), zip(*pieces), lines):
+            ui.write("".join(f) % p)
+            ui.write(": %s" % l[1])
 
         if lines and not lines[-1][1].endswith('\n'):
             ui.write('\n')


More information about the Mercurial-devel mailing list