[PATCH 2 of 2] templater: add nonempty function with an extra argument placeholder

Alexander Plavin me at aplavin.ru
Sat Jun 29 09:02:58 CDT 2013


# HG changeset patch
# User Alexander Plavin <me at aplavin.ru>
# Date 1372502211 -14400
#      Sat Jun 29 14:36:51 2013 +0400
# Node ID 7e93df87bd7b380bb27c3812285e215774602352
# Parent  7b9b339fa1ccb512aa220dbdf0c651c28d469317
templater: add nonempty function with an extra argument placeholder

Behaviour without the extra argument is same as for the existing
nonempty filter.

diff -r 7b9b339fa1cc -r 7e93df87bd7b mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Sat Jun 29 14:27:53 2013 +0400
+++ b/mercurial/help/templates.txt	Sat Jun 29 14:36:51 2013 +0400
@@ -58,6 +58,8 @@
 
 - label(label, expr)
 
+- nonempty(text[, placeholder])
+
 - rstdoc(text, style)
 
 - strip(text[, chars])
diff -r 7b9b339fa1cc -r 7e93df87bd7b mercurial/templater.py
--- a/mercurial/templater.py	Sat Jun 29 14:27:53 2013 +0400
+++ b/mercurial/templater.py	Sat Jun 29 14:36:51 2013 +0400
@@ -321,6 +321,17 @@
     t = stringify(args[1][0](context, mapping, args[1][1]))
     yield runtemplate(context, mapping, compiletemplate(t, context))
 
+def nonempty(context, mapping, args):
+    if not (1 <= len(args) <= 2):
+        raise error.ParseError(_("nonempty expects one or two arguments"))
+
+    text = args[0][0](context, mapping, args[0][1])
+    if len(args) == 1:
+        placeholder = "(none)"
+    else:
+        placeholder = args[1][0](context, mapping, args[1][1])
+    return text or placeholder
+
 def rstdoc(context, mapping, args):
     if len(args) != 2:
         # i18n: "rstdoc" is a keyword
@@ -361,6 +372,7 @@
     "ifeq": ifeq,
     "join": join,
     "label": label,
+    "nonempty": nonempty,
     "rstdoc": rstdoc,
     "strip": strip,
     "sub": sub,
diff -r 7b9b339fa1cc -r 7e93df87bd7b tests/test-command-template.t
--- a/tests/test-command-template.t	Sat Jun 29 14:27:53 2013 +0400
+++ b/tests/test-command-template.t	Sat Jun 29 14:36:51 2013 +0400
@@ -1537,7 +1537,7 @@
   $ hg log -R latesttag -r 10 --template '{sub("[0-9]", "x", "{rev}")}\n'
   xx
 
-Test the strip function with chars specified:
+Test the strip and nonempty functions with extra arguments specified:
 
   $ hg log -R latesttag --template '{desc}\n'
   at3
@@ -1552,14 +1552,14 @@
   b
   a
 
-  $ hg log -R latesttag --template '{strip(desc, "te")}\n'
+  $ hg log -R latesttag --template '{nonempty(strip(desc, "te2"), "<was empty>")}\n'
   at3
   5
   3
-  2
+  <was empty>
   1
   merg
-  h2
+  h
   h2d
   h1c
   b


More information about the Mercurial-devel mailing list