[PATCH 5 of 5] templater: port pad() to take keyword arguments

Yuya Nishihara yuya at tcha.org
Sat Apr 8 07:37:35 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1491225832 -32400
#      Mon Apr 03 22:23:52 2017 +0900
# Node ID 30aea1eb80a9141a50c54d1f30eac523818ba4a0
# Parent  3c73949652fca9dd142af5529fc8f190801b5913
templater: port pad() to take keyword arguments

This is another example where keyword arguments can be actually useful.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -588,29 +588,30 @@ def formatnode(context, mapping, args):
         return node
     return templatefilters.short(node)
 
- at templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])')
+ at templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
+              argspec='text width fillchar left')
 def pad(context, mapping, args):
     """Pad text with a
     fill character."""
-    if not (2 <= len(args) <= 4):
+    if 'text' not in args or 'width' not in args:
         # i18n: "pad" is a keyword
         raise error.ParseError(_("pad() expects two to four arguments"))
 
-    width = evalinteger(context, mapping, args[1],
+    width = evalinteger(context, mapping, args['width'],
                         # i18n: "pad" is a keyword
                         _("pad() expects an integer width"))
 
-    text = evalstring(context, mapping, args[0])
+    text = evalstring(context, mapping, args['text'])
 
     left = False
     fillchar = ' '
-    if len(args) > 2:
-        fillchar = evalstring(context, mapping, args[2])
+    if 'fillchar' in args:
+        fillchar = evalstring(context, mapping, args['fillchar'])
         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])
+    if 'left' in args:
+        left = evalboolean(context, mapping, args['left'])
 
     fillwidth = width - encoding.colwidth(color.stripeffects(text))
     if fillwidth <= 0:
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -146,6 +146,9 @@ Keyword arguments:
   hg: parse error: can't use a key-value pair in this context
   [255]
 
+  $ hg debugtemplate '{pad("foo", width=10, left=true)}\n'
+         foo
+
 Second branch starting at nullrev:
 
   $ hg update null


More information about the Mercurial-devel mailing list