Patchwork [2,of,4] Introduce startswith template function

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

Comments

Ryan McElroy - June 23, 2014, 5:55 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1402620817 25200
#      Thu Jun 12 17:53:37 2014 -0700
# Node ID 6f7e63ebea83c56ba70b17fb5688a42d1a6ec40b
# Parent  ea858bc9677e91361cc993d7d27476a41982400d
Introduce startswith template function

This function returns a string only if it starts with a given string.
It is particularly useful when combined with splitlines and/or used with
conditionals that fail when empty strings are passed in to take action
based on the contents of a line.
Sean Farley - June 23, 2014, 9:02 p.m.
Ryan McElroy writes:

> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1402620817 25200
> #      Thu Jun 12 17:53:37 2014 -0700
> # Node ID 6f7e63ebea83c56ba70b17fb5688a42d1a6ec40b
> # Parent  ea858bc9677e91361cc993d7d27476a41982400d
> Introduce startswith template function

templater: introduce startswith function

> This function returns a string only if it starts with a given string.
> It is particularly useful when combined with splitlines and/or used with
> conditionals that fail when empty strings are passed in to take action
> based on the contents of a line.
>
> diff -r ea858bc9677e -r 6f7e63ebea83 mercurial/templater.py
> --- a/mercurial/templater.py	Thu Jun 12 17:45:41 2014 -0700
> +++ b/mercurial/templater.py	Thu Jun 12 17:53:37 2014 -0700
> @@ -466,6 +466,17 @@
>      src = stringify(_evalifliteral(args[2], context, mapping))
>      yield re.sub(pat, rpl, src)
>  
> +def startswith(context, mapping, args):
> +    if len(args) != 2:
> +        raise error.ParseError(_("starts_with expects two arguments"))

'starts_with' in the error has a extraneous underscore.

Patch

diff -r ea858bc9677e -r 6f7e63ebea83 mercurial/templater.py
--- a/mercurial/templater.py	Thu Jun 12 17:45:41 2014 -0700
+++ b/mercurial/templater.py	Thu Jun 12 17:53:37 2014 -0700
@@ -466,6 +466,17 @@ 
     src = stringify(_evalifliteral(args[2], context, mapping))
     yield re.sub(pat, rpl, src)
 
+def startswith(context, mapping, args):
+    if len(args) != 2:
+        raise error.ParseError(_("starts_with expects two arguments"))
+
+    patn = stringify(args[0][0](context, mapping, args[0][1]))
+    text = stringify(args[1][0](context, mapping, args[1][1]))
+    if text.startswith(patn):
+        return text
+    return ''
+
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "rawstring": lambda e, c: (runrawstring, e[1]),
@@ -490,6 +501,7 @@ 
     "revset": revset,
     "rstdoc": rstdoc,
     "shortest": shortest,
+    "startswith": startswith,
     "strip": strip,
     "sub": sub,
 }
diff -r ea858bc9677e -r 6f7e63ebea83 tests/test-command-template.t
--- a/tests/test-command-template.t	Thu Jun 12 17:45:41 2014 -0700
+++ b/tests/test-command-template.t	Thu Jun 12 17:53:37 2014 -0700
@@ -1884,3 +1884,27 @@ 
   |  foo other 3
   o  foo line 1
      foo line 2
+
+Test startswith
+
+  $ hg log -Gv -R a --template "{startswith('line', desc)}"
+  @
+  |
+  o
+  |
+  o
+  
+  o
+  |\
+  | o
+  | |
+  o |
+  |/
+  o
+  |
+  o
+  |
+  o
+  |
+  o  line 1
+     line 2