Patchwork [5,of,8,V2] color: move the dict with terminfo parameters on the ui object

login
register
mail settings
Submitter Pierre-Yves David
Date Feb. 28, 2017, 10:54 a.m.
Message ID <244883d4b3dcd860631d.1488279269@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/18832/
State Accepted
Delegated to: Martin von Zweigbergk
Headers show

Comments

Pierre-Yves David - Feb. 28, 2017, 10:54 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1488031251 -3600
#      Sat Feb 25 15:00:51 2017 +0100
# Node ID 244883d4b3dcd860631dde2fb2610ad3d8b52139
# Parent  39d07e7ccfaef5f4596ce919846fcccb207ec60f
# EXP-Topic color
color: move the dict with terminfo parameters on the ui object

This dictionnary is affected by the content of the config, so we should have
one for each ui config.

We rename the global dict to '_baseterminfoparams' to make the situation
clearer.

Patch

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -212,7 +212,7 @@  def _debugdisplaycolor(ui):
         color._styles.clear()
         for effect in color._effects.keys():
             color._styles[effect] = effect
-        if color._terminfo_params:
+        if ui._terminfoparams:
             for k, v in ui.configitems('color'):
                 if k.startswith('color.'):
                     color._styles[k] = k[6:]
diff --git a/mercurial/color.py b/mercurial/color.py
--- a/mercurial/color.py
+++ b/mercurial/color.py
@@ -19,7 +19,7 @@  try:
     import curses
     # Mapping from effect name to terminfo attribute name (or raw code) or
     # color number.  This will also force-load the curses module.
-    _terminfo_params = {
+    _baseterminfoparams = {
         'none': (True, 'sgr0', ''),
         'standout': (True, 'smso', ''),
         'underline': (True, 'smul', ''),
@@ -41,7 +41,7 @@  try:
     }
 except ImportError:
     curses = None
-    _terminfo_params = {}
+    _baseterminfoparams = {}
 
 # allow the extensions to change the default
 _enabledbydefault = False
@@ -140,35 +140,36 @@  def _terminfosetup(ui, mode):
     # Otherwise, see what the config file says.
     if mode not in ('auto', 'terminfo'):
         return
+    ui._terminfoparams.update(_baseterminfoparams)
 
     for key, val in ui.configitems('color'):
         if key.startswith('color.'):
             newval = (False, int(val), '')
-            _terminfo_params[key[6:]] = newval
+            ui._terminfoparams[key[6:]] = newval
         elif key.startswith('terminfo.'):
             newval = (True, '', val.replace('\\E', '\x1b'))
-            _terminfo_params[key[9:]] = newval
+            ui._terminfoparams[key[9:]] = newval
     try:
         curses.setupterm()
     except curses.error as e:
-        _terminfo_params.clear()
+        ui._terminfoparams.clear()
         return
 
-    for key, (b, e, c) in _terminfo_params.items():
+    for key, (b, e, c) in ui._terminfoparams.items():
         if not b:
             continue
         if not c and not curses.tigetstr(e):
             # Most terminals don't support dim, invis, etc, so don't be
             # noisy and use ui.debug().
             ui.debug("no terminfo entry for %s\n" % e)
-            del _terminfo_params[key]
+            del ui._terminfoparams[key]
     if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
         # Only warn about missing terminfo entries if we explicitly asked for
         # terminfo mode.
         if mode == "terminfo":
             ui.warn(_("no terminfo entry for setab/setaf: reverting to "
               "ECMA-48 color\n"))
-        _terminfo_params.clear()
+        ui._terminfoparams.clear()
 
 def setup(ui):
     """configure color on a ui
@@ -232,16 +233,16 @@  def _modesetup(ui):
             ui.warn(_('warning: failed to set color mode to %s\n') % mode)
 
     if realmode == 'win32':
-        _terminfo_params.clear()
+        ui._terminfoparams.clear()
         if not w32effects:
             modewarn()
             return None
         _effects.update(w32effects)
     elif realmode == 'ansi':
-        _terminfo_params.clear()
+        ui._terminfoparams.clear()
     elif realmode == 'terminfo':
         _terminfosetup(ui, mode)
-        if not _terminfo_params:
+        if not ui._terminfoparams:
             ## FIXME Shouldn't we return None in this case too?
             modewarn()
             realmode = 'ansi'
@@ -270,9 +271,9 @@  def configstyles(ui):
 
 def valideffect(ui, effect):
     'Determine if the effect is valid or not.'
-    return ((not _terminfo_params and effect in _effects)
-             or (effect in _terminfo_params
-                 or effect[:-11] in _terminfo_params))
+    return ((not ui._terminfoparams and effect in _effects)
+             or (effect in ui._terminfoparams
+                 or effect[:-11] in ui._terminfoparams))
 
 def _effect_str(ui, effect):
     '''Helper function for render_effects().'''
@@ -282,7 +283,7 @@  def _effect_str(ui, effect):
         bg = True
         effect = effect[:-11]
     try:
-        attr, val, termcode = _terminfo_params[effect]
+        attr, val, termcode = ui._terminfoparams[effect]
     except KeyError:
         return ''
     if attr:
@@ -299,7 +300,7 @@  def _render_effects(ui, text, effects):
     'Wrap text in commands to turn on each effect.'
     if not text:
         return text
-    if _terminfo_params:
+    if ui._terminfoparams:
         start = ''.join(_effect_str(ui, effect)
                         for effect in ['none'] + effects.split())
         stop = _effect_str(ui, 'none')
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -157,6 +157,7 @@  class ui(object):
         self.logblockedtimes = False
         # color mode: see mercurial/color.py for possible value
         self._colormode = None
+        self._terminfoparams = {}
 
         if src:
             self.fout = src.fout
@@ -174,6 +175,7 @@  class ui(object):
             self.callhooks = src.callhooks
             self.insecureconnections = src.insecureconnections
             self._colormode = src._colormode
+            self._terminfoparams = src._terminfoparams.copy()
 
             self.fixconfig()