diff mercurial/templater.py @ 20370:aa51392da507

template: add pad function for padding output Adds a pad template function with the following signature: pad(text, width, fillchar=' ', right=False) This uses the standard python ljust and rjust functions to produce a string that is at least a certain width. This is useful for aligning variable length strings in log output (like user names or shortest(node) output).
author Durham Goode <durham@fb.com>
date Fri, 17 Jan 2014 00:16:48 -0800
parents 9c6b86dd2ed2
children 6f3fb6a974e0
line wrap: on
line diff
--- a/mercurial/templater.py	Fri Jan 17 00:10:37 2014 -0800
+++ b/mercurial/templater.py	Fri Jan 17 00:16:48 2014 -0800
@@ -245,6 +245,31 @@
 
     return templatefilters.fill(text, width, initindent, hangindent)
 
+def pad(context, mapping, args):
+    """usage: pad(text, width, fillchar=' ', right=False)
+    """
+    if not (2 <= len(args) <= 4):
+        raise error.ParseError(_("pad() expects two to four arguments"))
+
+    width = int(args[1][1])
+
+    text = stringify(args[0][0](context, mapping, args[0][1]))
+    if args[0][0] == runstring:
+        text = stringify(runtemplate(context, mapping,
+            compiletemplate(text, context)))
+
+    right = False
+    fillchar = ' '
+    if len(args) > 2:
+        fillchar = stringify(args[2][0](context, mapping, args[2][1]))
+    if len(args) > 3:
+        right = util.parsebool(args[3][1])
+
+    if right:
+        return text.rjust(width, fillchar)
+    else:
+        return text.ljust(width, fillchar)
+
 def get(context, mapping, args):
     if len(args) != 2:
         # i18n: "get" is a keyword
@@ -407,6 +432,7 @@
     "ifeq": ifeq,
     "join": join,
     "label": label,
+    "pad": pad,
     "rstdoc": rstdoc,
     "shortest": shortest,
     "strip": strip,