##// 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 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import itertools
11 import os
12 import os
12 import re
13 import re
13 import tempfile
14 import tempfile
@@ -1452,6 +1453,7 b' class changeset_templater(changeset_prin'
1452 self.t = formatter.maketemplater(ui, 'changeset', tmpl,
1453 self.t = formatter.maketemplater(ui, 'changeset', tmpl,
1453 cache=defaulttempl)
1454 cache=defaulttempl)
1454
1455
1456 self._counter = itertools.count()
1455 self.cache = {}
1457 self.cache = {}
1456
1458
1457 # find correct templates for current mode
1459 # find correct templates for current mode
@@ -1490,6 +1492,7 b' class changeset_templater(changeset_prin'
1490 props['ctx'] = ctx
1492 props['ctx'] = ctx
1491 props['repo'] = self.repo
1493 props['repo'] = self.repo
1492 props['ui'] = self.repo.ui
1494 props['ui'] = self.repo.ui
1495 props['index'] = next(self._counter)
1493 props['revcache'] = {'copies': copies}
1496 props['revcache'] = {'copies': copies}
1494 props['cache'] = self.cache
1497 props['cache'] = self.cache
1495
1498
@@ -103,6 +103,7 b' baz: foo, bar'
103
103
104 from __future__ import absolute_import
104 from __future__ import absolute_import
105
105
106 import itertools
106 import os
107 import os
107
108
108 from .i18n import _
109 from .i18n import _
@@ -338,6 +339,7 b' class templateformatter(baseformatter):'
338 self._topic = topic
339 self._topic = topic
339 self._t = gettemplater(ui, topic, opts.get('template', ''),
340 self._t = gettemplater(ui, topic, opts.get('template', ''),
340 cache=templatekw.defaulttempl)
341 cache=templatekw.defaulttempl)
342 self._counter = itertools.count()
341 self._cache = {} # for templatekw/funcs to store reusable data
343 self._cache = {} # for templatekw/funcs to store reusable data
342 def context(self, **ctxs):
344 def context(self, **ctxs):
343 '''insert context objects to be used to render template keywords'''
345 '''insert context objects to be used to render template keywords'''
@@ -350,6 +352,7 b' class templateformatter(baseformatter):'
350 props = {}
352 props = {}
351 if 'ctx' in self._item:
353 if 'ctx' in self._item:
352 props.update(templatekw.keywords)
354 props.update(templatekw.keywords)
355 props['index'] = next(self._counter)
353 # explicitly-defined fields precede templatekw
356 # explicitly-defined fields precede templatekw
354 props.update(self._item)
357 props.update(self._item)
355 if 'ctx' in self._item:
358 if 'ctx' in self._item:
@@ -7,6 +7,7 b''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 from .i18n import _
10 from .node import hex, nullid
11 from .node import hex, nullid
11 from . import (
12 from . import (
12 encoding,
13 encoding,
@@ -422,6 +423,12 b' def showgraphnode(repo, ctx, **args):'
422 else:
423 else:
423 return 'o'
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 @templatekeyword('latesttag')
432 @templatekeyword('latesttag')
426 def showlatesttag(**args):
433 def showlatesttag(**args):
427 """List of strings. The global tags on the most recent globally
434 """List of strings. The global tags on the most recent globally
@@ -411,8 +411,9 b' def runmap(context, mapping, data):'
411 else:
411 else:
412 raise error.ParseError(_("%r is not iterable") % d)
412 raise error.ParseError(_("%r is not iterable") % d)
413
413
414 for v in diter:
414 for i, v in enumerate(diter):
415 lm = mapping.copy()
415 lm = mapping.copy()
416 lm['index'] = i
416 if isinstance(v, dict):
417 if isinstance(v, dict):
417 lm.update(v)
418 lm.update(v)
418 lm['originalnode'] = mapping.get('node')
419 lm['originalnode'] = mapping.get('node')
@@ -2683,6 +2683,16 b' Pass generator object created by templat'
2683 $ hg log -l 1 --template '{if(author, author)|user}\n'
2683 $ hg log -l 1 --template '{if(author, author)|user}\n'
2684 test
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 Test diff function:
2696 Test diff function:
2687
2697
2688 $ hg diff -c 8
2698 $ hg diff -c 8
General Comments 0
You need to be logged in to leave comments. Login now