D5000: templatefuncs: add truncate parameter to pad

mbthomas (Mark Thomas) phabricator at mercurial-scm.org
Fri Oct 12 15:53:14 UTC 2018


mbthomas created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D5000

AFFECTED FILES
  mercurial/templatefuncs.py
  tests/test-template-functions.t

CHANGE DETAILS

diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -696,6 +696,12 @@
   > '{pad(label(red, "red"), 5, label(cyan, "-"))}\n'
   \x1b[0;31mred\x1b[0m\x1b[0;36m-\x1b[0m\x1b[0;36m-\x1b[0m (esc)
 
+pad() with truncate has to strip color codes, though
+
+  $ hg debugtemplate --color=always \
+  > '{pad(label(red, "scarlet"), 5, truncate=true)}\n'
+  scarl
+
 label should be no-op if color is disabled:
 
   $ hg log --color=never -l 1 --template '{label(red, "text\n")}'
@@ -928,6 +934,15 @@
   1------------------- {node|short}
   0------------------- test
 
+  $ hg log --template '{pad(author, 5, "-", False, True)}\n'
+  test-
+  {node
+  test-
+  $ hg log --template '{pad(author, 5, "-", True, True)}\n'
+  -test
+  hort}
+  -test
+
 Test template string in pad function
 
   $ hg log -r 0 -T '{pad("\{{rev}}", 10)} {author|user}\n'
diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -216,8 +216,9 @@
 
     return stringutil.mapname(cache['mailmap'], author)
 
- at templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
-              argspec='text width fillchar left')
+ at 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:



To: mbthomas, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list