Mercurial > hg
changeset 37325:41a5d815d2c1
templater: factor out generator of join()-ed items
Prepares for defining join() behavior per wrapped types and getting rid
of the public joinfmt attribute.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 17 Mar 2018 21:42:27 +0900 |
parents | c2f74b8f6b7f |
children | 9cd88dd3bf64 |
files | mercurial/templatefuncs.py mercurial/templateutil.py |
diffstat | 2 files changed, 15 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/templatefuncs.py Sun Mar 18 23:24:50 2018 +0900 +++ b/mercurial/templatefuncs.py Sat Mar 17 21:42:27 2018 +0900 @@ -324,14 +324,8 @@ joiner = " " if len(args) > 1: joiner = evalstring(context, mapping, args[1]) - - first = True - for x in pycompat.maybebytestr(joinset): - if first: - first = False - else: - yield joiner - yield joinfmt(x) + itemiter = (joinfmt(x) for x in pycompat.maybebytestr(joinset)) + return templateutil.joinitems(itemiter, joiner) @templatefunc('label(label, expr)') def label(context, mapping, args):
--- a/mercurial/templateutil.py Sun Mar 18 23:24:50 2018 +0900 +++ b/mercurial/templateutil.py Sat Mar 17 21:42:27 2018 +0900 @@ -75,19 +75,12 @@ """ def __init__(self, gen, values, makemap, joinfmt, keytype=None): - if gen is not None: - self._gen = gen # generator or function returning generator + self._gen = gen # generator or function returning generator self._values = values self._makemap = makemap self.joinfmt = joinfmt self.keytype = keytype # hint for 'x in y' where type(x) is unresolved - def _gen(self): - """Default generator to stringify this as {join(self, ' ')}""" - for i, x in enumerate(self._values): - if i > 0: - yield ' ' - yield self.joinfmt(x) def itermaps(self, context): makemap = self._makemap for x in self._values: @@ -96,6 +89,8 @@ def show(self, context, mapping): # TODO: switch gen to (context, mapping) API? gen = self._gen + if gen is None: + return joinitems((self.joinfmt(x) for x in self._values), ' ') if callable(gen): return gen() return gen @@ -556,3 +551,13 @@ if val is None: return return wraphybridvalue(dictarg, key, val) + +def joinitems(itemiter, sep): + """Join items with the separator; Returns generator of bytes""" + first = True + for x in itemiter: + if first: + first = False + else: + yield sep + yield x