Comments
Patch
@@ -477,6 +477,24 @@
return ''
+def word(context, mapping, args):
+ """return nth word from a string"""
+ if len(args) != 2 and len(args) != 3:
+ raise error.ParseError(_("word expects two or three arguments"))
+
+ 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 +522,7 @@
"startswith": startswith,
"strip": strip,
"sub": sub,
+ "word": word,
}
# template engine
@@ -1908,3 +1908,51 @@
|
o line 1
line 2
+
+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