RFC: generic command line output templating

Matt Mackall mpm at selenic.com
Tue Sep 27 14:47:55 CDT 2011


A frequent request is to add --template support to more commands. Here's
my proposal on how to make our output engine do this generically.

First, observe that the basic problem is to go from an internal
representation to an external representation with a built-in default.
Also observe that almost all of our output can be viewed as a list of
elements made up of key/value pairs. For instance, log is a list of
changesets with hash/author/date/summary values and status is a list of
state/filename pairs.

So my idea is to structure our output generation so that we can replace
the backend easily. For instance, to print status, we do something like:

t = templater(ui, "status", opts)
for state, fname in repo.status(...):
  t.startentry("statusentry")
  t.write("%s %s\n", "state filename", state, fname)
  t.endentry()
t.end()

By default, the templater can simply do a ui.write() for each t.write(),
but it now has enough information to build the data structures needed to
drive our templating engine. It also becomes possible to add generic XML
and JSON output formats to everything as well without writing templates
for them. So the XML output of the above might look something like:

<?xml version="1.0"?>
<status>
 <statusentry>
  <state>M</state>
  <filename>foo</filename>
 </statusentry>
</status>

Commands like branch and id return single-element lists, while commands
like summary probably return a list of diverse elements.

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list