Patchwork [V3] templater: introduce word function

login
register
mail settings
Submitter Ryan McElroy
Date July 6, 2014, 5:24 p.m.
Message ID <5efbb2b98205e1951e0a.1404667486@nexus.gateway.2wire.net>
Download mbox | patch
Permalink /patch/5117/
State Accepted
Headers show

Comments

Ryan McElroy - July 6, 2014, 5:24 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1402621343 25200
#      Thu Jun 12 18:02:23 2014 -0700
# Node ID 5efbb2b98205e1951e0a9e68bfdd8b5c226ed0c8
# Parent  61b333b982ea7baab198a188306fc05fb2850179
templater: introduce word function

This function allows returning only the nth "word" from a string. By default
a string is split as by Python's split() function default, but an optional
third parameter can also override what string the string is split by.
Pierre-Yves David - July 8, 2014, 10:34 a.m.
On 07/08/2014 01:10 AM, Ryan McElroy wrote:
> Thanks Pierre-Yves! I'll do better next time.

You already did quite well for a first series.

Running `test-check*.t` tests caught most of this kind of mistake.

I also highly recommand running all the tests before patchbombing,

Patch

diff -r 61b333b982ea -r 5efbb2b98205 mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Fri Jun 13 15:59:18 2014 -0700
+++ b/mercurial/help/templates.txt	Thu Jun 12 18:02:23 2014 -0700
@@ -72,6 +72,8 @@ 
 
 - sub(pat, repl, expr)
 
+- word(number, text[, separator])
+
 Also, for any expression that returns a list, there is a list operator:
 
 - expr % "{template}"
@@ -130,3 +132,7 @@ 
 - Show only commit descriptions that start with "template"::
 
    $ hg log --template "{startswith(\"template\", firstline(desc))}\n"
+
+- Print the first word of each line of a commit message::
+
+   $ hg log --template "{word(\"0\", desc)}\n"
diff -r 61b333b982ea -r 5efbb2b98205 mercurial/templater.py
--- a/mercurial/templater.py	Fri Jun 13 15:59:18 2014 -0700
+++ b/mercurial/templater.py	Thu Jun 12 18:02:23 2014 -0700
@@ -477,6 +477,25 @@ 
     return ''
 
 
+def word(context, mapping, args):
+    """return nth word from a string"""
+    if not (2 <= len(args) <= 3):
+        raise error.ParseError(
+                _("word expects two or three arguments, got %d" % len(args)))
+
+    num = int(stringify(args[0][0](context, mapping, args[0][1])))
+    text = stringify(args[1][0](context, mapping, args[1][1]))
+    if len(args) == 3:
+        splitter = stringify(args[2][0](context, mapping, args[2][1]))
+    else:
+        splitter = None
+
+    tokens = text.split(splitter)
+    if num >= len(tokens):
+        return ''
+    else:
+        return tokens[num]
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "rawstring": lambda e, c: (runrawstring, e[1]),
@@ -504,6 +523,7 @@ 
     "startswith": startswith,
     "strip": strip,
     "sub": sub,
+    "word": word,
 }
 
 # template engine
diff -r 61b333b982ea -r 5efbb2b98205 tests/test-command-template.t
--- a/tests/test-command-template.t	Fri Jun 13 15:59:18 2014 -0700
+++ b/tests/test-command-template.t	Thu Jun 12 18:02:23 2014 -0700
@@ -1917,3 +1917,61 @@ 
   $ hg log -Gv -R a --template '{desc|user()}'
   hg: parse error: expected a symbol, got 'func'
   [255]
+
+Test word function (including index out of bounds graceful failure)
+
+  $ hg log -Gv -R a --template "{word('1', desc)}"
+  @
+  |
+  o
+  |
+  o
+  
+  o
+  |\
+  | o  head
+  | |
+  o |  branch
+  |/
+  o  user,
+  |
+  o  person
+  |
+  o  1
+  |
+  o  1
+  
+
+Test word third parameter used as splitter
+
+  $ hg log -Gv -R a --template "{word('0', desc, 'o')}"
+  @  future
+  |
+  o  third
+  |
+  o  sec
+  
+  o    merge
+  |\
+  | o  new head
+  | |
+  o |  new branch
+  |/
+  o  n
+  |
+  o  n
+  |
+  o
+  |
+  o  line 1
+     line 2
+
+Test word error messages for not enough and too many arguments
+
+  $ hg log -Gv -R a --template "{word('0')}"
+  hg: parse error: word expects two or three arguments, got 1
+  [255]
+
+  $ 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]