[PATCH 1 of 8 simple] color: spread '_effect' values for readability

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1 of 8 simple] color: spread '_effect' values for readability

Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1479491019 -3600
#      Fri Nov 18 18:43:39 2016 +0100
# Node ID 42d4b49b39d6879065209ea3ca71f1e3fa88fcbc
# Parent  354020079723e02ad6db68f58ef26eb7ebd005a8
# EXP-Topic color
color: spread '_effect' values for readability

We move to our "usual" one value per line style.

diff -r 354020079723 -r 42d4b49b39d6 hgext/color.py
--- a/hgext/color.py Tue Feb 07 17:33:35 2017 +0100
+++ b/hgext/color.py Fri Nov 18 18:43:39 2016 +0100
@@ -187,13 +187,28 @@ command = cmdutil.command(cmdtable)
 testedwith = 'ships-with-hg-core'
 
 # start and stop parameters for effects
-_effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
-            'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
-            'italic': 3, 'underline': 4, 'inverse': 7, 'dim': 2,
-            'black_background': 40, 'red_background': 41,
-            'green_background': 42, 'yellow_background': 43,
-            'blue_background': 44, 'purple_background': 45,
-            'cyan_background': 46, 'white_background': 47}
+_effects = {'none': 0,
+            'black': 30,
+            'red': 31,
+            'green': 32,
+            'yellow': 33,
+            'blue': 34,
+            'magenta': 35,
+            'cyan': 36,
+            'white': 37,
+            'bold': 1,
+            'italic': 3,
+            'underline': 4,
+            'inverse': 7,
+            'dim': 2,
+            'black_background': 40,
+            'red_background': 41,
+            'green_background': 42,
+            'yellow_background': 43,
+            'blue_background': 44,
+            'purple_background': 45,
+            'cyan_background': 46,
+            'white_background': 47}
 
 def _terminfosetup(ui, mode):
     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 2 of 8 simple] color: move '_effect' mapping into core

Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1479491318 -3600
#      Fri Nov 18 18:48:38 2016 +0100
# Node ID fd46adc1f3ab7bb0800a9ff6482f7554bad8637e
# Parent  42d4b49b39d6879065209ea3ca71f1e3fa88fcbc
# EXP-Topic color
color: move '_effect' mapping into core

This is the second things we can move into core safely.

diff -r 42d4b49b39d6 -r fd46adc1f3ab hgext/color.py
--- a/hgext/color.py Fri Nov 18 18:43:39 2016 +0100
+++ b/hgext/color.py Fri Nov 18 18:48:38 2016 +0100
@@ -186,30 +186,6 @@ command = cmdutil.command(cmdtable)
 # leave the attribute unspecified.
 testedwith = 'ships-with-hg-core'
 
-# start and stop parameters for effects
-_effects = {'none': 0,
-            'black': 30,
-            'red': 31,
-            'green': 32,
-            'yellow': 33,
-            'blue': 34,
-            'magenta': 35,
-            'cyan': 36,
-            'white': 37,
-            'bold': 1,
-            'italic': 3,
-            'underline': 4,
-            'inverse': 7,
-            'dim': 2,
-            'black_background': 40,
-            'red_background': 41,
-            'green_background': 42,
-            'yellow_background': 43,
-            'blue_background': 44,
-            'purple_background': 45,
-            'cyan_background': 46,
-            'white_background': 47}
-
 def _terminfosetup(ui, mode):
     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
 
@@ -298,7 +274,7 @@ def _modesetup(ui, coloropt):
         if not w32effects:
             modewarn()
             return None
-        _effects.update(w32effects)
+        color._effects.update(w32effects)
     elif realmode == 'ansi':
         _terminfo_params.clear()
     elif realmode == 'terminfo':
@@ -365,9 +341,9 @@ def render_effects(text, effects):
     if not text:
         return text
     if not _terminfo_params:
-        start = [str(_effects[e]) for e in ['none'] + effects.split()]
+        start = [str(color._effects[e]) for e in ['none'] + effects.split()]
         start = '\033[' + ';'.join(start) + 'm'
-        stop = '\033[' + str(_effects['none']) + 'm'
+        stop = '\033[' + str(color._effects['none']) + 'm'
     else:
         start = ''.join(_effect_str(effect)
                         for effect in ['none'] + effects.split())
