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

Pierre-Yves David pierre-yves.david at ens-lyon.org
Tue Feb 28 04:46:49 EST 2017



On 02/28/2017 08:23 AM, Martin von Zweigbergk wrote:
> On Mon, Feb 27, 2017 at 7:00 AM, Pierre-Yves David
> <pierre-yves.david at ens-lyon.org> wrote:
>> # 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 d520bd43c7b26c3017329a2f9563701e5641d62d
>> # Parent  8e725873d8e1bec5dbdd0c69cbfc12af54af6a6a
>> # 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.
>
> Shouldn't '_styles' be renamed '_basestyles' for consistency with
> '_terminfoparams'? Please fix indentation too when you do that.

Patch 7 is doing exactly that. It is in a different patch because the 
odd for external code using it is higher (eg: thg) so making the rename 
easy to bisect seemed like a good idea.


> Probably send a formatting patch early in the series in V2.
>
>>
>> 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
>> @@ -248,6 +248,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
>> @@ -261,7 +262,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.'
>> @@ -315,7 +316,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):
>> @@ -437,7 +438,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()
>>
>> _______________________________________________
>> Mercurial-devel mailing list
>> Mercurial-devel at mercurial-scm.org
>> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

-- 
Pierre-Yves David


More information about the Mercurial-devel mailing list