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

login
register
mail settings
Submitter Alexander Plavin
Date June 29, 2013, 2:02 p.m.
Message ID <7e93df87bd7b380bb27c.1372514578@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/1762/
State Deferred
Headers show

Comments

Alexander Plavin - June 29, 2013, 2:02 p.m.
# HG changeset patch
# User Alexander Plavin <me@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.
Matt Mackall - July 1, 2013, 6:27 p.m.
On Sat, 2013-06-29 at 18:02 +0400, Alexander Plavin wrote:
> # HG changeset patch
> # User Alexander Plavin <me@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.

May I please finally call your attention to the if() function?
Alexander Plavin - July 1, 2013, 7:55 p.m.
2013/7/1 Matt Mackall <mpm@selenic.com>:
> On Sat, 2013-06-29 at 18:02 +0400, Alexander Plavin wrote:
>> # HG changeset patch
>> # User Alexander Plavin <me@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.
>
> May I please finally call your attention to the if() function?
>

I know about a similar if() usage, but compare these ones: 'if(<very
long expression>, <very long expression (again the same!)>,
placeholder)' with 'nonempty(<very long expression>, placeholder)':
the second variant doesn't need repeating the first argument.

> --
> Mathematics is the supreme nostalgia of our time.
>
>

Patch

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