@@ -377,7 +353,7 @@ def render_effects(text, effects):
 def valideffect(effect):
     'Determine if the effect is valid or not.'
     good = False
-    if not _terminfo_params and effect in _effects:
+    if not _terminfo_params and effect in color._effects:
         good = True
     elif effect in _terminfo_params or effect[:-11] in _terminfo_params:
         good = True
@@ -505,7 +481,7 @@ def _debugdisplaycolor(ui):
     oldstyle = color._styles.copy()
     try:
         color._styles.clear()
-        for effect in _effects.keys():
+        for effect in color._effects.keys():
             color._styles[effect] = effect
         if _terminfo_params:
             for k, v in ui.configitems('color'):
diff -r 42d4b49b39d6 -r fd46adc1f3ab mercurial/color.py
--- a/mercurial/color.py Fri Nov 18 18:43:39 2016 +0100
+++ b/mercurial/color.py Fri Nov 18 18:48:38 2016 +0100
@@ -7,6 +7,30 @@
 
 from __future__ import absolute_import
 
+# start and stop parameters for effects
+_effects = {'none': 0,
+            'black': 30,
+            'red': 31,
+            'green': 32,
+            'yellow': 33,
+            'blue': 34,
+            'magenta': 35,
+            'cyan': 36,
+            'white': 37,
+            'bold': 1,
+            'italic': 3,
+            'underline': 4,
+            'inverse': 7,
+            'dim': 2,
+            'black_background': 40,
+            'red_background': 41,
+            'green_background': 42,
+            'yellow_background': 43,
+            'blue_background': 44,
+            'purple_background': 45,
+            'cyan_background': 46,
+            'white_background': 47}
+
 _styles = {'grep.match': 'red bold',
            'grep.linenumber': 'green',
            'grep.rev': 'green',
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 3 of 8 simple] color: move '_terminfo_params' into the core 'color' module

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482369803 -3600
#      Thu Dec 22 02:23:23 2016 +0100
# Node ID b0ca5e9e8d9a6afa98accda7fcd5433ff19ddd1e
# Parent  fd46adc1f3ab7bb0800a9ff6482f7554bad8637e
# EXP-Topic color
color: move '_terminfo_params' into the core 'color' module

On step closer to have color in core.

diff -r fd46adc1f3ab -r b0ca5e9e8d9a hgext/color.py
--- a/hgext/color.py Fri Nov 18 18:48:38 2016 +0100
+++ b/hgext/color.py Thu Dec 22 02:23:23 2016 +0100
@@ -164,6 +164,12 @@ If ``pagermode`` is not defined, the ``m
 
 from __future__ import absolute_import
 
