[PATCH 2 of 2] templater: move label() function from color extension

Yuya Nishihara yuya at tcha.org
Sat Mar 5 09:07:00 EST 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1434031454 -32400
#      Thu Jun 11 23:04:14 2015 +0900
# Node ID a0bb91d92f440b114966015f11160527457f7325
# Parent  b4bdb4210d4d23d0c783bfed38ee1dd5c15f3a15
templater: move label() function from color extension

ui.label() is no-op by default, so we can just call ui.label() by label()
template function no matter if the color is enabled or not.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -157,7 +157,6 @@ import os
 
 from mercurial import cmdutil, commands, dispatch, extensions, subrepo, util
 from mercurial import ui as uimod
-from mercurial import templater, error
 from mercurial.i18n import _
 
 cmdtable = {}
@@ -480,24 +479,6 @@ class colorui(uimod.ui):
                               for s in msg.split('\n')])
         return msg
 
-def templatelabel(context, mapping, args):
-    if len(args) != 2:
-        # i18n: "label" is a keyword
-        raise error.ParseError(_("label expects two arguments"))
-
-    thing = templater.evalstring(context, mapping, args[1])
-
-    # apparently, repo could be a string that is the favicon?
-    repo = mapping.get('repo', '')
-    if isinstance(repo, str):
-        return thing
-
-    # preserve unknown symbol as literal so effects like 'red', 'bold',
-    # etc. don't need to be quoted
-    label = templater.evalstringliteral(context, mapping, args[0])
-
-    return repo.ui.label(thing, label)
-
 def uisetup(ui):
     if ui.plain():
         return
@@ -519,8 +500,6 @@ def uisetup(ui):
         return orig(gitsub, commands, env, stream, cwd)
     extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
     extensions.wrapfunction(subrepo.gitsubrepo, '_gitnodir', colorgit)
-    templatelabel.__doc__ = templater.funcs['label'].__doc__
-    templater.funcs['label'] = templatelabel
 
 def extsetup(ui):
     commands.globalopts.append(
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -547,8 +547,18 @@ def label(context, mapping, args):
         # i18n: "label" is a keyword
         raise error.ParseError(_("label expects two arguments"))
 
-    # ignore args[0] (the label string) since this is supposed to be a a no-op
-    yield args[1][0](context, mapping, args[1][1])
+    thing = evalstring(context, mapping, args[1])
+
+    # apparently, repo could be a string that is the favicon?
+    repo = mapping.get('repo', '')
+    if isinstance(repo, str):
+        return thing
+
+    # preserve unknown symbol as literal so effects like 'red', 'bold',
+    # etc. don't need to be quoted
+    label = evalstringliteral(context, mapping, args[0])
+
+    return repo.ui.label(thing, label)
 
 def latesttag(context, mapping, args):
     """:latesttag([pattern]): The global tags matching the given pattern on the
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -3183,6 +3183,13 @@ color effect can be specified without qu
   $ hg log --color=always -l 1 --template '{label(red, "text\n")}'
   \x1b[0;31mtext\x1b[0m (esc)
 
+label should be no-op if color is disabled:
+
+  $ hg log --color=never -l 1 --template '{label(red, "text\n")}'
+  text
+  $ hg log --config extensions.color=! -l 1 --template '{label(red, "text\n")}'
+  text
+
 Test branches inside if statement:
 
   $ hg log -r 0 --template '{if(branches, "yes", "no")}\n'


More information about the Mercurial-devel mailing list