##// 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 18 from . import (
19 19 encoding,
20 20 error,
21 templatekw,
21 22 templater,
22 23 util,
23 24 )
@@ -45,6 +46,10 b' class baseformatter(object):'
45 46 if self._item is not None:
46 47 self._showitem()
47 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 53 def data(self, **data):
49 54 '''insert data into item that's not shown in default output'''
50 55 self._item.update(data)
@@ -78,6 +83,10 b' class plainformatter(baseformatter):'
78 83 return False
79 84 def startitem(self):
80 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 90 def data(self, **data):
82 91 pass
83 92 def write(self, fields, deftext, *fielddata, **opts):
@@ -112,7 +121,7 b' class pickleformatter(baseformatter):'
112 121 self._ui.write(pickle.dumps(self._data))
113 122
114 123 def _jsonifyobj(v):
115 if isinstance(v, tuple):
124 if isinstance(v, (list, tuple)):
116 125 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
117 126 elif v is None:
118 127 return 'null'
@@ -157,6 +166,16 b' class templateformatter(baseformatter):'
157 166 def _showitem(self):
158 167 g = self._t(self._topic, ui=self._ui, **self._item)
159 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 180 def lookuptemplate(ui, topic, tmpl):
162 181 # looks like a literal template?
General Comments 0
You need to be logged in to leave comments. Login now