+try:
+    import curses
+    curses.COLOR_BLACK # force import
+except ImportError:
+    curses = None
+
 from mercurial.i18n import _
 from mercurial import (
     cmdutil,
@@ -190,40 +196,41 @@ def _terminfosetup(ui, mode):
     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
 
     # If we failed to load curses, we go ahead and return.
-    if not _terminfo_params:
+    if not color._terminfo_params:
         return
     # Otherwise, see what the config file says.
     if mode not in ('auto', 'terminfo'):
         return
 
-    _terminfo_params.update((key[6:], (False, int(val), ''))
+    color._terminfo_params.update((key[6:], (False, int(val), ''))
         for key, val in ui.configitems('color')
         if key.startswith('color.'))
-    _terminfo_params.update((key[9:], (True, '', val.replace('\\E', '\x1b')))
+    color._terminfo_params.update((key[9:],
+                                   (True, '', val.replace('\\E', '\x1b')))
         for key, val in ui.configitems('color')
         if key.startswith('terminfo.'))
 
     try:
         curses.setupterm()
     except curses.error as e:
-        _terminfo_params.clear()
+        color._terminfo_params.clear()
         return
 
-    for key, (b, e, c) in _terminfo_params.items():
+    for key, (b, e, c) in color._terminfo_params.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 color._terminfo_params[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()
+        color._terminfo_params.clear()
 
 def _modesetup(ui, coloropt):
     if coloropt == 'debug':
@@ -270,16 +277,16 @@ def _modesetup(ui, coloropt):
             ui.warn(_('warning: failed to set color mode to %s\n') % mode)
 
     if realmode == 'win32':
-        _terminfo_params.clear()
+        color._terminfo_params.clear()
         if not w32effects:
             modewarn()
             return None
         color._effects.update(w32effects)
     elif realmode == 'ansi':
-        _terminfo_params.clear()
+        color._terminfo_params.clear()
     elif realmode == 'terminfo':
         _terminfosetup(ui, mode)
-        if not _terminfo_params:
+        if not color._terminfo_params:
             ## FIXME Shouldn't we return None in this case too?
             modewarn()
             realmode = 'ansi'
@@ -290,31 +297,6 @@ def _modesetup(ui, coloropt):
         return realmode
     return None
 
-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 = {'none': (True, 'sgr0', ''),
-                        'standout': (True, 'smso', ''),
-                        'underline': (True, 'smul', ''),
-                        'reverse': (True, 'rev', ''),
-                        'inverse': (True, 'rev', ''),
-                        'blink': (True, 'blink', ''),
-                        'dim': (True, 'dim', ''),
-                        '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, '')}
-except ImportError:
-    _terminfo_params = {}
-
 def _effect_str(effect):
     '''Helper function for render_effects().'''
 
@@ -323,7 +305,7 @@ def _effect_str(effect):
         bg = True
         effect = effect[:-11]
     try:
-        attr, val, termcode = _terminfo_params[effect]
+        attr, val, termcode = color._terminfo_params[effect]
     except KeyError:
         return ''
     if attr:
@@ -340,7 +322,7 @@ def render_effects(text, effects):
     'Wrap text in commands to turn on each effect.'
     if not text:
         return text
-    if not _terminfo_params:
+    if not color._terminfo_params:
         start = [str(color._effects[e]) for e in ['none'] + effects.split()]
         start = '\033[' + ';'.join(start) + 'm'
         stop = '\033[' + str(color._effects['none']) + 'm'
@@ -353,9 +335,10 @@ def render_effects(text, effects):
 def valideffect(effect):
     'Determine if the effect is valid or not.'
     good = False
-    if not _terminfo_params and effect in color._effects:
+    if not color._terminfo_params and effect in color._effects:
         good = True
-    elif effect in _terminfo_params or effect[:-11] in _terminfo_params:
+    elif (effect in color._terminfo_params
+          or effect[:-11] in color._terminfo_params):
         good = True
     return good
 
@@ -483,7 +466,7 @@ def _debugdisplaycolor(ui):
         color._styles.clear()
         for effect in color._effects.keys():
             color._styles[effect] = effect
-        if _terminfo_params:
+        if color._terminfo_params:
             for k, v in ui.configitems('color'):
                 if k.startswith('color.'):
                     color._styles[k] = k[6:]
diff -r fd46adc1f3ab -r b0ca5e9e8d9a mercurial/color.py
--- a/mercurial/color.py Fri Nov 18 18:48:38 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:23:23 2016 +0100
@@ -7,6 +7,32 @@
 
 from __future__ import absolute_import
 
+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 = {'none': (True, 'sgr0', ''),
+                        'standout': (True, 'smso', ''),
+                        'underline': (True, 'smul', ''),
+                        'reverse': (True, 'rev', ''),
+                        'inverse': (True, 'rev', ''),
+                        'blink': (True, 'blink', ''),
+                        'dim': (True, 'dim', ''),
+                        '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, '')}
+except ImportError:
+    curses = None
+    _terminfo_params = {}
+
 # start and stop parameters for effects
 _effects = {'none': 0,
             'black': 30,
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 4 of 8 simple] color: move 'valideffect' function into the core module

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482370010 -3600
#      Thu Dec 22 02:26:50 2016 +0100
# Node ID bb59bec58bcdf57a3aeb1a9cd4a9b5b004c7c139
# Parent  b0ca5e9e8d9a6afa98accda7fcd5433ff19ddd1e
# EXP-Topic color
color: move 'valideffect' function into the core module

diff -r b0ca5e9e8d9a -r bb59bec58bcd hgext/color.py
--- a/hgext/color.py Thu Dec 22 02:23:23 2016 +0100
+++ b/hgext/color.py Thu Dec 22 02:26:50 2016 +0100
@@ -332,16 +332,6 @@ def render_effects(text, effects):
         stop = _effect_str('none')
     return ''.join([start, text, stop])
 
-def valideffect(effect):
-    'Determine if the effect is valid or not.'
-    good = False
-    if not color._terminfo_params and effect in color._effects:
-        good = True
-    elif (effect in color._terminfo_params
-          or effect[:-11] in color._terminfo_params):
-        good = True
-    return good
-
 def configstyles(ui):
     for status, cfgeffects in ui.configitems('color'):
         if '.' not in status or status.startswith(('color.', 'terminfo.')):
@@ -350,7 +340,7 @@ def configstyles(ui):
         if cfgeffects:
             good = []
             for e in cfgeffects:
-                if valideffect(e):
+                if color.valideffect(e):
                     good.append(e)
                 else:
                     ui.warn(_("ignoring unknown color/effect %r "
@@ -412,7 +402,7 @@ class colorui(uimod.ui):
             s = color._styles.get(l, '')
             if s:
                 effects.append(s)
-            elif valideffect(l):
+            elif color.valideffect(l):
                 effects.append(l)
         effects = ' '.join(effects)
         if effects:
diff -r b0ca5e9e8d9a -r bb59bec58bcd mercurial/color.py
--- a/mercurial/color.py Thu Dec 22 02:23:23 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:26:50 2016 +0100
@@ -113,3 +113,13 @@ except ImportError:
 
 def loadcolortable(ui, extname, colortable):
     _styles.update(colortable)
+
+def valideffect(effect):
+    'Determine if the effect is valid or not.'
+    good = False
+    if not _terminfo_params and effect in _effects:
+        good = True
+    elif (effect in _terminfo_params
+          or effect[:-11] in _terminfo_params):
+        good = True
+    return good
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 5 of 8 simple] color: rework conditional 'valideffect'

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482370203 -3600
#      Thu Dec 22 02:30:03 2016 +0100
# Node ID 07618a3840fbce3ab1084765d94ac8d7a9380ff8
# Parent  bb59bec58bcdf57a3aeb1a9cd4a9b5b004c7c139
# EXP-Topic color
color: rework conditional 'valideffect'

Not very important, but the full conditional is not that hard to follow and
having it unified make the function role a bit clearer in my opinion.

diff -r bb59bec58bcd -r 07618a3840fb mercurial/color.py
--- a/mercurial/color.py Thu Dec 22 02:26:50 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:30:03 2016 +0100
@@ -116,10 +116,6 @@ def loadcolortable(ui, extname, colortab
 
 def valideffect(effect):
     'Determine if the effect is valid or not.'
-    good = False
-    if not _terminfo_params and effect in _effects:
-        good = True
-    elif (effect in _terminfo_params
-          or effect[:-11] in _terminfo_params):
-        good = True
-    return good
+    return ((not _terminfo_params and effect in _effects)
+             or (effect in _terminfo_params
+                 or effect[:-11] in _terminfo_params))
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 6 of 8 simple] color: move configstyles into the core module

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482370462 -3600
#      Thu Dec 22 02:34:22 2016 +0100
# Node ID 93992a625b7141ab5ba0a6cdd5db1aac3ab75361
# Parent  07618a3840fbce3ab1084765d94ac8d7a9380ff8
# EXP-Topic color
color: move configstyles into the core module

The extension is getting thinner as we speak!

diff -r 07618a3840fb -r 93992a625b71 hgext/color.py
--- a/hgext/color.py Thu Dec 22 02:30:03 2016 +0100
+++ b/hgext/color.py Thu Dec 22 02:34:22 2016 +0100
@@ -332,22 +332,6 @@ def render_effects(text, effects):
         stop = _effect_str('none')
     return ''.join([start, text, stop])
 
-def configstyles(ui):
-    for status, cfgeffects in ui.configitems('color'):
-        if '.' not in status or status.startswith(('color.', 'terminfo.')):
-            continue
-        cfgeffects = ui.configlist('color', status)
-        if cfgeffects:
-            good = []
-            for e in cfgeffects:
-                if color.valideffect(e):
-                    good.append(e)
-                else:
-                    ui.warn(_("ignoring unknown color/effect %r "
-                              "(configured in color.%s)\n")
-                            % (e, status))
-            color._styles[status] = ' '.join(good)
-
 class colorui(uimod.ui):
     _colormode = 'ansi'
     def write(self, *args, **opts):
@@ -420,7 +404,7 @@ def uisetup(ui):
         mode = _modesetup(ui_, opts['color'])
         colorui._colormode = mode
         if mode and mode != 'debug':
-            configstyles(ui_)
+            color.configstyles(ui_)
         return orig(ui_, opts, cmd, cmdfunc)
     def colorgit(orig, gitsub, commands, env=None, stream=False, cwd=None):
         if gitsub.ui._colormode and len(commands) and commands[0] == "diff":
diff -r 07618a3840fb -r 93992a625b71 mercurial/color.py
--- a/mercurial/color.py Thu Dec 22 02:30:03 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:34:22 2016 +0100
@@ -7,6 +7,8 @@
 
 from __future__ import absolute_import
 
+from .i18n import _
+
 try:
     import curses
     # Mapping from effect name to terminfo attribute name (or raw code) or
@@ -114,6 +116,22 @@ except ImportError:
 def loadcolortable(ui, extname, colortable):
     _styles.update(colortable)
 
+def configstyles(ui):
+    for status, cfgeffects in ui.configitems('color'):
+        if '.' not in status or status.startswith(('color.', 'terminfo.')):
+            continue
+        cfgeffects = ui.configlist('color', status)
+        if cfgeffects:
+            good = []
+            for e in cfgeffects:
+                if valideffect(e):
+                    good.append(e)
+                else:
+                    ui.warn(_("ignoring unknown color/effect %r "
+                              "(configured in color.%s)\n")
+                            % (e, status))
+            _styles[status] = ' '.join(good)
+
 def valideffect(effect):
     'Determine if the effect is valid or not.'
     return ((not _terminfo_params and effect in _effects)
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 7 of 8 simple] color: move '_effect_str' function into the core module

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482370638 -3600
#      Thu Dec 22 02:37:18 2016 +0100
# Node ID 93e658cd6e685593ddc94d2b46ba596b1bb0d143
# Parent  93992a625b7141ab5ba0a6cdd5db1aac3ab75361
# EXP-Topic color
color: move '_effect_str' function into the core module

diff -r 93992a625b71 -r 93e658cd6e68 hgext/color.py
--- a/hgext/color.py Thu Dec 22 02:34:22 2016 +0100
+++ b/hgext/color.py Thu Dec 22 02:37:18 2016 +0100
@@ -297,27 +297,6 @@ def _modesetup(ui, coloropt):
         return realmode
     return None
 
-def _effect_str(effect):
-    '''Helper function for render_effects().'''
-
-    bg = False
-    if effect.endswith('_background'):
-        bg = True
-        effect = effect[:-11]
-    try:
-        attr, val, termcode = color._terminfo_params[effect]
-    except KeyError:
-        return ''
-    if attr:
-        if termcode:
-            return termcode
-        else:
-            return curses.tigetstr(val)
-    elif bg:
-        return curses.tparm(curses.tigetstr('setab'), val)
-    else:
-        return curses.tparm(curses.tigetstr('setaf'), val)
-
 def render_effects(text, effects):
     'Wrap text in commands to turn on each effect.'
     if not text:
@@ -327,9 +306,9 @@ def render_effects(text, effects):
         start = '\033[' + ';'.join(start) + 'm'
         stop = '\033[' + str(color._effects['none']) + 'm'
     else:
-        start = ''.join(_effect_str(effect)
+        start = ''.join(color._effect_str(effect)
                         for effect in ['none'] + effects.split())
-        stop = _effect_str('none')
+        stop = color._effect_str('none')
     return ''.join([start, text, stop])
 
 class colorui(uimod.ui):
diff -r 93992a625b71 -r 93e658cd6e68 mercurial/color.py
--- a/mercurial/color.py Thu Dec 22 02:34:22 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:37:18 2016 +0100
@@ -137,3 +137,24 @@ def valideffect(effect):
     return ((not _terminfo_params and effect in _effects)
              or (effect in _terminfo_params
                  or effect[:-11] in _terminfo_params))
+
+def _effect_str(effect):
+    '''Helper function for render_effects().'''
+
+    bg = False
+    if effect.endswith('_background'):
+        bg = True
+        effect = effect[:-11]
+    try:
+        attr, val, termcode = _terminfo_params[effect]
+    except KeyError:
+        return ''
+    if attr:
+        if termcode:
+            return termcode
+        else:
+            return curses.tigetstr(val)
+    elif bg:
+        return curses.tparm(curses.tigetstr('setab'), val)
+    else:
+        return curses.tparm(curses.tigetstr('setaf'), val)
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 8 of 8 simple] color: move the '_render_effects' function to the core module

Pierre-Yves David-2
In reply to this post by Pierre-Yves David-2
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1482370733 -3600
#      Thu Dec 22 02:38:53 2016 +0100
# Node ID f0f04ac8d4b17345b5375e4b11d895e7c22eb6ab
# Parent  93e658cd6e685593ddc94d2b46ba596b1bb0d143
# EXP-Topic color
color: move the '_render_effects' function to the core module

diff -r 93e658cd6e68 -r f0f04ac8d4b1 hgext/color.py
--- a/hgext/color.py Thu Dec 22 02:37:18 2016 +0100
+++ b/hgext/color.py Thu Dec 22 02:38:53 2016 +0100
@@ -297,20 +297,6 @@ def _modesetup(ui, coloropt):
         return realmode
     return None
 
-def render_effects(text, effects):
-    'Wrap text in commands to turn on each effect.'
-    if not text:
-        return text
-    if not color._terminfo_params:
-        start = [str(color._effects[e]) for e in ['none'] + effects.split()]
-        start = '\033[' + ';'.join(start) + 'm'
-        stop = '\033[' + str(color._effects['none']) + 'm'
-    else:
-        start = ''.join(color._effect_str(effect)
-                        for effect in ['none'] + effects.split())
-        stop = color._effect_str('none')
-    return ''.join([start, text, stop])
-
 class colorui(uimod.ui):
     _colormode = 'ansi'
     def write(self, *args, **opts):
@@ -369,7 +355,7 @@ class colorui(uimod.ui):
                 effects.append(l)
         effects = ' '.join(effects)
         if effects:
-            return '\n'.join([render_effects(line, effects)
+            return '\n'.join([color._render_effects(line, effects)
                               for line in msg.split('\n')])
         return msg
 
diff -r 93e658cd6e68 -r f0f04ac8d4b1 mercurial/color.py
--- a/mercurial/color.py Thu Dec 22 02:37:18 2016 +0100
+++ b/mercurial/color.py Thu Dec 22 02:38:53 2016 +0100
@@ -158,3 +158,17 @@ def _effect_str(effect):
         return curses.tparm(curses.tigetstr('setab'), val)
     else:
         return curses.tparm(curses.tigetstr('setaf'), val)
+
+def _render_effects(text, effects):
+    'Wrap text in commands to turn on each effect.'
+    if not text:
+        return text
+    if not _terminfo_params:
+        start = [str(_effects[e]) for e in ['none'] + effects.split()]
+        start = '\033[' + ';'.join(start) + 'm'
+        stop = '\033[' + str(_effects['none']) + 'm'
+    else:
+        start = ''.join(_effect_str(effect)
+                        for effect in ['none'] + effects.split())
+        stop = _effect_str('none')
+    return ''.join([start, text, stop])
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 1 of 8 simple] color: spread '_effect' values for readability

Martin von Zweigbergk via Mercurial-devel
In reply to this post by Pierre-Yves David-2
On Wed, Feb 15, 2017 at 3:06 AM, Pierre-Yves David
<[hidden email]> wrote:

> # HG changeset patch
> # User Pierre-Yves David <[hidden email]>
> # Date 1479491019 -3600
> #      Fri Nov 18 18:43:39 2016 +0100
> # Node ID 42d4b49b39d6879065209ea3ca71f1e3fa88fcbc
> # Parent  354020079723e02ad6db68f58ef26eb7ebd005a8
> # EXP-Topic color
> color: spread '_effect' values for readability
>
> We move to our "usual" one value per line style.

Nit: the even more usual style (by a factor 2-2.5, it seems) is to put
the values on separate lines from the curly braces. But I won't let
that stop the patch, of course.

>
> diff -r 354020079723 -r 42d4b49b39d6 hgext/color.py
> --- a/hgext/color.py    Tue Feb 07 17:33:35 2017 +0100
> +++ b/hgext/color.py    Fri Nov 18 18:43:39 2016 +0100
> @@ -187,13 +187,28 @@ command = cmdutil.command(cmdtable)
>  testedwith = 'ships-with-hg-core'
>
>  # start and stop parameters for effects
> -_effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33,
> -            'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1,
> -            'italic': 3, 'underline': 4, 'inverse': 7, 'dim': 2,
> -            'black_background': 40, 'red_background': 41,
> -            'green_background': 42, 'yellow_background': 43,
> -            'blue_background': 44, 'purple_background': 45,
> -            'cyan_background': 46, 'white_background': 47}
> +_effects = {'none': 0,
> +            'black': 30,
> +            'red': 31,
> +            'green': 32,
> +            'yellow': 33,
> +            'blue': 34,
> +            'magenta': 35,
> +            'cyan': 36,
> +            'white': 37,
> +            'bold': 1,
> +            'italic': 3,
> +            'underline': 4,
> +            'inverse': 7,
> +            'dim': 2,
> +            'black_background': 40,
> +            'red_background': 41,
> +            'green_background': 42,
> +            'yellow_background': 43,
> +            'blue_background': 44,
> +            'purple_background': 45,
> +            'cyan_background': 46,
> +            'white_background': 47}
>
>  def _terminfosetup(ui, mode):
>      '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
> _______________________________________________
> Mercurial-devel mailing list
> [hidden email]
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 8 of 8 simple] color: move the '_render_effects' function to the core module

Martin von Zweigbergk via Mercurial-devel
In reply to this post by Pierre-Yves David-2
On Wed, Feb 15, 2017 at 3:06 AM, Pierre-Yves David
<[hidden email]> wrote:
> # HG changeset patch
> # User Pierre-Yves David <[hidden email]>
> # Date 1482370733 -3600
> #      Thu Dec 22 02:38:53 2016 +0100
> # Node ID f0f04ac8d4b17345b5375e4b11d895e7c22eb6ab
> # Parent  93e658cd6e685593ddc94d2b46ba596b1bb0d143
> # EXP-Topic color
> color: move the '_render_effects' function to the core module

Queued, thanks.
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 1 of 8 simple] color: spread '_effect' values for readability

Pierre-Yves David-2
In reply to this post by Martin von Zweigbergk via Mercurial-devel


On 02/15/2017 09:49 PM, Martin von Zweigbergk wrote:

> On Wed, Feb 15, 2017 at 3:06 AM, Pierre-Yves David
> <[hidden email]> wrote:
>> # HG changeset patch
>> # User Pierre-Yves David <[hidden email]>
>> # Date 1479491019 -3600
>> #      Fri Nov 18 18:43:39 2016 +0100
>> # Node ID 42d4b49b39d6879065209ea3ca71f1e3fa88fcbc
>> # Parent  354020079723e02ad6db68f58ef26eb7ebd005a8
>> # EXP-Topic color
>> color: spread '_effect' values for readability
>>
>> We move to our "usual" one value per line style.
>
> Nit: the even more usual style (by a factor 2-2.5, it seems) is to put
> the values on separate lines from the curly braces. But I won't let
> that stop the patch, of course.

Just ot be sure, You mean:

_effects = {
     'none': 0,
     black': 30,
}

Right?

--
Pierre-Yves David
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 1 of 8 simple] color: spread '_effect' values for readability

Martin von Zweigbergk via Mercurial-devel


On Feb 16, 2017 01:06, "Pierre-Yves David" <[hidden email]> wrote:


On 02/15/2017 09:49 PM, Martin von Zweigbergk wrote:
On Wed, Feb 15, 2017 at 3:06 AM, Pierre-Yves David
<[hidden email]> wrote:
# HG changeset patch
# User Pierre-Yves David <[hidden email]>
# Date 1479491019 -3600
#      Fri Nov 18 18:43:39 2016 +0100
# Node ID 42d4b49b39d6879065209ea3ca71f1e3fa88fcbc
# Parent  354020079723e02ad6db68f58ef26eb7ebd005a8
# EXP-Topic color
color: spread '_effect' values for readability

We move to our "usual" one value per line style.

Nit: the even more usual style (by a factor 2-2.5, it seems) is to put
the values on separate lines from the curly braces. But I won't let
that stop the patch, of course.

Just ot be sure, You mean:


_effects = {
    'none': 0,
    black': 30,
}

Right?

Exactly. With the closing brace like that too (but a few were different). That's also my preferred form, which is probably why I even noticed.

That seemed to be more common if I was grepping right. But your form was used consistently (?) in color.py, so that was probably the right choice in this case anyway.




--
Pierre-Yves David


_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel