Patchwork [V4] templater: introduce indent function

login
register
mail settings
Submitter Ryan McElroy
Date June 5, 2015, 11:19 p.m.
Message ID <0828786502560d44090c.1433546368@devbig105.prn2.facebook.com>
Download mbox | patch
Permalink /patch/9535/
State Superseded
Commit ef8956aa8755d84a6894aa609dfa7ec2e51fed5a
Headers show

Comments

Ryan McElroy - June 5, 2015, 11:19 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1428134632 25200
#      Sat Apr 04 01:03:52 2015 -0700
# Node ID 0828786502560d44090cad0696f24f3eb89af043
# Parent  c39640d26a4c7546faef00b9e5c02af45ab8bf5e
templater: introduce indent function
Ryan McElroy - June 6, 2015, 12:06 a.m.
On 6/5/2015 4:19 PM, 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 0828786502560d44090cad0696f24f3eb89af043
> # Parent  c39640d26a4c7546faef00b9e5c02af45ab8bf5e
> templater: introduce indent function
>
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -327,6 +327,27 @@ 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

I need to update the help message. V5 will come.
> +    characters given in the indentchars string.
> +    Example: indent('hello\\n\\nworld', '  ') == "  hello\\n\\n  world"
> +    """
> +    if 2 > len(args) > 3:
> +        # i18n: "indent" is a keyword
> +        raise error.ParseError(_("indent() expects two or three arguments"))
> +
> +    text = stringify(args[0][0](context, mapping, args[0][1]))
> +    indent = stringify(args[1][0](context, mapping, args[1][1]))
> +
> +    if len(args) == 3:
> +        firstline = stringify(args[2][0](context, mapping, args[2][1]))
> +    else:
> +        firstline = indent
> +
> +    # the indent function doesn't indent the first line, so we do it here
> +    return templatefilters.indent(firstline + 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
> @@ -607,6 +628,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
> @@ -3252,3 +3252,17 @@ Test word for invalid numbers
>     $ hg log -Gv -R a --template "{word('a', desc)}"
>     hg: parse error: word expects an integer index
>     [255]
> +
> +Test indent
> +
> +  $ hg log -T "{indent(desc, '  ', ' ')}\n" -r 0:1 -R a
> +   line 1
> +    line 2
> +   other 1
> +    other 2
> +
> +    other 3
> +
> +  $ hg log -T "{indent(date, '   ')}\n" -r 2:3 -R a
> +     1200000.00
> +     1300000.00
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -327,6 +327,27 @@  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 2 > len(args) > 3:
+        # i18n: "indent" is a keyword
+        raise error.ParseError(_("indent() expects two or three arguments"))
+
+    text = stringify(args[0][0](context, mapping, args[0][1]))
+    indent = stringify(args[1][0](context, mapping, args[1][1]))
+
+    if len(args) == 3:
+        firstline = stringify(args[2][0](context, mapping, args[2][1]))
+    else:
+        firstline = indent
+
+    # the indent function doesn't indent the first line, so we do it here
+    return templatefilters.indent(firstline + 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
@@ -607,6 +628,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
@@ -3252,3 +3252,17 @@  Test word for invalid numbers
   $ hg log -Gv -R a --template "{word('a', desc)}"
   hg: parse error: word expects an integer index
   [255]
+
+Test indent
+
+  $ hg log -T "{indent(desc, '  ', ' ')}\n" -r 0:1 -R a
+   line 1
+    line 2
+   other 1
+    other 2
+  
+    other 3
+
+  $ hg log -T "{indent(date, '   ')}\n" -r 2:3 -R a
+     1200000.00
+     1300000.00