diff mercurial/templatefuncs.py @ 40189:9458dbfa7f33

templatefuncs: add truncate parameter to pad Add a truncate option to pad that additionally truncates the text to the pad width if it is wider. Since color codes can cause a problem with this, when the text is truncated, the color codes are also stripped. Users of the truncate option should label the text outside the pad. Differential Revision: https://phab.mercurial-scm.org/D5000
author Mark Thomas <mbthomas@fb.com>
date Fri, 12 Oct 2018 15:46:52 +0000
parents 83f8f7b9fa60
children d11e2c5b287e
line wrap: on
line diff
--- a/mercurial/templatefuncs.py	Fri Oct 12 15:46:51 2018 +0000
+++ b/mercurial/templatefuncs.py	Fri Oct 12 15:46:52 2018 +0000
@@ -216,8 +216,9 @@
 
     return stringutil.mapname(cache['mailmap'], author)
 
-@templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
-              argspec='text width fillchar left')
+@templatefunc(
+    'pad(text, width[, fillchar=\' \'[, left=False[, truncate=False]]])',
+    argspec='text width fillchar left truncate')
 def pad(context, mapping, args):
     """Pad text with a
     fill character."""
@@ -231,6 +232,7 @@
 
     text = evalstring(context, mapping, args['text'])
 
+    truncate = False
     left = False
     fillchar = ' '
     if 'fillchar' in args:
@@ -240,8 +242,12 @@
             raise error.ParseError(_("pad() expects a single fill character"))
     if 'left' in args:
         left = evalboolean(context, mapping, args['left'])
+    if 'truncate' in args:
+        truncate = evalboolean(context, mapping, args['truncate'])
 
     fillwidth = width - encoding.colwidth(color.stripeffects(text))
+    if fillwidth < 0 and truncate:
+        return encoding.trim(color.stripeffects(text), width, leftside=left)
     if fillwidth <= 0:
         return text
     if left: