Patchwork [2,of,2,V2] template: add pad function for padding output

login
register
mail settings
Submitter Durham Goode
Date Feb. 5, 2014, 3:56 a.m.
Message ID <33265256e6441d991d14.1391572599@dev010.prn1.facebook.com>
Download mbox | patch
Permalink /patch/3485/
State Accepted
Commit aa51392da50763f3e7b9ec40acc3a97682488b66
Headers show

Comments

Durham Goode - Feb. 5, 2014, 3:56 a.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1389946608 28800
#      Fri Jan 17 00:16:48 2014 -0800
# Node ID 33265256e6441d991d14e80019fcdc3cd692fa80
# Parent  bc85372a73c4f09e12d7ef8b7ac1f7dfcb1a5108
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).
Matt Mackall - Feb. 6, 2014, 12:34 a.m.
On Tue, 2014-02-04 at 19:56 -0800, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1389946608 28800
> #      Fri Jan 17 00:16:48 2014 -0800
> # Node ID 33265256e6441d991d14e80019fcdc3cd692fa80
> # Parent  bc85372a73c4f09e12d7ef8b7ac1f7dfcb1a5108
> template: add pad function for padding output

Shiny. Queued for default, thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -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,
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
@@ -1637,3 +1637,17 @@ 
   $ hg log --template '{shortest(node, 10)}\n'
   d97c383ae3
   f7769ec2ab
+
+Test pad function
+
+  $ hg log --template '{pad(rev, 20)} {author|user}\n'
+  1                    test
+  0                    test
+
+  $ hg log --template '{pad(rev, 20, " ", True)} {author|user}\n'
+                     1 test
+                     0 test
+
+  $ hg log --template '{pad(rev, 20, "-", False)} {author|user}\n'
+  1------------------- test
+  0------------------- test