hgweb: use generator to count parity of horizontal stripes for easier reading.
authorThomas Arendsen Hein <thomas@intevation.de>
Tue, 29 May 2007 16:42:05 +0200
changeset 4462 12e4d9524951
parent 4461 3e679426dd7f
child 4463 a73cf208b2a0
child 4464 3f444b82df9e
hgweb: use generator to count parity of horizontal stripes for easier reading. - use web.stripes in all places and consistently - start with parity0 for lists generated in reverse (e.g. changelog)
mercurial/hgweb/common.py
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/common.py	Tue May 29 07:00:36 2007 -0400
+++ b/mercurial/hgweb/common.py	Tue May 29 16:42:05 2007 +0200
@@ -60,3 +60,19 @@
             return mapfile
     raise RuntimeError("No hgweb templates found in %r" % templatepath)
 
+def paritygen(stripecount, offset=0):
+    """count parity of horizontal stripes for easier reading"""
+    if stripecount and offset:
+        # account for offset, e.g. due to building the list in reverse
+        count = (stripecount + offset) % stripecount
+        parity = (stripecount + offset) / stripecount & 1
+    else:
+        count = 0
+        parity = 0
+    while True:
+        yield parity
+        count += 1
+        if stripecount and count >= stripecount:
+            parity = 1 - parity
+            count = 0
+
--- a/mercurial/hgweb/hgweb_mod.py	Tue May 29 07:00:36 2007 -0400
+++ b/mercurial/hgweb/hgweb_mod.py	Tue May 29 16:42:05 2007 +0200
@@ -12,7 +12,7 @@
 from mercurial.i18n import gettext as _
 from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
 from mercurial import revlog, templater
-from common import get_mtime, staticfile, style_map
+from common import get_mtime, staticfile, style_map, paritygen
 
 def _up(p):
     if p[0] != "/":
@@ -147,14 +147,13 @@
                 l += [x for x in files if x.startswith(t)]
             return l
 
-        parity = [0]
+        parity = paritygen(self.stripecount)
         def diffblock(diff, f, fn):
             yield self.t("diffblock",
                          lines=prettyprintlines(diff),
-                         parity=parity[0],
+                         parity=parity.next(),
                          file=f,
                          filenode=hex(fn or nullid))
-            parity[0] = 1 - parity[0]
 
         def prettyprintlines(diff):
             for l in diff.splitlines(1):
@@ -197,14 +196,13 @@
 
     def changelog(self, ctx, shortlog=False):
         def changelist(**map):
-            parity = (start - end) & 1
             cl = self.repo.changelog
             l = [] # build a list in forward order for efficiency
             for i in xrange(start, end):
                 ctx = self.repo.changectx(i)
                 n = ctx.node()
 
