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

login
register
mail settings
Submitter Yuya Nishihara
Date April 8, 2017, 11:37 a.m.
Message ID <30aea1eb80a9141a50c5.1491651455@mimosa>
Download mbox | patch
Permalink /patch/20020/
State Superseded
Headers show

Comments

Yuya Nishihara - April 8, 2017, 11:37 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

Patch

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)
 
-@templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])')
+@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