[PATCH] hgweb: display branch names

Benoît Allard benoit at aeteurope.nl
Wed Oct 29 05:44:26 CDT 2008


Matt Mackall wrote:
> 
> Here's a simple alternative: pick a number of colors N such that they're
> not readily distinguishable (let's say 13), and pick N colors evenly
> distributed around the color wheel. Then pick a number Q relatively
> prime to N (say 5) and somewhere between N/3 and 2N/3 as the basic step
> between colors. Give the first branch you encounter color 0, the next Q,
> the next 2Q, until it wraps around:
> 
>>>> >>> [(x * 5) % 13 for x in range(13)]
> [0, 5, 10, 2, 7, 12, 4, 9, 1, 6, 11, 3, 8]

I implemented this ...

I've let the number of colors configurable, though 13 seems a bit big, 
as spotted by Dirkjan, the differences in the green are quite difficult 
to make (at least on my MacbookPro). The color of the default branch is 
also let as an exercise to the reader, I know Dirkjan had special ideas 
about that one, I just don't remember which one.

Patch is bellow, but my mailer is known to make strange things with it, 
for convenience, you can also peek it there: 
http://paste.lisp.org/display/69372

# HG changeset patch
# User Benoit Allard <benoit at aeteurope.nl>
# Date 1224779999 -7200
# Node ID 7ce7665b7e618e65c15ca5bd834bf0e55a185520
# Parent  b19c0200c90b33c1fa23317ceeca711ac2b61dd8
coal/paper: show branch names in colors

diff -r b19c0200c90b -r 7ce7665b7e61 mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Tue Oct 28 20:48:30 2008 +0100
+++ b/mercurial/templatefilters.py	Thu Oct 23 18:39:59 2008 +0200
@@ -152,6 +152,39 @@
      else:
          raise TypeError('cannot encode type %s' % obj.__class__.__name__)

+def hsv2rgb(h, s, v):
+    '''
+    convert from HSV color space (h = [0...360], s, v = [0...1])
+    to the RGB color space (r, g, b = [0...1])
+    '''
+    if not s:
+        return v, v, v # grey
+    h /= 60.0
+    i = int(h)
+    f = h - i
+    f = i & 1 and f or 1 - f
+    m = v * (1 - s)
+    n = v * (1 - s * f)
+    return {
+        0: (v, n, m),
+        1: (n, v, m),
+        2: (m, v, n),
+        3: (m, n, v),
+        4: (n, m, v),
+        5: (v, m, n),
+    }[i]
+
+branchcache = {'default': "#"}
+
+def branchcolor(name, saturation):
+    if not name in branchcache:
+        nbcolors = 13.0
+        colorindex = len(branchcache) + 1 % nbcolors
+        color = ((colorindex * 5) % nbcolors) * 360 / nbcolors
+        rgb = hsv2rgb(color, saturation, 0.7)
+        branchcache[name] = "#%02X%02X%02X" % (rgb[0] * 255, rgb[1] * 
255, rgb[2] * 255)
+    return branchcache[name]
+
  filters = {
      "addbreaks": nl2br,
      "basename": os.path.basename,
@@ -181,4 +214,6 @@
      "stringescape": lambda x: x.encode('string_escape'),
      "xmlescape": xmlescape,
      "json": json,
+    "branchheadcolor": lambda x: branchcolor(x, 0.8),
+    "branchnamecolor": lambda x: branchcolor(x, 0.5),
  }
diff -r b19c0200c90b -r 7ce7665b7e61 templates/coal/map
--- a/templates/coal/map	Tue Oct 28 20:48:30 2008 +0100
+++ b/templates/coal/map	Thu Oct 23 18:39:59 2008 +0200
@@ -57,6 +57,8 @@
  tags = tags.tmpl
  tagentry = '<tr class="tagEntry parity{parity}"><td><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{tag|escape}</a></td><td 
class="node">{node|short}</td></tr>'
  changelogtag = '<span class="tag">{name|escape}</span> '
+changelogbranchhead = '<span class="branchhead" style="color: 
{name|branchheadcolor};">{name|escape}</span> '
+changelogbranchname = '<span class="branchname" style="color: 
{name|branchnamecolor};">{name|escape}</span> '
  changesettag = '<span class="tag">{tag|escape}</span> '
  filediffparent = '<tr><th class="parent">parent {rev}:</th><td 
class="parent"><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
  filelogparent = '<tr><th>parent {rev}:</th><td><a 
href="{url}file/{node|short}/{file|urlescape}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
diff -r b19c0200c90b -r 7ce7665b7e61 templates/coal/shortlogentry.tmpl
--- a/templates/coal/shortlogentry.tmpl	Tue Oct 28 20:48:30 2008 +0100
+++ b/templates/coal/shortlogentry.tmpl	Thu Oct 23 18:39:59 2008 +0200
@@ -1,5 +1,5 @@
   <tr class="parity{parity}">
    <td class="age">{date|age}</td>
    <td class="author">{author|person}</td>
-  <td class="description"><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{desc|strip|firstline|escape}</a>{tags%changelogtag}</td>
+  <td class="description"><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{desc|strip|firstline|escape}</a>{inbranch%changelogbranchname}{branches%changelogbranchhead}{tags%changelogtag}</td>
   </tr>
diff -r b19c0200c90b -r 7ce7665b7e61 templates/paper/map
--- a/templates/paper/map	Tue Oct 28 20:48:30 2008 +0100
+++ b/templates/paper/map	Thu Oct 23 18:39:59 2008 +0200
@@ -56,8 +56,9 @@
  fileannotatechild = '<tr><td class="metatag">child:</td><td><a 
href="{url}annotate/{node|short}/{file|urlescape}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
  tags = ../coal/tags.tmpl
  tagentry = '<tr class="tagEntry parity{parity}"><td><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{tag|escape}</a></td><td 
class="node">{node|short}</td></tr>'
-changelogtag = '<tr><th class="tag">tag:</th><td 
class="tag">{tag|escape}</td></tr>'
  changelogtag = '<span class="tag">{name|escape}</span> '
+changelogbranchhead = '<span class="branchhead" style="color: 
{name|branchheadcolor};">{name|escape}</span> '
+changelogbranchname = '<span class="branchname" style="color: 
{name|branchnamecolor};">{name|escape}</span> '
  changesettag = '<span class="tag">{tag|escape}</span> '
  filediffparent = '<tr><th class="parent">parent {rev}:</th><td 
class="parent"><a 
href="{url}rev/{node|short}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
  filelogparent = '<tr><th>parent {rev}:</th><td><a 
href="{url}file/{node|short}/{file|urlescape}{sessionvars%urlparameter}">{node|short}</a></td></tr>'
diff -r b19c0200c90b -r 7ce7665b7e61 templates/static/style-coal.css
--- a/templates/static/style-coal.css	Tue Oct 28 20:48:30 2008 +0100
+++ b/templates/static/style-coal.css	Thu Oct 23 18:39:59 2008 +0200
@@ -95,6 +95,20 @@
    vertical-align: baseline;
  }

+.branchhead {
+  font-size: 80%;
+  font-weight: normal;
+  margin-left: .5em;
+  vertical-align: baseline;
+}
+
+.branchname {
+  font-size: 70%;
+  font-weight: normal;
+  margin-left: .5em;
+  vertical-align: baseline;
+}
+
  /* Common */
  pre { margin: 0; }

diff -r b19c0200c90b -r 7ce7665b7e61 templates/static/style-paper.css
--- a/templates/static/style-paper.css	Tue Oct 28 20:48:30 2008 +0100
+++ b/templates/static/style-paper.css	Thu Oct 23 18:39:59 2008 +0200
@@ -91,6 +91,20 @@
    vertical-align: baseline;
  }

+.branchhead {
+  font-size: 80%;
+  font-weight: normal;
+  margin-left: .5em;
+  vertical-align: baseline;
+}
+
+.branchname {
+  font-size: 70%;
+  font-weight: normal;
+  margin-left: .5em;
+  vertical-align: baseline;
+}
+
  /* Common */
  pre { margin: 0; }

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 4197 bytes
Desc: S/MIME Cryptographic Signature
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20081029/db74e91e/attachment.bin 


More information about the Mercurial-devel mailing list