Patchwork [V2] templater: introduce indent function

login
register
mail settings
Submitter Ryan McElroy
Date April 10, 2015, 6:26 a.m.
Message ID <74a9f4cfc279497f060d.1428647216@devbig105.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8590/
State Superseded
Commit ef8956aa8755d84a6894aa609dfa7ec2e51fed5a
Headers show

Comments

Ryan McElroy - April 10, 2015, 6:26 a.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1428134632 25200
#      Sat Apr 04 01:03:52 2015 -0700
# Node ID 74a9f4cfc279497f060db8ab4a2d3f9f44d8c01d
# Parent  e0e28e910fa3797fd0aa4f818e9b33c5bcbf0e53
templater: introduce indent function
Matt Mackall - April 10, 2015, 7:25 p.m.
On Thu, 2015-04-09 at 23:26 -0700, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1428134632 25200
> #      Sat Apr 04 01:03:52 2015 -0700
> # Node ID 74a9f4cfc279497f060db8ab4a2d3f9f44d8c01d
> # Parent  e0e28e910fa3797fd0aa4f818e9b33c5bcbf0e53
> templater: introduce indent function

This doesn't do what I'd expect "indent" to do, namely indent a whole
block. I think we should instead make the prototype this:

indent(text, indentchars[, firstline])

..and if firstline is present, but is '', there's no indent applied to
it.

While we're thinking about it, this doesn't interact well with fill,
because this:

indent(fill(text, 60), ">>>> ", "")

..gives us a ragged right margin, which is probably not what's wanted
with a hanging indent.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -301,6 +301,20 @@  def pad(context, mapping, args):
     else:
         return text.ljust(width, fillchar)
 
+def indent(context, mapping, args):
+    """:indent(text, indentchars): Indents all non-empty lines except the first
+    with the characters given in the indentchars string.
+    Example: indent('hello\\nworld', '    ') == "hello\\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]))
+
+    return templatefilters.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 +585,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
+    1649980800.00
+    1577872860.00
+    1000000.00
+    1500001.00
+    1500000.00
+    1400000.00
+    1300000.00
+    1200000.00
+    1100000.00
+    1000000.00