##// END OF EJS Templates
formatter: add function to convert list to appropriate format (issue5217)...
Yuya Nishihara -
r29676:c3a9cd78 default
parent child Browse files
Show More
@@ -18,6 +18,7 b' from .node import ('
18 from . import (
18 from . import (
19 encoding,
19 encoding,
20 error,
20 error,
21 templatekw,
21 templater,
22 templater,
22 util,
23 util,
23 )
24 )
@@ -45,6 +46,10 b' class baseformatter(object):'
45 if self._item is not None:
46 if self._item is not None:
46 self._showitem()
47 self._showitem()
47 self._item = {}
48 self._item = {}
49 @staticmethod
50 def formatlist(data, name, fmt='%s', sep=' '):
51 '''convert iterable to appropriate list format'''
52 return list(data)
48 def data(self, **data):
53 def data(self, **data):
49 '''insert data into item that's not shown in default output'''
54 '''insert data into item that's not shown in default output'''
50 self._item.update(data)
55 self._item.update(data)
@@ -78,6 +83,10 b' class plainformatter(baseformatter):'
78 return False
83 return False
79 def startitem(self):
84 def startitem(self):
80 pass
85 pass
86 @staticmethod
87 def formatlist(data, name, fmt='%s', sep=' '):
88 '''stringify iterable separated by sep'''
89 return sep.join(fmt % e for e in data)
81 def data(self, **data):
90 def data(self, **data):
82 pass
91 pass
83 def write(self, fields, deftext, *fielddata, **opts):
92 def write(self, fields, deftext, *fielddata, **opts):
@@ -112,7 +121,7 b' class pickleformatter(baseformatter):'
112 self._ui.write(pickle.dumps(self._data))
121 self._ui.write(pickle.dumps(self._data))
113
122
114 def _jsonifyobj(v):
123 def _jsonifyobj(v):
115 if isinstance(v, tuple):
124 if isinstance(v, (list, tuple)):
116 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
125 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
117 elif v is None:
126 elif v is None:
118 return 'null'
127 return 'null'
@@ -157,6 +166,16 b' class templateformatter(baseformatter):'
157 def _showitem(self):
166 def _showitem(self):
158 g = self._t(self._topic, ui=self._ui, **self._item)
167 g = self._t(self._topic, ui=self._ui, **self._item)
159 self._ui.write(templater.stringify(g))
168 self._ui.write(templater.stringify(g))
169 @staticmethod
170 def formatlist(data, name, fmt='%s', sep=' '):
171 '''build object that can be evaluated as either plain string or list'''
172 # name is mandatory argument for now, but it could be optional if
173 # we have default template keyword, e.g. {item}
174 data = list(data)
175 def f():
176 yield plainformatter.formatlist(data, name, fmt, sep)
177 return templatekw._hybrid(f(), data, lambda x: {name: x},
178 lambda d: fmt % d[name])
160
179
161 def lookuptemplate(ui, topic, tmpl):
180 def lookuptemplate(ui, topic, tmpl):
162 # looks like a literal template?
181 # looks like a literal template?
General Comments 0
You need to be logged in to leave comments. Login now