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

login
register
mail settings
Submitter Simon Heimberg
Date March 22, 2013, 1:20 a.m.
Message ID <01d17e4dac6c2b84106e.1363915256@lapsi.heimberg.home>
Download mbox | patch
Permalink /patch/1157/
State Changes Requested, archived
Headers show

Comments

Simon Heimberg - March 22, 2013, 1:20 a.m.
# HG changeset patch
# User Simon Heimberg <simohe@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).
Bryan O'Sullivan - March 26, 2013, 5:39 p.m.
On Thu, Mar 21, 2013 at 6:20 PM, Simon Heimberg <simohe@besonet.ch> wrote:

> color: set the ui class before a repo is created
>

This patch feels like it tries to do too many things, and I don't
understand it at all. I've applied 1 through 5, though.

Patch

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',