Mercurial > hg-stable
comparison mercurial/color.py @ 31129:7f056fdbe37e
color: add ui to effect rendering
We'll carry more and more color specific data on the ui object. This will help
isolating different color configuration from each other. For example repository
config might configure special style that should not affect other ui object.
The first step is to make sure the ui object is available were we will needs it.
author | Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
---|---|
date | Sat, 25 Feb 2017 15:00:44 +0100 |
parents | 7fec37746417 |
children | 268caf97c38f |
comparison
equal
deleted
inserted
replaced
31128:95ec3ad62f62 | 31129:7f056fdbe37e |
---|---|
258 continue | 258 continue |
259 cfgeffects = ui.configlist('color', status) | 259 cfgeffects = ui.configlist('color', status) |
260 if cfgeffects: | 260 if cfgeffects: |
261 good = [] | 261 good = [] |
262 for e in cfgeffects: | 262 for e in cfgeffects: |
263 if valideffect(e): | 263 if valideffect(ui, e): |
264 good.append(e) | 264 good.append(e) |
265 else: | 265 else: |
266 ui.warn(_("ignoring unknown color/effect %r " | 266 ui.warn(_("ignoring unknown color/effect %r " |
267 "(configured in color.%s)\n") | 267 "(configured in color.%s)\n") |
268 % (e, status)) | 268 % (e, status)) |
269 _styles[status] = ' '.join(good) | 269 _styles[status] = ' '.join(good) |
270 | 270 |
271 def valideffect(effect): | 271 def valideffect(ui, effect): |
272 'Determine if the effect is valid or not.' | 272 'Determine if the effect is valid or not.' |
273 return ((not _terminfo_params and effect in _effects) | 273 return ((not _terminfo_params and effect in _effects) |
274 or (effect in _terminfo_params | 274 or (effect in _terminfo_params |
275 or effect[:-11] in _terminfo_params)) | 275 or effect[:-11] in _terminfo_params)) |
276 | 276 |
277 def _effect_str(effect): | 277 def _effect_str(ui, effect): |
278 '''Helper function for render_effects().''' | 278 '''Helper function for render_effects().''' |
279 | 279 |
280 bg = False | 280 bg = False |
281 if effect.endswith('_background'): | 281 if effect.endswith('_background'): |
282 bg = True | 282 bg = True |
293 elif bg: | 293 elif bg: |
294 return curses.tparm(curses.tigetstr('setab'), val) | 294 return curses.tparm(curses.tigetstr('setab'), val) |
295 else: | 295 else: |
296 return curses.tparm(curses.tigetstr('setaf'), val) | 296 return curses.tparm(curses.tigetstr('setaf'), val) |
297 | 297 |
298 def _render_effects(text, effects): | 298 def _render_effects(ui, text, effects): |
299 'Wrap text in commands to turn on each effect.' | 299 'Wrap text in commands to turn on each effect.' |
300 if not text: | 300 if not text: |
301 return text | 301 return text |
302 if _terminfo_params: | 302 if _terminfo_params: |
303 start = ''.join(_effect_str(effect) | 303 start = ''.join(_effect_str(ui, effect) |
304 for effect in ['none'] + effects.split()) | 304 for effect in ['none'] + effects.split()) |
305 stop = _effect_str('none') | 305 stop = _effect_str(ui, 'none') |
306 else: | 306 else: |
307 start = [str(_effects[e]) for e in ['none'] + effects.split()] | 307 start = [str(_effects[e]) for e in ['none'] + effects.split()] |
308 start = '\033[' + ';'.join(start) + 'm' | 308 start = '\033[' + ';'.join(start) + 'm' |
309 stop = '\033[' + str(_effects['none']) + 'm' | 309 stop = '\033[' + str(_effects['none']) + 'm' |
310 return ''.join([start, text, stop]) | 310 return ''.join([start, text, stop]) |
321 effects = [] | 321 effects = [] |
322 for l in label.split(): | 322 for l in label.split(): |
323 s = _styles.get(l, '') | 323 s = _styles.get(l, '') |
324 if s: | 324 if s: |
325 effects.append(s) | 325 effects.append(s) |
326 elif valideffect(l): | 326 elif valideffect(ui, l): |
327 effects.append(l) | 327 effects.append(l) |
328 effects = ' '.join(effects) | 328 effects = ' '.join(effects) |
329 if effects: | 329 if effects: |
330 msg = '\n'.join([_render_effects(line, effects) | 330 msg = '\n'.join([_render_effects(ui, line, effects) |
331 for line in msg.split('\n')]) | 331 for line in msg.split('\n')]) |
332 return msg | 332 return msg |
333 | 333 |
334 w32effects = None | 334 w32effects = None |
335 if pycompat.osname == 'nt': | 335 if pycompat.osname == 'nt': |