changeset 31520:6f150bb19317

templater: make pad() compute actual width str.ljust() and .rjust() are based on byte length, which are valid only for ASCII characters.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 18 Mar 2017 20:50:15 +0900
parents 3725986b151a
children 44c591f63458
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sat Mar 18 20:38:44 2017 +0900
+++ b/mercurial/templater.py	Sat Mar 18 20:50:15 2017 +0900
@@ -14,6 +14,7 @@
 from .i18n import _
 from . import (
     config,
+    encoding,
     error,
     minirst,
     parser,
@@ -581,10 +582,13 @@
     if len(args) > 3:
         left = evalboolean(context, mapping, args[3])
 
+    fillwidth = width - encoding.colwidth(text)
+    if fillwidth <= 0:
+        return text
     if left:
-        return text.rjust(width, fillchar)
+        return fillchar * fillwidth + text
     else:
-        return text.ljust(width, fillchar)
+        return text + fillchar * fillwidth
 
 @templatefunc('indent(text, indentchars[, firstline])')
 def indent(context, mapping, args):
--- a/tests/test-command-template.t	Sat Mar 18 20:38:44 2017 +0900
+++ b/tests/test-command-template.t	Sat Mar 18 20:50:15 2017 +0900
@@ -4115,6 +4115,11 @@
   abort: template filter 'utf8' is not compatible with keyword 'rev'
   [255]
 
+pad width:
+
+  $ HGENCODING=utf-8 hg debugtemplate "{pad('`cat utf-8`', 2, '-')}\n"
+  \xc3\xa9- (esc)
+
   $ cd ..
 
 Test that template function in extension is registered as expected