diff mercurial/color.py @ 31518:43d6ef658874

color: insert color code after every "\e[0m" (issue5413) This assumes the last color wins, tested in ANSI mode. I guess terminfo mode would work in the same way.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 18 Mar 2017 20:11:15 +0900
parents 31d2ddfd338c
children 44c591f63458
line wrap: on
line diff
--- a/mercurial/color.py	Sat Mar 18 19:59:47 2017 +0900
+++ b/mercurial/color.py	Sat Mar 18 20:11:15 2017 +0900
@@ -296,6 +296,23 @@
     else:
         return curses.tparm(curses.tigetstr('setaf'), val)
 
+def _mergeeffects(text, start, stop):
+    """Insert start sequence at every occurrence of stop sequence
+
+    >>> s = _mergeeffects('cyan', '[C]', '|')
+    >>> s = _mergeeffects(s + 'yellow', '[Y]', '|')
+    >>> s = _mergeeffects('ma' + s + 'genta', '[M]', '|')
+    >>> s = _mergeeffects('red' + s, '[R]', '|')
+    >>> s
+    '[R]red[M]ma[Y][C]cyan|[R][M][Y]yellow|[R][M]genta|'
+    """
+    parts = []
+    for t in text.split(stop):
+        if not t:
+            continue
+        parts.extend([start, t, stop])
+    return ''.join(parts)
+
 def _render_effects(ui, text, effects):
     'Wrap text in commands to turn on each effect.'
     if not text:
@@ -308,7 +325,7 @@
         start = [str(_effects[e]) for e in ['none'] + effects.split()]
         start = '\033[' + ';'.join(start) + 'm'
         stop = '\033[' + str(_effects['none']) + 'm'
-    return ''.join([start, text, stop])
+    return _mergeeffects(text, start, stop)
 
 def colorlabel(ui, msg, label):
     """add color control code according to the mode"""