# HG changeset patch
# User Yuya Nishihara <y...@tcha.org>
# Date 1521290547 -32400
#      Sat Mar 17 21:42:27 2018 +0900
# Node ID 419e9203f95ad088cea54213ddbe892beb4775bd
# Parent  1dba3dd3243a9fd0429c2abc28271b17f1e4c097
templater: factor out generator of join()-ed items

Prepares for defining join() behavior per wrapped types and getting rid
of the public joinfmt attribute.

diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -324,14 +324,8 @@ def join(context, mapping, args):
     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):
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -75,19 +75,12 @@ class hybrid(wrapped):
     """
 
     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 @@ class hybrid(wrapped):
     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 @@ def getdictitem(dictarg, key):
     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
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to