[PATCH 3 of 4] color: move hgext.color._styles to mercurial.color.style

Pierre-Yves David pierre-yves.david at ens-lyon.org
Wed Dec 21 11:23:18 EST 2016


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1479488976 -3600
#      Fri Nov 18 18:09:36 2016 +0100
# Node ID 764600c7f44086f0d6bbbaf31a668093999190ce
# Parent  a2f55e5d78ca779bd031a5ab6cd32167f4a6ffdf
# EXP-Topic color
color: move hgext.color._styles to mercurial.color.style

This is small first step to start moving the color infrastructure into core. The
current code of the color extensions is full of strange and debatable things,
we'll clean it up in the process as having things into core help the cleaning.

Moving _style was the simplest sensible move that is possible. It will also help
cleaning up the extension setup process in a later changesets.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -169,6 +169,7 @@ import os
 from mercurial.i18n import _
 from mercurial import (
     cmdutil,
+    color,
     commands,
     dispatch,
     extensions,
@@ -322,61 +323,6 @@ try:
 except ImportError:
     _terminfo_params = {}
 
-_styles = {'grep.match': 'red bold',
-           'grep.linenumber': 'green',
-           'grep.rev': 'green',
-           'grep.change': 'green',
-           'grep.sep': 'cyan',
-           'grep.filename': 'magenta',
-           'grep.user': 'magenta',
-           'grep.date': 'magenta',
-           'bookmarks.active': 'green',
-           'branches.active': 'none',
-           'branches.closed': 'black bold',
-           'branches.current': 'green',
-           'branches.inactive': 'none',
-           'diff.changed': 'white',
-           'diff.deleted': 'red',
-           'diff.diffline': 'bold',
-           'diff.extended': 'cyan bold',
-           'diff.file_a': 'red bold',
-           'diff.file_b': 'green bold',
-           'diff.hunk': 'magenta',
-           'diff.inserted': 'green',
-           'diff.tab': '',
-           'diff.trailingwhitespace': 'bold red_background',
-           'changeset.public' : '',
-           'changeset.draft' : '',
-           'changeset.secret' : '',
-           'diffstat.deleted': 'red',
-           'diffstat.inserted': 'green',
-           'histedit.remaining': 'red bold',
-           'ui.prompt': 'yellow',
-           'log.changeset': 'yellow',
-           'patchbomb.finalsummary': '',
-           'patchbomb.from': 'magenta',
-           'patchbomb.to': 'cyan',
-           'patchbomb.subject': 'green',
-           'patchbomb.diffstats': '',
-           'rebase.rebased': 'blue',
-           'rebase.remaining': 'red bold',
-           'resolve.resolved': 'green bold',
-           'resolve.unresolved': 'red bold',
-           'shelve.age': 'cyan',
-           'shelve.newest': 'green bold',
-           'shelve.name': 'blue bold',
-           'status.added': 'green bold',
-           'status.clean': 'none',
-           'status.copied': 'none',
-           'status.deleted': 'cyan bold underline',
-           'status.ignored': 'black bold',
-           'status.modified': 'blue bold',
-           'status.removed': 'red bold',
-           'status.unknown': 'magenta bold underline',
-           'tags.normal': 'green',
-           'tags.local': 'black bold'}
-
-
 def _effect_str(effect):
     '''Helper function for render_effects().'''
 
@@ -414,7 +360,7 @@ def render_effects(text, effects):
 
 def extstyles():
     for name, ext in extensions.extensions():
-        _styles.update(getattr(ext, 'colortable', {}))
+        color._styles.update(getattr(ext, 'colortable', {}))
 
 def valideffect(effect):
     'Determine if the effect is valid or not.'
@@ -439,7 +385,7 @@ def configstyles(ui):
                     ui.warn(_("ignoring unknown color/effect %r "
                               "(configured in color.%s)\n")
                             % (e, status))
-            _styles[status] = ' '.join(good)
+            color._styles[status] = ' '.join(good)
 
 class colorui(uimod.ui):
     _colormode = 'ansi'
@@ -492,7 +438,7 @@ class colorui(uimod.ui):
 
         effects = []
         for l in label.split():
-            s = _styles.get(l, '')
+            s = color._styles.get(l, '')
             if s:
                 effects.append(s)
             elif valideffect(l):
@@ -545,31 +491,31 @@ def debugcolor(ui, repo, **opts):
         return _debugdisplaycolor(ui)
 
 def _debugdisplaycolor(ui):
-    oldstyle = _styles.copy()
+    oldstyle = color._styles.copy()
     try:
-        _styles.clear()
+        color._styles.clear()
         for effect in _effects.keys():
-            _styles[effect] = effect
+            color._styles[effect] = effect
         if _terminfo_params:
             for k, v in ui.configitems('color'):
                 if k.startswith('color.'):
-                    _styles[k] = k[6:]
+                    color._styles[k] = k[6:]
                 elif k.startswith('terminfo.'):
-                    _styles[k] = k[9:]
+                    color._styles[k] = k[9:]
         ui.write(_('available colors:\n'))
         # sort label with a '_' after the other to group '_background' entry.
-        items = sorted(_styles.items(),
+        items = sorted(color._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:
-        _styles.clear()
-        _styles.update(oldstyle)
+        color._styles.clear()
+        color._styles.update(oldstyle)
 
 def _debugdisplaystyle(ui):
     ui.write(_('available style:\n'))
-    width = max(len(s) for s in _styles)
-    for label, effects in sorted(_styles.items()):
+    width = max(len(s) for s in color._styles)
+    for label, effects in sorted(color._styles.items()):
         ui.write('%s' % label, label=label)
         if effects:
             # 50
@@ -686,7 +632,7 @@ else:
 
         # determine console attributes based on labels
         for l in label.split():
-            style = _styles.get(l, '')
+            style = color._styles.get(l, '')
             for effect in style.split():
                 try:
                     attr = mapcolor(w32effects[effect], attr)
diff --git a/mercurial/color.py b/mercurial/color.py
new file mode 100644
--- /dev/null
+++ b/mercurial/color.py
@@ -0,0 +1,62 @@
+# utility for color output for Mercurial commands
+#
+# Copyright (C) 2007 Kevin Christen <kevin.christen at gmail.com> and other
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+_styles = {'grep.match': 'red bold',
+           'grep.linenumber': 'green',
+           'grep.rev': 'green',
+           'grep.change': 'green',
+           'grep.sep': 'cyan',
+           'grep.filename': 'magenta',
+           'grep.user': 'magenta',
+           'grep.date': 'magenta',
+           'bookmarks.active': 'green',
+           'branches.active': 'none',
+           'branches.closed': 'black bold',
+           'branches.current': 'green',
+           'branches.inactive': 'none',
+           'diff.changed': 'white',
+           'diff.deleted': 'red',
+           'diff.diffline': 'bold',
+           'diff.extended': 'cyan bold',
+           'diff.file_a': 'red bold',
+           'diff.file_b': 'green bold',
+           'diff.hunk': 'magenta',
+           'diff.inserted': 'green',
+           'diff.tab': '',
+           'diff.trailingwhitespace': 'bold red_background',
+           'changeset.public' : '',
+           'changeset.draft' : '',
+           'changeset.secret' : '',
+           'diffstat.deleted': 'red',
+           'diffstat.inserted': 'green',
+           'histedit.remaining': 'red bold',
+           'ui.prompt': 'yellow',
+           'log.changeset': 'yellow',
+           'patchbomb.finalsummary': '',
+           'patchbomb.from': 'magenta',
+           'patchbomb.to': 'cyan',
+           'patchbomb.subject': 'green',
+           'patchbomb.diffstats': '',
+           'rebase.rebased': 'blue',
+           'rebase.remaining': 'red bold',
+           'resolve.resolved': 'green bold',
+           'resolve.unresolved': 'red bold',
+           'shelve.age': 'cyan',
+           'shelve.newest': 'green bold',
+           'shelve.name': 'blue bold',
+           'status.added': 'green bold',
+           'status.clean': 'none',
+           'status.copied': 'none',
+           'status.deleted': 'cyan bold underline',
+           'status.ignored': 'black bold',
+           'status.modified': 'blue bold',
+           'status.removed': 'red bold',
+           'status.unknown': 'magenta bold underline',
+           'tags.normal': 'green',
+           'tags.local': 'black bold'}


More information about the Mercurial-devel mailing list