[PATCH] color: allow multiple args to ui.write()

Kevin Bullock kbullock+mercurial at ringworld.org
Wed Sep 30 22:54:09 CDT 2009


On 30 Sep 2009, at 6:54 PM, Matt Mackall wrote:

> On Tue, 2009-09-29 at 23:59 -0500, Kevin Bullock wrote:
>> So here's a patch to make colorwrap() accept multiple arguments. As
>> implemented, it only colorizes the first argument; I'm not sure  
>> that's
>> the right way to do it and would welcome feedback.
>
> Yeah, it's not. For correctness, all args should be colorized.
> Alternately, we can change ui.write to accept only one arg? Not sure  
> how
> often that's used, but it seems like not much.

Okay, here's a revised patch that colorizes all args. Apologies if  
this isn't the most Pythonic way of doing it (defining an inner  
function to use in a list comprehension). I'm happy to iterate again  
based on feedback.

Pacem in terris / Mir / Shanti / Salaam / Heiwa
Kevin R. Bullock


# HG changeset patch
# User Kevin Bullock <kbullock at ringworld.org>
# Date 1254286182 18000
# Node ID 11fc94edbdc28bb911dec9f2535226f89dbc363d
# Parent  7f691058d62965f55051f2a2b253d094a721f1ac
color: allow multiple args to ui.write()

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -163,22 +163,24 @@
                     'missing': ['red', 'bold'],
                     'unapplied': ['black', 'bold'], }

-def colorwrap(orig, s):
+def colorwrap(orig, *args):
      '''wrap ui.write for colored diff output'''
-    lines = s.split('\n')
-    for i, line in enumerate(lines):
-        stripline = line
-        if line and line[0] in '+-':
-            # highlight trailing whitespace, but only in changed lines
-            stripline = line.rstrip()
-        for prefix, style in _diff_prefixes:
-            if stripline.startswith(prefix):
-                lines[i] = render_effects(stripline, _diff_effects 
[style])
-                break
-        if line != stripline:
-            lines[i] += render_effects(
-                line[len(stripline):], _diff_effects 
['trailingwhitespace'])
-    orig('\n'.join(lines))
+    def _colorize(s):
+        lines = s.split('\n')
+        for i, line in enumerate(lines):
+            stripline = line
+            if line and line[0] in '+-':
+                # highlight trailing whitespace, but only in changed  
lines
+                stripline = line.rstrip()
+            for prefix, style in _diff_prefixes:
+                if stripline.startswith(prefix):
+                    lines[i] = render_effects(stripline, _diff_effects 
[style])
+                    break
+            if line != stripline:
+                lines[i] += render_effects(
+                    line[len(stripline):], _diff_effects 
['trailingwhitespace'])
+        return '\n'.join(lines)
+    orig(*[_colorize(s) for s in args])

  def colorshowpatch(orig, self, node):
      '''wrap cmdutil.changeset_printer.showpatch with colored output'''



More information about the Mercurial-devel mailing list