Mercurial > public > mercurial-scm > hg-stable
diff hgext/progress.py @ 11555:d8d0fc3988ca stable
color/progress: subclass ui instead of using wrapfunction (issue2096)
This resolves the issue of hg cmd --mq not being colorized. This was due
to color wrapping only the instance of ui passed to dispatch._runcommand(),
which isn't the same ui object that mq.mqcommand() receives. After dispatch
calls extensions.loadall(), it makes sure any changes to ui.__class__ in
uisetup are propagated.
progress is updated to wrap ui in the same manner because wrapfunction
doesn't play well when ui.__class__ has been replaced by another extension
(orig will point to the old class method instead of color's).
author | Brodie Rao <brodie@bitheap.org> |
---|---|
date | Thu, 01 Jul 2010 19:23:26 -0500 |
parents | ec21d91c79b3 |
children | 646eb9337c87 |
line wrap: on
line diff
--- a/hgext/progress.py Wed Jul 14 19:43:31 2010 +0200 +++ b/hgext/progress.py Thu Jul 01 19:23:26 2010 -0500 @@ -45,7 +45,6 @@ import sys import time -from mercurial import extensions from mercurial import util def spacejoin(*args): @@ -159,7 +158,7 @@ tw = util.termwidth() return min(int(self.ui.config('progress', 'width', default=tw)), tw) - def progress(self, orig, topic, pos, item='', unit='', total=None): + def progress(self, topic, pos, item='', unit='', total=None): if pos is None: if self.topics and self.topics[-1] == topic and self.printed: self.complete() @@ -172,29 +171,35 @@ and topic == self.topics[-1]): self.lastprint = now self.show(topic, pos, item, unit, total) - return orig(topic, pos, item=item, unit=unit, total=total) - - def write(self, orig, *args, **opts): - if self.printed: - self.clear() - return orig(*args, **opts) - -sharedprog = None def uisetup(ui): + class progressui(ui.__class__): + _progbar = None + + def progress(self, *args, **opts): + self._progbar.progress(*args, **opts) + return super(progressui, self).progress(*args, **opts) + + def write(self, *args, **opts): + if self._progbar.printed: + self._progbar.clear() + return super(progressui, self).write(*args, **opts) + + def write_err(self, *args, **opts): + if self._progbar.printed: + self._progbar.clear() + return super(progressui, self).write_err(*args, **opts) + # Apps that derive a class from ui.ui() can use # setconfig('progress', 'disable', 'True') to disable this extension if ui.configbool('progress', 'disable'): return if shouldprint(ui) and not ui.debugflag and not ui.quiet: + ui.__class__ = progressui # we instantiate one globally shared progress bar to avoid # competing progress bars when multiple UI objects get created - global sharedprog - if not sharedprog: - sharedprog = progbar(ui) - extensions.wrapfunction(ui, 'progress', sharedprog.progress) - extensions.wrapfunction(ui, 'write', sharedprog.write) - extensions.wrapfunction(ui, 'write_err', sharedprog.write) + if not progressui._progbar: + progressui._progbar = progbar(ui) def reposetup(ui, repo): uisetup(repo.ui)