Mercurial > hg-stable
changeset 31527:44c591f63458
templater: make pad() strip color codes before computing width (issue5416)
Tested in ANSI mode. We might have to extend _ansieffectre to support
terminfo mode.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 18 Mar 2017 21:02:20 +0900 |
parents | 6f150bb19317 |
children | 23fc9b98445f |
files | mercurial/color.py mercurial/templater.py tests/test-command-template.t |
diffstat | 3 files changed, 17 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/color.py Sat Mar 18 20:50:15 2017 +0900 +++ b/mercurial/color.py Sat Mar 18 21:02:20 2017 +0900 @@ -7,6 +7,8 @@ from __future__ import absolute_import +import re + from .i18n import _ from . import ( @@ -327,6 +329,12 @@ stop = '\033[' + str(_effects['none']) + 'm' return _mergeeffects(text, start, stop) +_ansieffectre = re.compile(br'\x1b\[[0-9;]*m') + +def stripeffects(text): + """Strip ANSI control codes which could be inserted by colorlabel()""" + return _ansieffectre.sub('', text) + def colorlabel(ui, msg, label): """add color control code according to the mode""" if ui._colormode == 'debug': @@ -352,7 +360,6 @@ w32effects = None if pycompat.osname == 'nt': import ctypes - import re _kernel32 = ctypes.windll.kernel32
--- a/mercurial/templater.py Sat Mar 18 20:50:15 2017 +0900 +++ b/mercurial/templater.py Sat Mar 18 21:02:20 2017 +0900 @@ -13,6 +13,7 @@ from .i18n import _ from . import ( + color, config, encoding, error, @@ -576,13 +577,13 @@ fillchar = ' ' if len(args) > 2: fillchar = evalstring(context, mapping, args[2]) - if len(fillchar) != 1: + if len(color.stripeffects(fillchar)) != 1: # i18n: "pad" is a keyword raise error.ParseError(_("pad() expects a single fill character")) if len(args) > 3: left = evalboolean(context, mapping, args[3]) - fillwidth = width - encoding.colwidth(text) + fillwidth = width - encoding.colwidth(color.stripeffects(text)) if fillwidth <= 0: return text if left:
--- a/tests/test-command-template.t Sat Mar 18 20:50:15 2017 +0900 +++ b/tests/test-command-template.t Sat Mar 18 21:02:20 2017 +0900 @@ -3354,6 +3354,12 @@ > '{label(red, "red{label(magenta, "ma{label(cyan, "cyan")}{label(yellow, "yellow")}genta")}")}\n' \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) +pad() should interact well with color codes (issue5416) + + $ hg debugtemplate --color=always \ + > '{pad(label(red, "red"), 5, label(cyan, "-"))}\n' + \x1b[0;31mred\x1b[0m\x1b[0;36m-\x1b[0m\x1b[0;36m-\x1b[0m (esc) + label should be no-op if color is disabled: $ hg log --color=never -l 1 --template '{label(red, "text\n")}'