16134
|
1 # formatter.py - generic output formatting for mercurial
|
|
2 #
|
|
3 # Copyright 2012 Matt Mackall <mpm@selenic.com>
|
|
4 #
|
|
5 # This software may be used and distributed according to the terms of the
|
|
6 # GNU General Public License version 2 or any later version.
|
|
7
|
|
8 class baseformatter(object):
|
|
9 def __init__(self, ui, topic, opts):
|
|
10 self._ui = ui
|
|
11 self._topic = topic
|
|
12 self._style = opts.get("style")
|
|
13 self._template = opts.get("template")
|
|
14 self._item = None
|
|
15 def __bool__(self):
|
|
16 '''return False if we're not doing real templating so we can
|
|
17 skip extra work'''
|
|
18 return True
|
|
19 def _showitem(self):
|
|
20 '''show a formatted item once all data is collected'''
|
|
21 pass
|
|
22 def startitem(self):
|
|
23 '''begin an item in the format list'''
|
|
24 if self._item is not None:
|
|
25 self._showitem()
|
|
26 self._item = {}
|
|
27 def data(self, **data):
|
|
28 '''insert data into item that's not shown in default output'''
|
|
29 def write(self, fields, deftext, *fielddata, **opts):
|
|
30 '''do default text output while assigning data to item'''
|
|
31 for k, v in zip(fields.split(), fielddata):
|
|
32 self._item[k] = v
|
|
33 def plain(self, text, **opts):
|
|
34 '''show raw text for non-templated mode'''
|
|
35 pass
|
|
36 def end(self):
|
|
37 '''end output for the formatter'''
|
|
38 if self._item is not None:
|
|
39 self._showitem()
|
|
40
|
|
41 class plainformatter(baseformatter):
|
|
42 '''the default text output scheme'''
|
|
43 def __init__(self, ui, topic, opts):
|
|
44 baseformatter.__init__(self, ui, topic, opts)
|
|
45 def __bool__(self):
|
|
46 return False
|
|
47 def startitem(self):
|
|
48 pass
|
|
49 def data(self, **data):
|
|
50 pass
|
|
51 def write(self, fields, deftext, *fielddata, **opts):
|
|
52 self._ui.write(deftext % fielddata, **opts)
|
|
53 def plain(self, text, **opts):
|
|
54 self._ui.write(text, **opts)
|
|
55 def end(self):
|
|
56 pass
|
|
57
|
|
58 class debugformatter(baseformatter):
|
|
59 def __init__(self, ui, topic, opts):
|
|
60 baseformatter.__init__(self, ui, topic, opts)
|
|
61 self._ui.write("%s = {\n" % self._topic)
|
|
62 def _showitem(self):
|
|
63 self._ui.write(" " + repr(self._item) + ",\n")
|
|
64 def end(self):
|
|
65 baseformatter.end(self)
|
|
66 self._ui.write("}\n")
|
|
67
|
|
68 def formatter(ui, topic, opts):
|
|
69 if ui.configbool('ui', 'formatdebug'):
|
|
70 return debugformatter(ui, topic, opts)
|
|
71 return plainformatter(ui, topic, opts)
|