[PATCH] color: labeled text should be passed to ui.write() as ui.labeled

Steve Borho steve at borho.org
Thu Jun 3 23:53:23 CDT 2010


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1275625098 18000
# Node ID ce4224afddd4e2c3c3a138d0fdec001fe0dbd5ef
# Parent  e581f3acc3385da5d5ddf547882f274d9f7e5b6d
color: labeled text should be passed to ui.write() as ui.labeled

Some implementations of ui.label() (HTML versions in particular) must escape
the provided text and then markup the text with their tags.  When this marked
up text is then passed to ui.write(), we must label the text as 'ui.labeled'
so the implementation knows not to escape it a second time (exposing the initial
markup).

This required the addition of a 'ui.plain' label for text that is purposefully
not marked up.

I was a little pedantic here, passing even ' ' strings to ui.label() when it
would be included with other labeled text in a ui.write() call.   But it seemed
appropriate to lean to the side of caution.

diff --git a/hgext/churn.py b/hgext/churn.py
--- a/hgext/churn.py
+++ b/hgext/churn.py
@@ -150,8 +150,10 @@
     if opts.get('diffstat'):
         width -= 15
         def format(name, (added, removed)):
-            return "%s %15s %s%s\n" % (pad(name, maxname),
-                                       '+%d/-%d' % (added, removed),
+            return "%s %15s %s%s\n" % (ui.label(pad(name, maxname),
+                                                'ui.plain'),
+                                       ui.label('+%d/-%d' % (added, removed),
+                                                'ui.plain'),
                                        ui.label('+' * charnum(added),
                                                 'diffstat.inserted'),
                                        ui.label('-' * charnum(removed),
@@ -159,14 +161,14 @@
     else:
         width -= 6
         def format(name, count):
-            return "%s %6d %s\n" % (pad(name, maxname), sum(count),
-                                    '*' * charnum(sum(count)))
+            return ui.label("%s %6d %s\n" % (pad(name, maxname), sum(count),
+                            '*' * charnum(sum(count))), 'ui.plain')
 
     def charnum(count):
         return int(round(count * width / maxcount))
 
     for name, count in rate:
-        ui.write(format(name, count))
+        ui.write(format(name, count), label='ui.labeled')
 
 
 cmdtable = {
diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -108,7 +108,9 @@
            'status.ignored': 'black bold',
            'status.modified': 'blue bold',
            'status.removed': 'red bold',
-           'status.unknown': 'magenta bold underline'}
+           'status.unknown': 'magenta bold underline',
+           'ui.labeled': 'none',
+           'ui.plain': 'none'}
 
 
 def render_effects(text, effects):
@@ -142,6 +144,8 @@
 
 _buffers = None
 def style(msg, label):
+    if label in ('ui.plain', 'ui.labeled'):
+        return msg
     effects = []
     for l in label.split():
         s = _styles.get(l, '')
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -2145,17 +2145,17 @@
     '''
     def status(idx):
         guards = q.series_guards[idx] or ['unguarded']
-        ui.write('%s: ' % ui.label(q.series[idx], 'qguard.patch'))
+        out = ['%s: ' % ui.label(q.series[idx], 'qguard.patch')]
         for i, guard in enumerate(guards):
             if guard.startswith('+'):
-                ui.write(guard, label='qguard.positive')
+                out.append(ui.label(guard, 'qguard.positive'))
             elif guard.startswith('-'):
-                ui.write(guard, label='qguard.negative')
+                out.append(ui.label(guard, 'qguard.negative'))
             else:
-                ui.write(guard, label='qguard.unguarded')
+                out.append(ui.label(guard, 'qguard.unguarded'))
             if i != len(guards) - 1:
-                ui.write(' ')
-        ui.write('\n')
+                out.append(ui.label(' ', 'ui.plain'))
+        ui.write(''.join(out) + '\n', label='ui.labeled')
     q = repo.mq
     patch = None
     args = list(args)
@@ -2799,7 +2799,8 @@
     if u:
         m.append(ui.label(_("%d unapplied"), 'qseries.unapplied') % u)
     if m:
-        ui.write("mq:     %s\n" % ', '.join(m))
+        ui.write("mq:     ")
+        ui.write(', '.join(m) + '\n', label='ui.labeled')
     else:
         ui.note(_("mq:     (empty queue)\n"))
     return r
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3273,22 +3273,22 @@
     cleanworkdir = False
 
     if len(parents) > 1:
-        t += _(' (merge)')
+        t += ui.label(_(' (merge)'), 'ui.plain')
     elif branch != parents[0].branch():
-        t += _(' (new branch)')
+        t += ui.label(_(' (new branch)'), 'ui.plain')
     elif (parents[0].extra().get('close') and
           pnode in repo.branchheads(branch, closed=True)):
-        t += _(' (head closed)')
+        t += ui.label(_(' (head closed)'), 'ui.plain')
     elif (not st[0] and not st[1] and not st[2] and not st[7]):
-        t += _(' (clean)')
+        t += ui.label(_(' (clean)'), 'ui.plain')
         cleanworkdir = True
     elif pnode not in bheads:
-        t += _(' (new branch head)')
+        t += ui.label(_(' (new branch head)'), 'ui.plain')
 
     if cleanworkdir:
-        ui.status(_('commit: %s\n') % t.strip())
+        ui.status(_('commit: %s\n') % t.strip(), label='ui.labeled')
     else:
-        ui.write(_('commit: %s\n') % t.strip())
+        ui.write(_('commit: %s\n') % t.strip(), label='ui.labeled')
 
     # all ancestors of branch heads - all ancestors of parent = new csets
     new = [0] * len(repo)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -546,5 +546,9 @@
 
         ui.write(s, 'label') is equivalent to
         ui.write(ui.label(s, 'label')).
+
+        Callers of ui.label() should pass labeled text back to
+        ui.write() with a label of 'ui.labeled' so implementations know
+        that the text has already been escaped and marked up.
         '''
         return msg


More information about the Mercurial-devel mailing list