##// END OF EJS Templates
templater: provide loop counter as "index" keyword...
Yuya Nishihara -
r31807:e6eb86b1 default
parent child Browse files
Show More
@@ -8,6 +8,7 b''
8 8 from __future__ import absolute_import
9 9
10 10 import errno
11 import itertools
11 12 import os
12 13 import re
13 14 import tempfile
@@ -1452,6 +1453,7 b' class changeset_templater(changeset_prin'
1452 1453 self.t = formatter.maketemplater(ui, 'changeset', tmpl,
1453 1454 cache=defaulttempl)
1454 1455
1456 self._counter = itertools.count()
1455 1457 self.cache = {}
1456 1458
1457 1459 # find correct templates for current mode
@@ -1490,6 +1492,7 b' class changeset_templater(changeset_prin'
1490 1492 props['ctx'] = ctx
1491 1493 props['repo'] = self.repo
1492 1494 props['ui'] = self.repo.ui
1495 props['index'] = next(self._counter)
1493 1496 props['revcache'] = {'copies': copies}
1494 1497 props['cache'] = self.cache
1495 1498
@@ -103,6 +103,7 b' baz: foo, bar'
103 103
104 104 from __future__ import absolute_import
105 105
106 import itertools
106 107 import os
107 108
108 109 from .i18n import _
@@ -338,6 +339,7 b' class templateformatter(baseformatter):'
338 339 self._topic = topic
339 340 self._t = gettemplater(ui, topic, opts.get('template', ''),
340 341 cache=templatekw.defaulttempl)
342 self._counter = itertools.count()
341 343 self._cache = {} # for templatekw/funcs to store reusable data
342 344 def context(self, **ctxs):
343 345 '''insert context objects to be used to render template keywords'''
@@ -350,6 +352,7 b' class templateformatter(baseformatter):'
350 352 props = {}
351 353 if 'ctx' in self._item:
352 354 props.update(templatekw.keywords)
355 props['index'] = next(self._counter)
353 356 # explicitly-defined fields precede templatekw
354 357 props.update(self._item)
355 358 if 'ctx' in self._item:
@@ -7,6 +7,7 b''
7 7
8 8 from __future__ import absolute_import
9 9
10 from .i18n import _
10 11 from .node import hex, nullid
11 12 from . import (
12 13 encoding,
@@ -422,6 +423,12 b' def showgraphnode(repo, ctx, **args):'
422 423 else:
423 424 return 'o'
424 425
426 @templatekeyword('index')
427 def showindex(**args):
428 """Integer. The current iteration of the loop. (0 indexed)"""
429 # just hosts documentation; should be overridden by template mapping
430 raise error.Abort(_("can't use index in this context"))
431
425 432 @templatekeyword('latesttag')
426 433 def showlatesttag(**args):
427 434 """List of strings. The global tags on the most recent globally
@@ -411,8 +411,9 b' def runmap(context, mapping, data):'
411 411 else:
412 412 raise error.ParseError(_("%r is not iterable") % d)
413 413
414 for v in diter:
414 for i, v in enumerate(diter):
415 415 lm = mapping.copy()
416 lm['index'] = i
416 417 if isinstance(v, dict):
417 418 lm.update(v)
418 419 lm['originalnode'] = mapping.get('node')
@@ -2683,6 +2683,16 b' Pass generator object created by templat'
2683 2683 $ hg log -l 1 --template '{if(author, author)|user}\n'
2684 2684 test
2685 2685
2686 Test index keyword:
2687
2688 $ hg log -l 2 -T '{index + 10}{files % " {index}:{file}"}\n'
2689 10 0:a 1:b 2:fifth 3:fourth 4:third
2690 11 0:a
2691
2692 $ hg branches -T '{index} {branch}\n'
2693 0 default
2694 1 foo
2695
2686 2696 Test diff function:
2687 2697
2688 2698 $ hg diff -c 8
General Comments 0
You need to be logged in to leave comments. Login now