Patchwork [V3] templater: introduce indent function

login
register
mail settings
Submitter Ryan McElroy
Date April 14, 2015, 5 p.m.
Message ID <62c312bd92edb3d21abd.1429030835@devbig105.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8650/
State Superseded
Commit ef8956aa8755d84a6894aa609dfa7ec2e51fed5a
Headers show

Comments

Ryan McElroy - April 14, 2015, 5 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1428134632 25200
#      Sat Apr 04 01:03:52 2015 -0700
# Node ID 62c312bd92edb3d21abdb7c2946e9a8c640fce9a
# Parent  e0e28e910fa3797fd0aa4f818e9b33c5bcbf0e53
templater: introduce indent function
Ryan McElroy - April 16, 2015, 3:53 a.m.
On 4/15/2015 11:10 AM, Kevin Bullock wrote:
>> On Apr 14, 2015, at 1:00 PM, Ryan McElroy <rmcelroy@fb.com> wrote:
>>
>> # HG changeset patch
>> # User Ryan McElroy <rmcelroy@fb.com>
>> # Date 1428134632 25200
>> #      Sat Apr 04 01:03:52 2015 -0700
>> # Node ID 62c312bd92edb3d21abdb7c2946e9a8c640fce9a
>> # Parent  e0e28e910fa3797fd0aa4f818e9b33c5bcbf0e53
>> templater: introduce indent function
> This version doesn't appear to address Matt's comments in <https://urldefense.proofpoint.com/v1/url?u=http://markmail.org/message/7qutjdtiwtldaenj&k=ZVNjlDMF0FElm4dQtryO4A%3D%3D%0A&r=hqIjMAw8NkLVCXt7oICF1w%3D%3D%0A&m=mzfTlQcEG5PNJ5abX3NlpVGZ8i4Xo3fvUmvvZb0UB7s%3D%0A&s=d7baaf039440309dc9fad6fb8b3c324fcfc9d860c563ae67cdbc1b31d1db04a2>.
Ug, sorry I missed that. I deleted a bunch of emails and somehow missed 
that. I'll resend after the freeze.

Thanks Kevin.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -301,6 +301,22 @@  def pad(context, mapping, args):
     else:
         return text.ljust(width, fillchar)
 
+def indent(context, mapping, args):
+    """
+    :indent(text, indentchars): Indents all non-empty lines with the
+    characters given in the indentchars string.
+    Example: indent('hello\\n\\nworld', '  ') == "  hello\\n\\n  world"
+    """
+    if len(args) != 2:
+        # i18n: "indent" is a keyword
+        raise error.ParseError(_("indent() expects two arguments"))
+
+    text = stringify(args[0][0](context, mapping, args[0][1]))
+    indent = stringify(args[1][0](context, mapping, args[1][1]))
+
+    # the indent function doesn't indent the first line, so we do it here
+    return templatefilters.indent(indent + text, indent)
+
 def get(context, mapping, args):
     """:get(dict, key): Get an attribute/key from an object. Some keywords
     are complex types. This function allows you to obtain the value of an
@@ -571,6 +587,7 @@  funcs = {
     "if": if_,
     "ifcontains": ifcontains,
     "ifeq": ifeq,
+    "indent": indent,
     "join": join,
     "label": label,
     "pad": pad,
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
@@ -2554,3 +2554,33 @@  Test word error messages for not enough 
   $ hg log -Gv -R a --template "{word('0', desc, 'o', 'h', 'b', 'o', 'y')}"
   hg: parse error: word expects two or three arguments, got 7
   [255]
+
+Test indent
+
+  $ hg log -T "  {indent(desc, '    ')}\n" -R a
+        future
+        third
+        second
+        merge
+        new head
+        new branch
+        no user, no domain
+        no person
+        other 1
+      other 2
+  
+      other 3
+        line 1
+      line 2
+
+  $ hg log -T "  {indent(date, '    ')}\n" -R a
+        1650326400.00
+        1577872860.00
+        1000000.00
+        1500001.00
+        1500000.00
+        1400000.00
+        1300000.00
+        1200000.00
+        1100000.00
+        1000000.00