##// END OF EJS Templates
color/progress: subclass ui instead of using wrapfunction (issue2096)...
Brodie Rao -
r11555:d8d0fc39 stable
parent child Browse files
Show More
@@ -74,7 +74,7 b" Any value other than 'ansi', 'win32', or"
74
74
75 import os, sys
75 import os, sys
76
76
77 from mercurial import commands, dispatch, extensions
77 from mercurial import commands, dispatch, extensions, ui as uimod
78 from mercurial.i18n import _
78 from mercurial.i18n import _
79
79
80 # start and stop parameters for effects
80 # start and stop parameters for effects
@@ -140,49 +140,50 b' def configstyles(ui):'
140 % (e, status))
140 % (e, status))
141 _styles[status] = ' '.join(good)
141 _styles[status] = ' '.join(good)
142
142
143 _buffers = None
143 class colorui(uimod.ui):
144 def style(msg, label):
144 def popbuffer(self, labeled=False):
145 effects = []
145 if labeled:
146 for l in label.split():
146 return ''.join(self.label(a, label) for a, label
147 s = _styles.get(l, '')
147 in self._buffers.pop())
148 if s:
148 return ''.join(a for a, label in self._buffers.pop())
149 effects.append(s)
150 effects = ''.join(effects)
151 if effects:
152 return '\n'.join([render_effects(s, effects)
153 for s in msg.split('\n')])
154 return msg
155
149
156 def popbuffer(orig, labeled=False):
150 _colormode = 'ansi'
157 global _buffers
151 def write(self, *args, **opts):
158 if labeled:
152 label = opts.get('label', '')
159 return ''.join(style(a, label) for a, label in _buffers.pop())
153 if self._buffers:
160 return ''.join(a for a, label in _buffers.pop())
154 self._buffers[-1].extend([(str(a), label) for a in args])
155 elif self._colormode == 'win32':
156 for a in args:
157 win32print(a, orig, **opts)
158 else:
159 return super(colorui, self).write(
160 *[self.label(str(a), label) for a in args], **opts)
161
161
162 mode = 'ansi'
162 def write_err(self, *args, **opts):
163 def write(orig, *args, **opts):
163 label = opts.get('label', '')
164 label = opts.get('label', '')
164 if self._colormode == 'win32':
165 global _buffers
165 for a in args:
166 if _buffers:
166 win32print(a, orig, **opts)
167 _buffers[-1].extend([(str(a), label) for a in args])
167 else:
168 elif mode == 'win32':
168 return super(colorui, self).write(
169 for a in args:
169 *[self.label(str(a), label) for a in args], **opts)
170 win32print(a, orig, **opts)
171 else:
172 return orig(*[style(str(a), label) for a in args], **opts)
173
170
174 def write_err(orig, *args, **opts):
171 def label(self, msg, label):
175 label = opts.get('label', '')
172 effects = []
176 if mode == 'win32':
173 for l in label.split():
177 for a in args:
174 s = _styles.get(l, '')
178 win32print(a, orig, **opts)
175 if s:
179 else:
176 effects.append(s)
180 return orig(*[style(str(a), label) for a in args], **opts)
177 effects = ''.join(effects)
178 if effects:
179 return '\n'.join([render_effects(s, effects)
180 for s in msg.split('\n')])
181 return msg
182
181
183
182 def uisetup(ui):
184 def uisetup(ui):
183 if ui.plain():
185 if ui.plain():
184 return
186 return
185 global mode
186 mode = ui.config('color', 'mode', 'auto')
187 mode = ui.config('color', 'mode', 'auto')
187 if mode == 'auto':
188 if mode == 'auto':
188 if os.name == 'nt' and 'TERM' not in os.environ:
189 if os.name == 'nt' and 'TERM' not in os.environ:
@@ -202,14 +203,11 b' def uisetup(ui):'
202 if (opts['color'] == 'always' or
203 if (opts['color'] == 'always' or
203 (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb'
204 (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb'
204 and ui_.formatted()))):
205 and ui_.formatted()))):
205 global _buffers
206 colorui._colormode = mode
206 _buffers = ui_._buffers
207 colorui.__bases__ = (ui_.__class__,)
207 extensions.wrapfunction(ui_, 'popbuffer', popbuffer)
208 ui_.__class__ = colorui
208 extensions.wrapfunction(ui_, 'write', write)
209 extensions.wrapfunction(ui_, 'write_err', write_err)
210 ui_.label = style
211 extstyles()
209 extstyles()
212 configstyles(ui)
210 configstyles(ui_)
213 return orig(ui_, opts, cmd, cmdfunc)
211 return orig(ui_, opts, cmd, cmdfunc)
214 extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
212 extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
215
213
@@ -45,7 +45,6 b' num characters, or ``+<num>`` for the fi'
45 import sys
45 import sys
46 import time
46 import time
47
47
48 from mercurial import extensions
49 from mercurial import util
48 from mercurial import util
50
49
51 def spacejoin(*args):
50 def spacejoin(*args):
@@ -159,7 +158,7 b' class progbar(object):'
159 tw = util.termwidth()
158 tw = util.termwidth()
160 return min(int(self.ui.config('progress', 'width', default=tw)), tw)
159 return min(int(self.ui.config('progress', 'width', default=tw)), tw)
161
160
162 def progress(self, orig, topic, pos, item='', unit='', total=None):
161 def progress(self, topic, pos, item='', unit='', total=None):
163 if pos is None:
162 if pos is None:
164 if self.topics and self.topics[-1] == topic and self.printed:
163 if self.topics and self.topics[-1] == topic and self.printed:
165 self.complete()
164 self.complete()
@@ -172,29 +171,35 b' class progbar(object):'
172 and topic == self.topics[-1]):
171 and topic == self.topics[-1]):
173 self.lastprint = now
172 self.lastprint = now
174 self.show(topic, pos, item, unit, total)
173 self.show(topic, pos, item, unit, total)
175 return orig(topic, pos, item=item, unit=unit, total=total)
176
177 def write(self, orig, *args, **opts):
178 if self.printed:
179 self.clear()
180 return orig(*args, **opts)
181
182 sharedprog = None
183
174
184 def uisetup(ui):
175 def uisetup(ui):
176 class progressui(ui.__class__):
177 _progbar = None
178
179 def progress(self, *args, **opts):
180 self._progbar.progress(*args, **opts)
181 return super(progressui, self).progress(*args, **opts)
182
183 def write(self, *args, **opts):
184 if self._progbar.printed:
185 self._progbar.clear()
186 return super(progressui, self).write(*args, **opts)
187
188 def write_err(self, *args, **opts):
189 if self._progbar.printed:
190 self._progbar.clear()
191 return super(progressui, self).write_err(*args, **opts)
192
185 # Apps that derive a class from ui.ui() can use
193 # Apps that derive a class from ui.ui() can use
186 # setconfig('progress', 'disable', 'True') to disable this extension
194 # setconfig('progress', 'disable', 'True') to disable this extension
187 if ui.configbool('progress', 'disable'):
195 if ui.configbool('progress', 'disable'):
188 return
196 return
189 if shouldprint(ui) and not ui.debugflag and not ui.quiet:
197 if shouldprint(ui) and not ui.debugflag and not ui.quiet:
198 ui.__class__ = progressui
190 # we instantiate one globally shared progress bar to avoid
199 # we instantiate one globally shared progress bar to avoid
191 # competing progress bars when multiple UI objects get created
200 # competing progress bars when multiple UI objects get created
192 global sharedprog
201 if not progressui._progbar:
193 if not sharedprog:
202 progressui._progbar = progbar(ui)
194 sharedprog = progbar(ui)
195 extensions.wrapfunction(ui, 'progress', sharedprog.progress)
196 extensions.wrapfunction(ui, 'write', sharedprog.write)
197 extensions.wrapfunction(ui, 'write_err', sharedprog.write)
198
203
199 def reposetup(ui, repo):
204 def reposetup(ui, repo):
200 uisetup(repo.ui)
205 uisetup(repo.ui)
@@ -388,6 +388,8 b' def _dispatch(ui, args):'
388 # times so we keep track of configured extensions in _loaded.
388 # times so we keep track of configured extensions in _loaded.
389 extensions.loadall(lui)
389 extensions.loadall(lui)
390 exts = [ext for ext in extensions.extensions() if ext[0] not in _loaded]
390 exts = [ext for ext in extensions.extensions() if ext[0] not in _loaded]
391 # Propagate any changes to lui.__class__ by extensions
392 ui.__class__ = lui.__class__
391
393
392 # (uisetup and extsetup are handled in extensions.loadall)
394 # (uisetup and extsetup are handled in extensions.loadall)
393
395
@@ -80,6 +80,9 b" echo ' .hgignore:'"
80 cat .hg/patches/.hgignore
80 cat .hg/patches/.hgignore
81 echo ' series:'
81 echo ' series:'
82 cat .hg/patches/series
82 cat .hg/patches/series
83
84 echo '% status --mq with color (issue2096)'
85 hg status --mq --config extensions.color= --color=always
83 cd ..
86 cd ..
84
87
85 echo '% init --mq without repo'
88 echo '% init --mq without repo'
@@ -93,6 +93,11 b' bleh'
93 series:
93 series:
94 A
94 A
95 B
95 B
96 % status --mq with color (issue2096)
97 A .hgignore
98 A A
99 A B
100 A series
96 % init --mq without repo
101 % init --mq without repo
97 abort: There is no Mercurial repository here (.hg not found)
102 abort: There is no Mercurial repository here (.hg not found)
98 % init --mq with repo path
103 % init --mq with repo path
General Comments 0
You need to be logged in to leave comments. Login now