Patchwork [2,of,2] color: add template label function

login
register
mail settings
Submitter Sean Farley
Date Dec. 29, 2012, 1:30 a.m.
Message ID <5739e1cd5ce260376a6c.1356744622@laptop.local>
Download mbox | patch
Permalink /patch/329/
State Accepted
Commit 323e1267de36bb933b8b568da5156693dab0f341
Delegated to: Bryan O'Sullivan
Headers show

Comments

Sean Farley - Dec. 29, 2012, 1:30 a.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley at gmail.com>
# Date 1356744310 21600
# Node ID 5739e1cd5ce260376a6cc55d0a7a0af75dfc2b84
# Parent  f80de1a51abf6299401672ce29ae38b1c192e3ec
color: add template label function

Patch

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -101,10 +101,11 @@ 
 '''
 
 import os
 
 from mercurial import commands, dispatch, extensions, ui as uimod, util
+from mercurial import templater
 from mercurial.i18n import _
 
 testedwith = 'internal'
 
 # start and stop parameters for effects
@@ -352,10 +353,32 @@ 
         if effects:
             return '\n'.join([render_effects(s, effects)
                               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.stringify(args[1][0](context, mapping, args[1][1]))
+    thing = templater.runtemplate(context, mapping,
+                                  templater.compiletemplate(thing, context))
+
+    # apparently, repo could be a string that is the favicon?
+    repo = mapping.get('repo', '')
+    if isinstance(repo, str):
+        return thing
+
+    label = templater.stringify(args[0][0](context, mapping, args[0][1]))
+    label = templater.runtemplate(context, mapping,
+                                  templater.compiletemplate(label, context))
+
+    thing = templater.stringify(thing)
+    label = templater.stringify(label)
+
+    return repo.ui.label(thing, label)
 
 def uisetup(ui):
     global _terminfo_params
     if ui.plain():
         return
@@ -368,10 +391,11 @@ 
                 ui_.__class__ = colorui
             extstyles()
             configstyles(ui_)
         return orig(ui_, opts, cmd, cmdfunc)
     extensions.wrapfunction(dispatch, '_runcommand', colorcmd)
+    templater.funcs['label'] = templatelabel
 
 def extsetup(ui):
     commands.globalopts.append(
         ('', 'color', 'auto',
          # i18n: 'always', 'auto', and 'never' are keywords and should