changeset 18970:3cdb6f2f6789

templatekw: add default styles for hybrid types (issue3887) This allows elements like file_copies to be printed as 'name (source)' when used with join.
author Matt Mackall <mpm@selenic.com>
date Tue, 16 Apr 2013 09:44:29 -0500
parents 257afe5489d4
children c4266e9afc4f
files mercurial/templatekw.py mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatekw.py	Wed Apr 10 02:27:35 2013 +0900
+++ b/mercurial/templatekw.py	Tue Apr 16 09:44:29 2013 -0500
@@ -14,9 +14,13 @@
 #  "{files % '{file}\n'}" (hgweb-style with inlining and function support)
 
 class _hybrid(object):
-    def __init__(self, gen, values):
+    def __init__(self, gen, values, joinfmt=None):
         self.gen = gen
         self.values = values
+        if joinfmt:
+            self.joinfmt = joinfmt
+        else:
+            self.joinfmt = lambda x: x.values()[0]
     def __iter__(self):
         return self.gen
     def __call__(self):
@@ -245,7 +249,7 @@
 
     c = [{'name': x[0], 'source': x[1]} for x in copies]
     f = _showlist('file_copy', c, plural='file_copies', **args)
-    return _hybrid(f, c)
+    return _hybrid(f, c, lambda x: '%s (%s)' % (x['name'], x['source']))
 
 # showfilecopiesswitch() displays file copies only if copy records are
 # provided before calling the templater, usually with a --copies
@@ -257,7 +261,7 @@
     copies = args['revcache'].get('copies') or []
     c = [{'name': x[0], 'source': x[1]} for x in copies]
     f = _showlist('file_copy', c, plural='file_copies', **args)
-    return _hybrid(f, c)
+    return _hybrid(f, c, lambda x: '%s (%s)' % (x['name'], x['source']))
 
 def showfiledels(**args):
     """:file_dels: List of strings. Files removed by this changeset."""
--- a/mercurial/templater.py	Wed Apr 10 02:27:35 2013 +0900
+++ b/mercurial/templater.py	Tue Apr 16 09:44:29 2013 -0500
@@ -228,7 +228,8 @@
 
     joinset = args[0][0](context, mapping, args[0][1])
     if util.safehasattr(joinset, '__call__'):
-        joinset = [x.values()[0] for x in joinset()]
+        jf = joinset.joinfmt
+        joinset = [jf(x) for x in joinset()]
 
     joiner = " "
     if len(args) > 1:
--- a/tests/test-command-template.t	Wed Apr 10 02:27:35 2013 +0900
+++ b/tests/test-command-template.t	Tue Apr 16 09:44:29 2013 -0500
@@ -43,6 +43,8 @@
   $ hg mv second fourth
   $ hg commit -m third -d "2020-01-01 10:01"
 
+  $ hg log --template '{join(file_copies, ",\n")}\n' -r .
+  fourth (second)
   $ hg log --template '{file_copies % "{source} -> {name}\n"}' -r .
   second -> fourth