[PATCH 7 of 8 V2] color: move 'styles' definition on the 'ui' object

Pierre-Yves David pierre-yves.david at ens-lyon.org
Tue Feb 28 05:54:31 EST 2017


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1478459761 -3600
#      Sun Nov 06 20:16:01 2016 +0100
# Node ID 9a08f902da2abeb6cadae6526874327e01943e61
# Parent  b29fb424dbc9e6fc13c369c34277e3fb36284cd4
# EXP-Topic color
color: move 'styles' definition on the 'ui' object

Same logic as for '_terminfoparams'. The content depends on the config so it
should be specific to each 'ui instance.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -207,31 +207,31 @@ def debugcolor(ui, repo, **opts):
         return _debugdisplaycolor(ui)
 
 def _debugdisplaycolor(ui):
-    oldstyle = color._styles.copy()
+    oldstyle = ui._styles.copy()
     try:
-        color._styles.clear()
+        ui._styles.clear()
         for effect in color._effects.keys():
-            color._styles[effect] = effect
+            ui._styles[effect] = effect
         if ui._terminfoparams:
             for k, v in ui.configitems('color'):
                 if k.startswith('color.'):
-                    color._styles[k] = k[6:]
+                    ui._styles[k] = k[6:]
                 elif k.startswith('terminfo.'):
-                    color._styles[k] = k[9:]
+                    ui._styles[k] = k[9:]
         ui.write(_('available colors:\n'))
         # sort label with a '_' after the other to group '_background' entry.
-        items = sorted(color._styles.items(),
+        items = sorted(ui._styles.items(),
                        key=lambda i: ('_' in i[0], i[0], i[1]))
         for colorname, label in items:
             ui.write(('%s\n') % colorname, label=label)
     finally:
-        color._styles.clear()
-        color._styles.update(oldstyle)
+        ui._styles.clear()
+        ui._styles.update(oldstyle)
 
 def _debugdisplaystyle(ui):
     ui.write(_('available style:\n'))
-    width = max(len(s) for s in color._styles)
-    for label, effects in sorted(color._styles.items()):
+    width = max(len(s) for s in ui._styles)
+    for label, effects in sorted(ui._styles.items()):
         ui.write('%s' % label, label=label)
         if effects:
             # 50
diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -254,6 +254,7 @@ def _modesetup(ui):
     return None
 
 def configstyles(ui):
+    ui._styles.update(_styles)
     for status, cfgeffects in ui.configitems('color'):
         if '.' not in status or status.startswith(('color.', 'terminfo.')):
             continue
@@ -267,7 +268,7 @@ def configstyles(ui):
                     ui.warn(_("ignoring unknown color/effect %r "
                               "(configured in color.%s)\n")
                             % (e, status))
-            _styles[status] = ' '.join(good)
+            ui._styles[status] = ' '.join(good)
 
 def valideffect(ui, effect):
     'Determine if the effect is valid or not.'
@@ -321,7 +322,7 @@ def colorlabel(ui, msg, label):
     elif ui._colormode is not None:
         effects = []
         for l in label.split():
-            s = _styles.get(l, '')
+            s = ui._styles.get(l, '')
             if s:
                 effects.append(s)
             elif valideffect(ui, l):
@@ -443,7 +444,7 @@ if pycompat.osname == 'nt':
 
         # determine console attributes based on labels
         for l in label.split():
-            style = _styles.get(l, '')
+            style = ui._styles.get(l, '')
             for effect in style.split():
                 try:
                     attr = mapcolor(w32effects[effect], attr)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -158,6 +158,7 @@ class ui(object):
         # color mode: see mercurial/color.py for possible value
         self._colormode = None
         self._terminfoparams = {}
+        self._styles = {}
 
         if src:
             self.fout = src.fout
@@ -176,6 +177,7 @@ class ui(object):
             self.insecureconnections = src.insecureconnections
             self._colormode = src._colormode
             self._terminfoparams = src._terminfoparams.copy()
+            self._styles = src._styles.copy()
 
             self.fixconfig()
 


More information about the Mercurial-devel mailing list