changeset 32038:3920b5970f95

templatekw: factor out showdict() helper Make it less cryptic for common cases.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 05 Apr 2017 21:57:05 +0900
parents e5eab0fe69ee
children 2ab7578e685b
files mercurial/templatekw.py
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatekw.py	Wed Apr 05 21:47:34 2017 +0900
+++ b/mercurial/templatekw.py	Wed Apr 05 21:57:05 2017 +0900
@@ -78,6 +78,12 @@
         return thing
     return thing.gen
 
+def showdict(name, data, mapping, plural=None, key='key', value='value',
+             fmt='%s=%s', separator=' '):
+    c = [{key: k, value: v} for k, v in data.iteritems()]
+    f = _showlist(name, c, mapping, plural, separator)
+    return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
+
 def showlist(name, values, mapping, plural=None, element=None, separator=' '):
     if not element:
         element = name
@@ -348,14 +354,9 @@
 @templatekeyword('envvars')
 def showenvvars(repo, **args):
     """A dictionary of environment variables. (EXPERIMENTAL)"""
-
     env = repo.ui.exportableenviron()
     env = util.sortdict((k, env[k]) for k in sorted(env))
-    makemap = lambda k: {'key': k, 'value': env[k]}
-    c = [makemap(k) for k in env]
-    f = _showlist('envvar', c, args, plural='envvars')
-    return _hybrid(f, env, makemap,
-                   lambda x: '%s=%s' % (x['key'], x['value']))
+    return showdict('envvar', env, args, plural='envvars')
 
 @templatekeyword('extras')
 def showextras(**args):
@@ -394,11 +395,8 @@
                 copies.append((fn, rename[0]))
 
     copies = util.sortdict(copies)
-    makemap = lambda k: {'name': k, 'source': copies[k]}
-    c = [makemap(k) for k in copies]
-    f = _showlist('file_copy', c, args, plural='file_copies')
-    return _hybrid(f, copies, makemap,
-                   lambda x: '%s (%s)' % (x['name'], x['source']))
+    return showdict('file_copy', copies, args, plural='file_copies',
+                    key='name', value='source', fmt='%s (%s)')
 
 # showfilecopiesswitch() displays file copies only if copy records are
 # provided before calling the templater, usually with a --copies
@@ -410,11 +408,8 @@
     """
     copies = args['revcache'].get('copies') or []
     copies = util.sortdict(copies)
-    makemap = lambda k: {'name': k, 'source': copies[k]}
-    c = [makemap(k) for k in copies]
-    f = _showlist('file_copy', c, args, plural='file_copies')
-    return _hybrid(f, copies, makemap,
-                   lambda x: '%s (%s)' % (x['name'], x['source']))
+    return showdict('file_copy', copies, args, plural='file_copies',
+                    key='name', value='source', fmt='%s (%s)')
 
 @templatekeyword('file_dels')
 def showfiledels(**args):