Patchwork [1,of,4] Add splitlines function to templates

login
register
mail settings
Submitter Ryan McElroy
Date June 23, 2014, 5:55 p.m.
Message ID <ea858bc9677e91361cc9.1403546116@nexus.gateway.2wire.net>
Download mbox | patch
Permalink /patch/5045/
State Rejected
Headers show

Comments

Ryan McElroy - June 23, 2014, 5:55 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1402620341 25200
#      Thu Jun 12 17:45:41 2014 -0700
# Node ID ea858bc9677e91361cc993d7d27476a41982400d
# Parent  cd3c79392056a0d965236e4986a7a4b5d580f3e5
Add splitlines function to templates

This is useful for applying changes to each line, and it's especially powerful
when used in conjunction with conditionals to modify lines based on content.
Matt Mackall - June 23, 2014, 6:43 p.m.
On Mon, 2014-06-23 at 10:55 -0700, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1402620341 25200
> #      Thu Jun 12 17:45:41 2014 -0700
> # Node ID ea858bc9677e91361cc993d7d27476a41982400d
> # Parent  cd3c79392056a0d965236e4986a7a4b5d580f3e5
> Add splitlines function to templates

These looks good, but two problems:

- non-standard commit summary line (see the first bullet point of the
checklist: http://mercurial.selenic.com/wiki/ContributingChanges)

- we need some mention in the template help ('hg help templates')
Sean Farley - June 23, 2014, 8:56 p.m.
Ryan McElroy writes:

> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1402620341 25200
> #      Thu Jun 12 17:45:41 2014 -0700
> # Node ID ea858bc9677e91361cc993d7d27476a41982400d
> # Parent  cd3c79392056a0d965236e4986a7a4b5d580f3e5
> Add splitlines function to templates

Following up on Matt's comments, something like:

templatefilter: add splitlines function

> This is useful for applying changes to each line, and it's especially powerful
> when used in conjunction with conditionals to modify lines based on content.
>
> diff -r cd3c79392056 -r ea858bc9677e mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py	Wed Jun 18 20:59:36 2014 -0500
> +++ b/mercurial/templatefilters.py	Thu Jun 12 17:45:41 2014 -0700
> @@ -302,6 +302,11 @@
>      """:shortdate: Date. Returns a date like "2006-09-18"."""
>      return util.shortdate(text)
>  
> +def splitlines(text):
> +    """split text into a list of lines"""
> +    from templatekw import showlist

We usually don't use this form of import in Mercurial. Due to the
dependency graph of the template engine, this function might need to go
into templater.py (see for instance 'shortest', 'strip', or 'sub').

Patch

diff -r cd3c79392056 -r ea858bc9677e mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Wed Jun 18 20:59:36 2014 -0500
+++ b/mercurial/templatefilters.py	Thu Jun 12 17:45:41 2014 -0700
@@ -302,6 +302,11 @@ 
     """:shortdate: Date. Returns a date like "2006-09-18"."""
     return util.shortdate(text)
 
+def splitlines(text):
+    """split text into a list of lines"""
+    from templatekw import showlist
+    return showlist('line', text.splitlines(), 'lines')
+
 def stringescape(text):
     return text.encode('string_escape')
 
@@ -384,6 +389,7 @@ 
     "short": short,
     "shortbisect": shortbisect,
     "shortdate": shortdate,
+    "splitlines": splitlines,
     "stringescape": stringescape,
     "stringify": stringify,
     "strip": strip,
diff -r cd3c79392056 -r ea858bc9677e tests/test-command-template.t
--- a/tests/test-command-template.t	Wed Jun 18 20:59:36 2014 -0500
+++ b/tests/test-command-template.t	Thu Jun 12 17:45:41 2014 -0700
@@ -1859,3 +1859,28 @@ 
   $ hg log -R a -r 8 --template '{strip(if("1", if("1", "-abc-")), if("1", if("1", "-")))}\n'
   abc
 
+Test splitlines
+
+  $ hg log -Gv -R a --template "{splitlines(desc) % 'foo {line}\n'}"
+  @  foo future
+  |
+  o  foo third
+  |
+  o  foo second
+  
+  o    foo merge
+  |\
+  | o  foo new head
+  | |
+  o |  foo new branch
+  |/
+  o  foo no user, no domain
+  |
+  o  foo no person
+  |
+  o  foo other 1
+  |  foo other 2
+  |  foo
+  |  foo other 3
+  o  foo line 1
+     foo line 2