diff mercurial/formatter.py @ 31807:e6eb86b154c5

templater: provide loop counter as "index" keyword This was originally written for JSON templating where we would have to be careful to not add extra comma, but seems generally useful. Inner loop started by % operator has its own counter.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 22 Apr 2016 21:46:33 +0900
parents dca9b6922514
children 21f129354dd0
line wrap: on
line diff
--- a/mercurial/formatter.py	Fri Apr 22 21:45:06 2016 +0900
+++ b/mercurial/formatter.py	Fri Apr 22 21:46:33 2016 +0900
@@ -103,6 +103,7 @@
 
 from __future__ import absolute_import
 
+import itertools
 import os
 
 from .i18n import _
@@ -338,6 +339,7 @@
         self._topic = topic
         self._t = gettemplater(ui, topic, opts.get('template', ''),
                                cache=templatekw.defaulttempl)
+        self._counter = itertools.count()
         self._cache = {}  # for templatekw/funcs to store reusable data
     def context(self, **ctxs):
         '''insert context objects to be used to render template keywords'''
@@ -350,6 +352,7 @@
         props = {}
         if 'ctx' in self._item:
             props.update(templatekw.keywords)
+        props['index'] = next(self._counter)
         # explicitly-defined fields precede templatekw
         props.update(self._item)
         if 'ctx' in self._item: