[PATCH 3 of 5] formatter: add template support

Matt Mackall mpm at selenic.com
Wed Jun 10 18:09:47 CDT 2015


# HG changeset patch
# User Matt Mackall <mpm at selenic.com>
# Date 1433964818 18000
#      Wed Jun 10 14:33:38 2015 -0500
# Node ID 82d8c52373f2f06112c902aaf307c58143bbf32e
# Parent  ce12011158e9c4a97dad04404afaf349e946fb51
formatter: add template support

This lets all the non-log commands that use the formatter use
templates. There are still some things that don't work, for instance:

- color (needs "repo" in map)
- shortest (needs "ctx" in map)

diff -r ce12011158e9 -r 82d8c52373f2 mercurial/formatter.py
--- a/mercurial/formatter.py	Wed Jun 10 14:30:18 2015 -0500
+++ b/mercurial/formatter.py	Wed Jun 10 14:33:38 2015 -0500
@@ -135,6 +135,15 @@
         baseformatter.end(self)
         self._ui.write("\n]\n")
 
+class templateformatter(baseformatter):
+    def __init__(self, ui, topic, opts):
+        baseformatter.__init__(self, ui, topic, opts)
+        self._topic = topic
+        self._t = gettemplater(ui, topic, opts.get('template', ''))
+    def _showitem(self):
+        g = self._t(self._topic, **self._item)
+        self._ui.write(templater.stringify(g))
+
 def lookuptemplate(ui, topic, tmpl):
     # looks like a literal template?
     if '{' in tmpl:
@@ -187,7 +196,7 @@
     elif template == "debug":
         return debugformatter(ui, topic, opts)
     elif template != "":
-        raise util.Abort(_("custom templates not yet supported"))
+        return templateformatter(ui, topic, opts)
     elif ui.configbool('ui', 'formatdebug'):
         return debugformatter(ui, topic, opts)
     elif ui.configbool('ui', 'formatjson'):


More information about the Mercurial-devel mailing list