Patchwork templater: add nonempty function with an extra argument placeholder

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 5, 2013, 9:20 p.m.
Message ID <29057a4b94c98256b13e.1375737603@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2000/
State Deferred
Delegated to: Kevin Bullock
Headers show

Comments

Alexander Plavin - Aug. 5, 2013, 9:20 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1372502211 -14400
#      Sat Jun 29 14:36:51 2013 +0400
# Node ID 29057a4b94c98256b13e0948bd79cb106f918ed7
# Parent  2813b5f5514c7692e74befe2fee13ee58170b8df
templater: add nonempty function with an extra argument placeholder

Behaviour without the extra argument is same as for the existing
nonempty filter.
Alexander Plavin - Aug. 5, 2013, 9:23 p.m.
Last time the patch was 'deferred', so I assumed I should send it again.

Patch

diff -r 2813b5f5514c -r 29057a4b94c9 mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Mon Jul 22 17:07:19 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 2813b5f5514c -r 29057a4b94c9 mercurial/templater.py
--- a/mercurial/templater.py	Mon Jul 22 17:07:19 2013 +0400
+++ b/mercurial/templater.py	Sat Jun 29 14:36:51 2013 +0400
@@ -311,6 +311,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 2813b5f5514c -r 29057a4b94c9 tests/test-command-template.t
--- a/tests/test-command-template.t	Mon Jul 22 17:07:19 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