Patchwork [2,of,5] color: restore _style global after debugcolor ran

login
register
mail settings
Submitter Pierre-Yves David
Date Nov. 3, 2016, 3:56 p.m.
Message ID <d2f98dbce59c7f0cfa88.1478188593@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/17317/
State Accepted
Headers show

Comments

Pierre-Yves David - Nov. 3, 2016, 3:56 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1478179759 -3600
#      Thu Nov 03 14:29:19 2016 +0100
# Node ID d2f98dbce59c7f0cfa8856ead06ad59985e23332
# Parent  eb48068030c410636db6c862921f1c3b3372601d
# EXP-Topic debugcolor
color: restore _style global after debugcolor ran

Before this change, running 'debugcolor' would destroy all color style for the
rest of the process life. We now properly backup and restore the variable
content. Using a global variable is sketchy in general and could probably be
removed. However, this is a quest for another adventure.

Patch

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -540,19 +540,23 @@  def extsetup(ui):
 def debugcolor(ui, repo, **opts):
     """show available colors and effects"""
     global _styles
-    _styles = {}
-    for effect in _effects.keys():
-        _styles[effect] = effect
-    if _terminfo_params:
-        for k, v in ui.configitems('color'):
-            if k.startswith('color.'):
-                _styles[k] = k[6:]
-            elif k.startswith('terminfo.'):
-                _styles[k] = k[9:]
-    ui.write(('color mode: %s\n') % ui._colormode)
-    ui.write(_('available colors:\n'))
-    for colorname, label in _styles.items():
-        ui.write(('%s\n') % colorname, label=label)
+    oldstyle = _styles
+    try:
+        _styles = {}
+        for effect in _effects.keys():
+            _styles[effect] = effect
+        if _terminfo_params:
+            for k, v in ui.configitems('color'):
+                if k.startswith('color.'):
+                    _styles[k] = k[6:]
+                elif k.startswith('terminfo.'):
+                    _styles[k] = k[9:]
+        ui.write(('color mode: %s\n') % ui._colormode)
+        ui.write(_('available colors:\n'))
+        for colorname, label in _styles.items():
+            ui.write(('%s\n') % colorname, label=label)
+    finally:
+        _styles = oldstyle
 
 if os.name != 'nt':
     w32effects = None