# HG changeset patch
# User Gregory Szorc <gregory.sz...@gmail.com>
# Date 1499639997 25200
#      Sun Jul 09 15:39:57 2017 -0700
# Node ID 98c54db9407a7e0ba94a632aafdbbec3fc76fa8b
# Parent  129c9709980108beecfa1c02a52c679654de8b21
color: refactor how terminfo colors are defined

Terminfo colors are currently lumped together with terminfo control
and styling attributes. As part of implementing 16 and 256 color
support, it will help to differentiate terminfo colors from other
attributes. So this commit does that.

Colors are split to their own dict. A new function for obtaining
a dict of color name to color number has been implemented and code
has been refactored to use it.

The color dict preserves whether the color is user-defined so that
`hg debugcolors` output remains consistent. I'm not sure if this is
worth it. I kept the old behavior to avoid a BC break.

diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -32,18 +32,23 @@ try:
         'bold': (True, 'bold', ''),
         'invisible': (True, 'invis', ''),
         'italic': (True, 'sitm', ''),
-        'black': (False, curses.COLOR_BLACK, ''),
-        'red': (False, curses.COLOR_RED, ''),
-        'green': (False, curses.COLOR_GREEN, ''),
-        'yellow': (False, curses.COLOR_YELLOW, ''),
-        'blue': (False, curses.COLOR_BLUE, ''),
-        'magenta': (False, curses.COLOR_MAGENTA, ''),
-        'cyan': (False, curses.COLOR_CYAN, ''),
-        'white': (False, curses.COLOR_WHITE, ''),
     }
+
+    TERMINFO_COLOR_8 = {
+        'black': curses.COLOR_BLACK,
+        'red': curses.COLOR_RED,
+        'green': curses.COLOR_GREEN,
+        'yellow': curses.COLOR_YELLOW,
+        'blue': curses.COLOR_BLUE,
+        'magenta': curses.COLOR_MAGENTA,
+        'cyan': curses.COLOR_CYAN,
+        'white': curses.COLOR_WHITE,
+    }
+
 except ImportError:
     curses = None
     _baseterminfoparams = {}
+    TERMINFO_COLOR_8 = {}
 
 _enabledbydefault = True
 
@@ -132,6 +137,21 @@ except ImportError:
 def loadcolortable(ui, extname, colortable):
     _defaultstyles.update(colortable)
 
+def _terminfocolors(ui):
+    """Obtain defined terminfo colors as a dict.
+
+    Keys are color names. Values are tuples of (value, user-defined).
+    """
+    colors = {}
+    for color, value in TERMINFO_COLOR_8.items():
+        colors[color] = (value, False)
+
+    for key, value in ui.configitems('color'):
+        if key.startswith('color.'):
+            colors[key[6:]] = (int(value), True)
+
+    return colors
+
 def _terminfosetup(ui, mode):
     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
 
@@ -150,11 +170,11 @@ def _terminfosetup(ui, mode):
 
     ui._terminfoparams.update(_baseterminfoparams)
 
+    for color, value in _terminfocolors(ui).items():
+        ui._terminfoparams[color] = (False, value[0], '')
+
     for key, val in ui.configitems('color'):
-        if key.startswith('color.'):
-            newval = (False, int(val), '')
-            ui._terminfoparams[key[6:]] = newval
-        elif key.startswith('terminfo.'):
+        if key.startswith('terminfo.'):
             newval = (True, '', val.replace('\\E', '\x1b'))
             ui._terminfoparams[key[9:]] = newval
 
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -405,10 +405,11 @@ def _debugdisplaycolor(ui):
     for effect in color._activeeffects(ui).keys():
         ui._styles[effect] = effect
     if ui._terminfoparams:
+        for k, v in color._terminfocolors(ui).items():
+            ui._styles['color.%s' % k if v[1] else k] = k
+
         for k, v in ui.configitems('color'):
-            if k.startswith('color.'):
-                ui._styles[k] = k[6:]
-            elif k.startswith('terminfo.'):
+            if k.startswith('terminfo.'):
                 ui._styles[k] = k[9:]
     ui.write(_('available colors:\n'))
     # sort label with a '_' after the other to group '_background' entry.
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to