[PATCH 06 of 10] color: set the ui class before a repo is created

Simon Heimberg simohe at besonet.ch
Thu Mar 21 20:20:56 CDT 2013


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1363886209 -3600
# Node ID 01d17e4dac6c2b84106e38448820844c0fb62eea
# Parent  961866d514b1fd4a6cdc806df185164a6e3014fc
color: set the ui class before a repo is created

Previous only the class of repo.ui was set. The repo was created before so
repo.baseui already existed. Do it in uisetup because this called earlier.
This is also the place suggested in the wiki.
Wrap _parse for reading the global option --color. Do the rest in reposetup
because this is run after _parse is called.
Only run once in reposetup because this is the same as before. When we would
run it for every repo creation it could have side effects. Most settings are
global anyway (colorui._colormode, _styles).

diff -r 961866d514b1 -r 01d17e4dac6c hgext/color.py
--- a/hgext/color.py	Don Mär 21 18:16:49 2013 +0100
+++ b/hgext/color.py	Don Mär 21 18:16:49 2013 +0100
@@ -154,10 +154,9 @@
               "ECMA-48 color\n"))
         _terminfo_params = {}
 
-def _modesetup(ui, opts):
+def _modesetup(ui, coloropt):
     global _terminfo_params
 
-    coloropt = opts['color']
     auto = coloropt == 'auto'
     always = not auto and util.parsebool(coloropt)
     if not always and not auto:
@@ -322,6 +321,7 @@
                            in self._buffers.pop())
         return ''.join(a for a, label in self._buffers.pop())
 
+    _coloropt = None
     _colormode = 'ansi'
     def write(self, *args, **opts):
         label = opts.get('label', '')
@@ -378,22 +378,33 @@
 
     return repo.ui.label(thing, label)
 
+def _parse_color(orig, lui, args):
+    ret = orig(lui, args)
+    options = ret[3]
+    # set on class because lui is not the same as the ui passed later
+    colorui._coloropt = options['color']
+    return ret
+
 def uisetup(ui):
     if ui.plain():
         return
-    def colorcmd(orig, ui_, opts, cmd, cmdfunc):
-        mode = _modesetup(ui_, opts)
-        if mode:
-            colorui._colormode = mode
-            if not issubclass(ui_.__class__, colorui):
-                colorui.__bases__ = (ui_.__class__,)
-                ui_.__class__ = colorui
-            extstyles()
-            configstyles(ui_)
-        return orig(ui_, opts, cmd, cmdfunc)
-    extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
+    if not isinstance(ui, colorui):
+        colorui.__bases__ = (ui.__class__,)
+        ui.__class__ = colorui
+    # warp parse because this is called before reposetup
+    extensions.wrapfunction(dispatch, '_parse', _parse_color)
     templater.funcs['label'] = templatelabel
 
+def reposetup(ui, repo):
+    if ui.plain() or colorui._coloropt is None:
+        return
+    mode = _modesetup(ui, colorui._coloropt)
+    colorui._coloropt = None # only run once because most things are global
+    if mode:
+        colorui._colormode = mode
+        extstyles()
+        configstyles(ui)
+
 def extsetup(ui):
     commands.globalopts.append(
         ('', 'color', 'auto',


More information about the Mercurial-devel mailing list