[PATCH] templater: add intersperse() template function

timeless timeless at gmail.com
Tue May 3 15:31:18 EDT 2016


i like this

On Tue, May 3, 2016 at 12:58 PM, Martin von Zweigbergk via
Mercurial-devel <mercurial-devel at mercurial-scm.org> wrote:
> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz at google.com>
> # Date 1462294194 25200
> #      Tue May 03 09:49:54 2016 -0700
> # Branch stable
> # Node ID d88e385e84ca345474c177932e605c79a487a8d4
> # Parent  61463b8fcef871ff3efff53945df4671c71c4e31
> templater: add intersperse() template function
>
> A pretty common pattern in templates is adding conditional separators
> like so:
>
>   {node}{if(bookmarks, " {bookmarks}")}{if(tags, " {tags}")}
>
> With this patch, the above can be simplified to:
>
>   {intersperse(" ", node, bookmarks, tags)}
>
> diff -r 61463b8fcef8 -r d88e385e84ca mercurial/templater.py
> --- a/mercurial/templater.py    Sun May 01 14:36:12 2016 -0500
> +++ b/mercurial/templater.py    Tue May 03 09:49:54 2016 -0700
> @@ -621,6 +621,26 @@
>              yield joiner
>          yield x
>
> + at templatefunc('intersperse(sep, args)')
> +def intersperse(context, mapping, args):
> +    """Add a separator between non-empty arguments."""
> +    if not args:
> +        # i18n: "intersperse" is a keyword
> +        raise error.ParseError(_("intersperse expects at least one argument"))
> +
> +    joiner = evalstring(context, mapping, args[0])
> +
> +    first = True
> +    for x in args[1:]:
> +        argstr = evalstring(context, mapping, x)
> +        if not argstr:
> +            continue
> +        if first:
> +            first = False
> +        else:
> +            yield joiner
> +        yield argstr
> +
>  @templatefunc('label(label, expr)')
>  def label(context, mapping, args):
>      """Apply a label to generated content. Content with
> diff -r 61463b8fcef8 -r d88e385e84ca tests/test-command-template.t
> --- a/tests/test-command-template.t     Sun May 01 14:36:12 2016 -0500
> +++ b/tests/test-command-template.t     Tue May 03 09:49:54 2016 -0700
> @@ -3320,6 +3320,15 @@
>    hg: parse error: pad() expects an integer width
>    [255]
>
> +Test intersperse function
> +
> +  $ hg log -r 0 -T '{intersperse("-", "", "a", "b", "", "", "c", "")}\n'
> +  a-b-c
> +  $ hg log -r 0 -T '{intersperse(" ", "{rev}:{node|short}", author|user, branch)}\n'
> +  0:f7769ec2ab97 test default
> +  $ hg log -r 0 --color=always -T '{intersperse(" ", "a", label(red, "b"), "c", label(red, ""), "d")}\n'
> +  a \x1b[0;31mb\x1b[0m c d (esc)
> +
>  Test ifcontains function
>
>    $ hg log --template '{rev} {ifcontains(rev, "2 two 0", "is in the string", "is not")}\n'
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


More information about the Mercurial-devel mailing list