[PATCH 03 of 13 V2] color: extract the label code into its own function

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Feb 24 17:09:37 EST 2017


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1487872826 -3600
#      Thu Feb 23 19:00:26 2017 +0100
# Node ID f7e501bba8311353d25762412670cfffb306a3b1
# Parent  f53e95ec986616a48a3a8e67e5a9674b8a684d6e
# EXP-Topic color
color: extract the label code into its own function

We extract the logic into a function. This will allow use to move the logic into
the core 'color' module and later call it directly from core.

diff -r f53e95ec9866 -r f7e501bba831 hgext/color.py
--- a/hgext/color.py	Mon Feb 20 12:13:23 2017 +0100
+++ b/hgext/color.py	Thu Feb 23 19:00:26 2017 +0100
@@ -331,16 +331,17 @@ class colorui(uimod.ui):
     def label(self, msg, label):
         if self._colormode is None:
             return super(colorui, self).label(msg, label)
+        return colorlabel(self, msg, label)
 
-        if self._colormode == 'debug':
-            if label and msg:
-                if msg[-1] == '\n':
-                    return "[%s|%s]\n" % (label, msg[:-1])
-                else:
-                    return "[%s|%s]" % (label, msg)
+def colorlabel(ui, msg, label):
+    """add color control code according to the mode"""
+    if ui._colormode == 'debug':
+        if label and msg:
+            if msg[-1] == '\n':
+                msg = "[%s|%s]\n" % (label, msg[:-1])
             else:
-                return msg
-
+                msg = "[%s|%s]" % (label, msg)
+    elif ui._colormode is not None:
         effects = []
         for l in label.split():
             s = color._styles.get(l, '')
@@ -350,9 +351,9 @@ class colorui(uimod.ui):
                 effects.append(l)
         effects = ' '.join(effects)
         if effects:
-            return '\n'.join([color._render_effects(line, effects)
-                              for line in msg.split('\n')])
-        return msg
+            msg =  '\n'.join([color._render_effects(line, effects)
+                             for line in msg.split('\n')])
+    return msg
 
 def uisetup(ui):
     if ui.plain():


More information about the Mercurial-devel mailing list