comparison hgext/color.py @ 31085:3422de9b657e

color: extract the label code into its own function We extract the logic into a function. This will allow us to move the logic into the core 'color' module and later call it directly from core.
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Thu, 23 Feb 2017 19:00:26 +0100
parents 1482b57701ad
children e6082078c853
comparison
equal deleted inserted replaced
31084:1482b57701ad 31085:3422de9b657e
329 *[self.label(a, label) for a in args], **opts) 329 *[self.label(a, label) for a in args], **opts)
330 330
331 def label(self, msg, label): 331 def label(self, msg, label):
332 if self._colormode is None: 332 if self._colormode is None:
333 return super(colorui, self).label(msg, label) 333 return super(colorui, self).label(msg, label)
334 334 return colorlabel(self, msg, label)
335 if self._colormode == 'debug': 335
336 if label and msg: 336 def colorlabel(ui, msg, label):
337 if msg[-1] == '\n': 337 """add color control code according to the mode"""
338 return "[%s|%s]\n" % (label, msg[:-1]) 338 if ui._colormode == 'debug':
339 else: 339 if label and msg:
340 return "[%s|%s]" % (label, msg) 340 if msg[-1] == '\n':
341 msg = "[%s|%s]\n" % (label, msg[:-1])
341 else: 342 else:
342 return msg 343 msg = "[%s|%s]" % (label, msg)
343 344 elif ui._colormode is not None:
344 effects = [] 345 effects = []
345 for l in label.split(): 346 for l in label.split():
346 s = color._styles.get(l, '') 347 s = color._styles.get(l, '')
347 if s: 348 if s:
348 effects.append(s) 349 effects.append(s)
349 elif color.valideffect(l): 350 elif color.valideffect(l):
350 effects.append(l) 351 effects.append(l)
351 effects = ' '.join(effects) 352 effects = ' '.join(effects)
352 if effects: 353 if effects:
353 return '\n'.join([color._render_effects(line, effects) 354 msg = '\n'.join([color._render_effects(line, effects)
354 for line in msg.split('\n')]) 355 for line in msg.split('\n')])
355 return msg 356 return msg
356 357
357 def uisetup(ui): 358 def uisetup(ui):
358 if ui.plain(): 359 if ui.plain():
359 return 360 return
360 if not isinstance(ui, colorui): 361 if not isinstance(ui, colorui):