-                l.insert(0, {"parity": parity,
+                l.insert(0, {"parity": parity.next(),
                              "author": ctx.user(),
                              "parent": self.siblings(ctx.parents(), i - 1),
                              "child": self.siblings(ctx.children(), i + 1),
@@ -214,7 +212,6 @@
                              "files": self.listfilediffs(ctx.files(), n),
                              "rev": i,
                              "node": hex(n)})
-                parity = 1 - parity
 
             for e in l:
                 yield e
@@ -226,6 +223,7 @@
         start = max(0, pos - maxchanges + 1)
         end = min(count, start + maxchanges)
         pos = end - 1
+        parity = paritygen(self.stripecount, offset=start-end)
 
         changenav = revnavgen(pos, maxchanges, count, self.repo.changectx)
 
@@ -267,7 +265,7 @@
                 n = ctx.node()
 
                 yield self.t('searchentry',
-                             parity=self.stripes(count),
+                             parity=parity.next(),
                              author=ctx.user(),
                              parent=self.siblings(ctx.parents()),
                              child=self.siblings(ctx.children()),
@@ -282,6 +280,7 @@
                     break
 
         cl = self.repo.changelog
+        parity = paritygen(self.stripecount)
 
         yield self.t('search',
                      query=query,
@@ -294,12 +293,11 @@
         p1 = parents[0].node()
 
         files = []
-        parity = 0
+        parity = paritygen(self.stripecount)
         for f in ctx.files():
             files.append(self.t("filenodelink",
                                 node=hex(n), file=f,
-                                parity=parity))
-            parity = 1 - parity
+                                parity=parity.next()))
 
         def diff(**map):
             yield self.diff(p1, n, None)
@@ -326,16 +324,16 @@
         start = max(0, pos - pagelen + 1)
         end = min(count, start + pagelen)
         pos = end - 1
+        parity = paritygen(self.stripecount, offset=start-end)
 
         def entries(**map):
             l = []
-            parity = (count - 1) & 1
 
             for i in xrange(start, end):
                 ctx = fctx.filectx(i)
                 n = fl.node(i)
 
-                l.insert(0, {"parity": parity,
+                l.insert(0, {"parity": parity.next(),
                              "filerev": i,
                              "file": f,
                              "node": hex(ctx.node()),
@@ -345,7 +343,6 @@
                              "parent": self.siblings(fctx.parents()),
                              "child": self.siblings(fctx.children()),
                              "desc": ctx.description()})
-                parity = 1 - parity
 
             for e in l:
                 yield e
@@ -360,6 +357,7 @@
         text = fctx.data()
         fl = fctx.filelog()
         n = fctx.filenode()
+        parity = paritygen(self.stripecount)
 
         mt = mimetypes.guess_type(f)[0]
         rawtext = text
@@ -372,7 +370,7 @@
             for l, t in enumerate(text.splitlines(1)):
                 yield {"line": t,
                        "linenumber": "% 6d" % (l + 1),
-                       "parity": self.stripes(l)}
+                       "parity": parity.next()}
 
         yield self.t("filerevision",
                      file=f,
@@ -394,19 +392,18 @@
         f = fctx.path()
         n = fctx.filenode()
         fl = fctx.filelog()
+        parity = paritygen(self.stripecount)
 
         def annotate(**map):
-            parity = 0
             last = None
             for f, l in fctx.annotate(follow=True):
                 fnode = f.filenode()
                 name = self.repo.ui.shortuser(f.user())
 
                 if last != fnode:
-                    parity = 1 - parity
                     last = fnode
 
-                yield {"parity": parity,
+                yield {"parity": parity.next(),
                        "node": hex(f.node()),
                        "rev": f.rev(),
                        "author": name,
@@ -432,6 +429,7 @@
         node = ctx.node()
 
         files = {}
+        parity = paritygen(self.stripecount)
 
         if path and path[-1] != "/":
             path += "/"
@@ -450,7 +448,6 @@
                 files[short] = (f, n)
 
         def filelist(**map):
-            parity = 0
             fl = files.keys()
             fl.sort()
             for f in fl:
@@ -459,14 +456,12 @@
                     continue
 
                 yield {"file": full,
-                       "parity": self.stripes(parity),
+                       "parity": parity.next(),
                        "basename": f,
                        "size": ctx.filectx(full).size(),
                        "permissions": mf.execf(full)}
-                parity += 1
 
         def dirlist(**map):
-            parity = 0
             fl = files.keys()
             fl.sort()
             for f in fl:
@@ -474,17 +469,16 @@
                 if fnode:
                     continue
 
-                yield {"parity": self.stripes(parity),
+                yield {"parity": parity.next(),
                        "path": os.path.join(abspath, f),
                        "basename": f[:-1]}
-                parity += 1
 
         yield self.t("manifest",
                      rev=ctx.rev(),
                      node=hex(node),
                      path=abspath,
                      up=_up(abspath),
-                     upparity=self.stripes(0),
+                     upparity=parity.next(),
                      fentries=filelist,
                      dentries=dirlist,
                      archives=self.archivelist(hex(node)))
@@ -492,17 +486,16 @@
     def tags(self):
         i = self.repo.tagslist()
         i.reverse()
+        parity = paritygen(self.stripecount)
 
         def entries(notip=False, **map):
-            parity = 0
             for k, n in i:
                 if notip and k == "tip":
                     continue
-                yield {"parity": self.stripes(parity),
+                yield {"parity": parity.next(),
                        "tag": k,
                        "date": self.repo.changectx(n).date(),
                        "node": hex(n)}
-                parity += 1
 
         yield self.t("tags",
                      node=hex(self.repo.changelog.tip()),
@@ -514,7 +507,7 @@
         i.reverse()
 
         def tagentries(**map):
-            parity = 0
+            parity = paritygen(self.stripecount)
             count = 0
             for k, n in i:
                 if k == "tip": # skip tip
@@ -525,15 +518,14 @@
                     break;
 
                 yield self.t("tagentry",
-                             parity=self.stripes(parity),
+                             parity=parity.next(),
                              tag=k,
                              node=hex(n),
                              date=self.repo.changectx(n).date())
-                parity += 1
 
 
         def branches(**map):
-            parity = 0
+            parity = paritygen(self.stripecount)
 
             b = self.repo.branchtags()
             l = [(-self.repo.changelog.rev(n), n, t) for t, n in b.items()]
@@ -542,14 +534,13 @@
             for r,n,t in l:
                 ctx = self.repo.changectx(n)
 
-                yield {'parity': self.stripes(parity),
+                yield {'parity': parity.next(),
                        'branch': t,
                        'node': hex(n),
                        'date': ctx.date()}
-                parity += 1
 
         def changelist(**map):
-            parity = 0
+            parity = paritygen(self.stripecount, offset=start-end)
             l = [] # build a list in forward order for efficiency
             for i in xrange(start, end):
                 ctx = self.repo.changectx(i)
@@ -557,13 +548,12 @@
 
                 l.insert(0, self.t(
                     'shortlogentry',
-                    parity=parity,
+                    parity=parity.next(),
                     author=ctx.user(),
                     desc=ctx.description(),
                     date=ctx.date(),
                     rev=i,
                     node=hn))
-                parity = 1 - parity
 
             yield l
 
@@ -846,13 +836,6 @@
 
         return fctx
 
-    def stripes(self, parity):
-        "make horizontal stripes for easier reading"
-        if self.stripecount:
-            return (1 + parity / self.stripecount) & 1
-        else:
-            return 0
-
     def do_log(self, req):
         if req.form.has_key('file') and req.form['file'][0]:
             self.do_filelog(req)
--- a/mercurial/hgweb/hgwebdir_mod.py	Tue May 29 07:00:36 2007 -0400
+++ b/mercurial/hgweb/hgwebdir_mod.py	Tue May 29 16:42:05 2007 +0200
@@ -10,7 +10,7 @@
 import os, mimetools, cStringIO
 from mercurial.i18n import gettext as _
 from mercurial import ui, hg, util, templater
-from common import get_mtime, staticfile, style_map
+from common import get_mtime, staticfile, style_map, paritygen
 from hgweb_mod import hgweb
 
 # This is a stopgap
@@ -22,6 +22,7 @@
         self.parentui = parentui
         self.motd = None
         self.style = None
+        self.stripecount = None
         self.repos_sorted = ('name', False)
         if isinstance(config, (list, tuple)):
             self.repos = cleannames(config)
@@ -41,6 +42,8 @@
                     self.motd = cp.get('web', 'motd')
                 if cp.has_option('web', 'style'):
                     self.style = cp.get('web', 'style')
+                if cp.has_option('web', 'stripes'):
+                    self.stripecount = int(cp.get('web', 'stripes'))
             if cp.has_section('paths'):
                 self.repos.extend(cleannames(cp.items('paths')))
             if cp.has_section('collections'):
@@ -97,6 +100,8 @@
             style = config('web', 'style', '')
         if req.form.has_key('style'):
             style = req.form['style'][0]
+        if self.stripecount is None:
+            self.stripecount = int(config('web', 'stripes', 1))
         mapfile = style_map(templater.templatepath(), style)
         tmpl = templater.templater(mapfile, templater.common_filters,
                                    defaults={"header": header,
@@ -127,7 +132,7 @@
                     separator = ';'
 
             rows = []
-            parity = 0
+            parity = paritygen(self.stripecount)
             for name, path in self.repos:
                 u = ui.ui(parentui=parentui)
                 try:
@@ -165,8 +170,7 @@
                 if (not sortcolumn
                     or (sortcolumn, descending) == self.repos_sorted):
                     # fast path for unsorted output
-                    row['parity'] = parity
-                    parity = 1 - parity
+                    row['parity'] = parity.next()
                     yield row
                 else:
                     rows.append((row["%s_sort" % sortcolumn], row))
@@ -175,8 +179,7 @@
                 if descending:
                     rows.reverse()
                 for key, row in rows:
-                    row['parity'] = parity
-                    parity = 1 - parity
+                    row['parity'] = parity.next()
                     yield row
 
         try: