changeset 32950:5100ce217dfa

formatter: add support for separator template This seems useful for writing JSON template.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 21:50:51 +0900
parents 13eebc189ff3
children 050efe9a1644
files mercurial/formatter.py tests/test-branches.t
diffstat 2 files changed, 6 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/formatter.py	Sat Apr 22 21:46:14 2017 +0900
+++ b/mercurial/formatter.py	Sat Apr 22 21:50:51 2017 +0900
@@ -351,7 +351,7 @@
         self._tref = spec.ref
         self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl)
         self._parts = templatepartsmap(spec, self._t,
-                                       ['docheader', 'docfooter'])
+                                       ['docheader', 'docfooter', 'separator'])
         self._counter = itertools.count()
         self._cache = {}  # for templatekw/funcs to store reusable data
         self._renderitem('docheader', {})
@@ -364,7 +364,9 @@
 
     def _showitem(self):
         item = self._item.copy()
-        item['index'] = next(self._counter)
+        item['index'] = index = next(self._counter)
+        if index > 0:
+            self._renderitem('separator', {})
         self._renderitem(self._tref, item)
 
     def _renderitem(self, part, item):
--- a/tests/test-branches.t	Sat Apr 22 21:46:14 2017 +0900
+++ b/tests/test-branches.t	Sat Apr 22 21:50:51 2017 +0900
@@ -533,7 +533,8 @@
   $ cat <<'EOF' > "$TESTTMP/map-myjson"
   > docheader = '\{\n'
   > docfooter = '\n}\n'
-  > branches = '{ifeq(index, 0, "", ",\n")} {dict(branch, node|short)|json}'
+  > separator = ',\n'
+  > branches = ' {dict(branch, node|short)|json}'
   > EOF
   $ hg branches -T "$TESTTMP/map-myjson"
   {