mirror of
https://github.com/facebook/sapling.git
synced 2024-10-07 07:17:55 +03:00
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.
This commit is contained in:
parent
717a34e2df
commit
caee220313
@ -8,6 +8,7 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
|
import itertools
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
@ -1452,6 +1453,7 @@ class changeset_templater(changeset_printer):
|
|||||||
self.t = formatter.maketemplater(ui, 'changeset', tmpl,
|
self.t = formatter.maketemplater(ui, 'changeset', tmpl,
|
||||||
cache=defaulttempl)
|
cache=defaulttempl)
|
||||||
|
|
||||||
|
self._counter = itertools.count()
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
|
||||||
# find correct templates for current mode
|
# find correct templates for current mode
|
||||||
@ -1490,6 +1492,7 @@ class changeset_templater(changeset_printer):
|
|||||||
props['ctx'] = ctx
|
props['ctx'] = ctx
|
||||||
props['repo'] = self.repo
|
props['repo'] = self.repo
|
||||||
props['ui'] = self.repo.ui
|
props['ui'] = self.repo.ui
|
||||||
|
props['index'] = next(self._counter)
|
||||||
props['revcache'] = {'copies': copies}
|
props['revcache'] = {'copies': copies}
|
||||||
props['cache'] = self.cache
|
props['cache'] = self.cache
|
||||||
|
|
||||||
|
@ -103,6 +103,7 @@ baz: foo, bar
|
|||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
@ -338,6 +339,7 @@ class templateformatter(baseformatter):
|
|||||||
self._topic = topic
|
self._topic = topic
|
||||||
self._t = gettemplater(ui, topic, opts.get('template', ''),
|
self._t = gettemplater(ui, topic, opts.get('template', ''),
|
||||||
cache=templatekw.defaulttempl)
|
cache=templatekw.defaulttempl)
|
||||||
|
self._counter = itertools.count()
|
||||||
self._cache = {} # for templatekw/funcs to store reusable data
|
self._cache = {} # for templatekw/funcs to store reusable data
|
||||||
def context(self, **ctxs):
|
def context(self, **ctxs):
|
||||||
'''insert context objects to be used to render template keywords'''
|
'''insert context objects to be used to render template keywords'''
|
||||||
@ -350,6 +352,7 @@ class templateformatter(baseformatter):
|
|||||||
props = {}
|
props = {}
|
||||||
if 'ctx' in self._item:
|
if 'ctx' in self._item:
|
||||||
props.update(templatekw.keywords)
|
props.update(templatekw.keywords)
|
||||||
|
props['index'] = next(self._counter)
|
||||||
# explicitly-defined fields precede templatekw
|
# explicitly-defined fields precede templatekw
|
||||||
props.update(self._item)
|
props.update(self._item)
|
||||||
if 'ctx' in self._item:
|
if 'ctx' in self._item:
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from .i18n import _
|
||||||
from .node import hex, nullid
|
from .node import hex, nullid
|
||||||
from . import (
|
from . import (
|
||||||
encoding,
|
encoding,
|
||||||
@ -422,6 +423,12 @@ def showgraphnode(repo, ctx, **args):
|
|||||||
else:
|
else:
|
||||||
return 'o'
|
return 'o'
|
||||||
|
|
||||||
|
@templatekeyword('index')
|
||||||
|
def showindex(**args):
|
||||||
|
"""Integer. The current iteration of the loop. (0 indexed)"""
|
||||||
|
# just hosts documentation; should be overridden by template mapping
|
||||||
|
raise error.Abort(_("can't use index in this context"))
|
||||||
|
|
||||||
@templatekeyword('latesttag')
|
@templatekeyword('latesttag')
|
||||||
def showlatesttag(**args):
|
def showlatesttag(**args):
|
||||||
"""List of strings. The global tags on the most recent globally
|
"""List of strings. The global tags on the most recent globally
|
||||||
|
@ -411,8 +411,9 @@ def runmap(context, mapping, data):
|
|||||||
else:
|
else:
|
||||||
raise error.ParseError(_("%r is not iterable") % d)
|
raise error.ParseError(_("%r is not iterable") % d)
|
||||||
|
|
||||||
for v in diter:
|
for i, v in enumerate(diter):
|
||||||
lm = mapping.copy()
|
lm = mapping.copy()
|
||||||
|
lm['index'] = i
|
||||||
if isinstance(v, dict):
|
if isinstance(v, dict):
|
||||||
lm.update(v)
|
lm.update(v)
|
||||||
lm['originalnode'] = mapping.get('node')
|
lm['originalnode'] = mapping.get('node')
|
||||||
|
@ -2683,6 +2683,16 @@ Pass generator object created by template function to filter
|
|||||||
$ hg log -l 1 --template '{if(author, author)|user}\n'
|
$ hg log -l 1 --template '{if(author, author)|user}\n'
|
||||||
test
|
test
|
||||||
|
|
||||||
|
Test index keyword:
|
||||||
|
|
||||||
|
$ hg log -l 2 -T '{index + 10}{files % " {index}:{file}"}\n'
|
||||||
|
10 0:a 1:b 2:fifth 3:fourth 4:third
|
||||||
|
11 0:a
|
||||||
|
|
||||||
|
$ hg branches -T '{index} {branch}\n'
|
||||||
|
0 default
|
||||||
|
1 foo
|
||||||
|
|
||||||
Test diff function:
|
Test diff function:
|
||||||
|
|
||||||
$ hg diff -c 8
|
$ hg diff -c 8
|
||||||
|
Loading…
Reference in New Issue
Block a user