##// END OF EJS Templates
templater: make pad() strip color codes before computing width (issue5416)...
Yuya Nishihara -
r31521:44c591f6 default
parent child Browse files
Show More
@@ -7,6 +7,8 b''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import re
11
10 from .i18n import _
12 from .i18n import _
11
13
12 from . import (
14 from . import (
@@ -327,6 +329,12 b' def _render_effects(ui, text, effects):'
327 stop = '\033[' + str(_effects['none']) + 'm'
329 stop = '\033[' + str(_effects['none']) + 'm'
328 return _mergeeffects(text, start, stop)
330 return _mergeeffects(text, start, stop)
329
331
332 _ansieffectre = re.compile(br'\x1b\[[0-9;]*m')
333
334 def stripeffects(text):
335 """Strip ANSI control codes which could be inserted by colorlabel()"""
336 return _ansieffectre.sub('', text)
337
330 def colorlabel(ui, msg, label):
338 def colorlabel(ui, msg, label):
331 """add color control code according to the mode"""
339 """add color control code according to the mode"""
332 if ui._colormode == 'debug':
340 if ui._colormode == 'debug':
@@ -352,7 +360,6 b' def colorlabel(ui, msg, label):'
352 w32effects = None
360 w32effects = None
353 if pycompat.osname == 'nt':
361 if pycompat.osname == 'nt':
354 import ctypes
362 import ctypes
355 import re
356
363
357 _kernel32 = ctypes.windll.kernel32
364 _kernel32 = ctypes.windll.kernel32
358
365
@@ -13,6 +13,7 b' import types'
13
13
14 from .i18n import _
14 from .i18n import _
15 from . import (
15 from . import (
16 color,
16 config,
17 config,
17 encoding,
18 encoding,
18 error,
19 error,
@@ -576,13 +577,13 b' def pad(context, mapping, args):'
576 fillchar = ' '
577 fillchar = ' '
577 if len(args) > 2:
578 if len(args) > 2:
578 fillchar = evalstring(context, mapping, args[2])
579 fillchar = evalstring(context, mapping, args[2])
579 if len(fillchar) != 1:
580 if len(color.stripeffects(fillchar)) != 1:
580 # i18n: "pad" is a keyword
581 # i18n: "pad" is a keyword
581 raise error.ParseError(_("pad() expects a single fill character"))
582 raise error.ParseError(_("pad() expects a single fill character"))
582 if len(args) > 3:
583 if len(args) > 3:
583 left = evalboolean(context, mapping, args[3])
584 left = evalboolean(context, mapping, args[3])
584
585
585 fillwidth = width - encoding.colwidth(text)
586 fillwidth = width - encoding.colwidth(color.stripeffects(text))
586 if fillwidth <= 0:
587 if fillwidth <= 0:
587 return text
588 return text
588 if left:
589 if left:
@@ -3354,6 +3354,12 b' color effects can be nested (issue5413)'
3354 > '{label(red, "red{label(magenta, "ma{label(cyan, "cyan")}{label(yellow, "yellow")}genta")}")}\n'
3354 > '{label(red, "red{label(magenta, "ma{label(cyan, "cyan")}{label(yellow, "yellow")}genta")}")}\n'
3355 \x1b[0;31mred\x1b[0;35mma\x1b[0;36mcyan\x1b[0m\x1b[0;31m\x1b[0;35m\x1b[0;33myellow\x1b[0m\x1b[0;31m\x1b[0;35mgenta\x1b[0m (esc)
3355 \x1b[0;31mred\x1b[0;35mma\x1b[0;36mcyan\x1b[0m\x1b[0;31m\x1b[0;35m\x1b[0;33myellow\x1b[0m\x1b[0;31m\x1b[0;35mgenta\x1b[0m (esc)
3356
3356
3357 pad() should interact well with color codes (issue5416)
3358
3359 $ hg debugtemplate --color=always \
3360 > '{pad(label(red, "red"), 5, label(cyan, "-"))}\n'
3361 \x1b[0;31mred\x1b[0m\x1b[0;36m-\x1b[0m\x1b[0;36m-\x1b[0m (esc)
3362
3357 label should be no-op if color is disabled:
3363 label should be no-op if color is disabled:
3358
3364
3359 $ hg log --color=never -l 1 --template '{label(red, "text\n")}'
3365 $ hg log --color=never -l 1 --template '{label(red, "text\n")}'
General Comments 0
You need to be logged in to leave comments. Login now