comparison mercurial/formatter.py @ 37500:8bb3899a0f47

formatter: make nested items somewhat readable in template output
author Yuya Nishihara <yuya@tcha.org>
date Thu, 15 Mar 2018 22:27:16 +0900
parents e7bc0667c521
children 8c121a9837ca
comparison
equal deleted inserted replaced
37499:75c13343cf38 37500:8bb3899a0f47
93 Nested example: 93 Nested example:
94 94
95 >>> def subrepos(ui, fm): 95 >>> def subrepos(ui, fm):
96 ... fm.startitem() 96 ... fm.startitem()
97 ... fm.write(b'reponame', b'[%s]\\n', b'baz') 97 ... fm.write(b'reponame', b'[%s]\\n', b'baz')
98 ... files(ui, fm.nested(b'files')) 98 ... files(ui, fm.nested(b'files', tmpl=b'{reponame}'))
99 ... fm.end() 99 ... fm.end()
100 >>> show(subrepos) 100 >>> show(subrepos)
101 [baz] 101 [baz]
102 foo 102 foo
103 bar 103 bar
135 '''convert non-primitive data types to be processed by formatter''' 135 '''convert non-primitive data types to be processed by formatter'''
136 136
137 # set to True if context object should be stored as item 137 # set to True if context object should be stored as item
138 storecontext = False 138 storecontext = False
139 139
140 @staticmethod
141 def wrapnested(data, tmpl, sep):
142 '''wrap nested data by appropriate type'''
143 return data
140 @staticmethod 144 @staticmethod
141 def formatdate(date, fmt): 145 def formatdate(date, fmt):
142 '''convert date tuple to appropriate format''' 146 '''convert date tuple to appropriate format'''
143 return date 147 return date
144 @staticmethod 148 @staticmethod
208 def plain(self, text, **opts): 212 def plain(self, text, **opts):
209 '''show raw text for non-templated mode''' 213 '''show raw text for non-templated mode'''
210 def isplain(self): 214 def isplain(self):
211 '''check for plain formatter usage''' 215 '''check for plain formatter usage'''
212 return False 216 return False
213 def nested(self, field): 217 def nested(self, field, tmpl=None, sep=''):
214 '''sub formatter to store nested data in the specified field''' 218 '''sub formatter to store nested data in the specified field'''
215 self._item[field] = data = [] 219 data = []
220 self._item[field] = self._converter.wrapnested(data, tmpl, sep)
216 return _nestedformatter(self._ui, self._converter, data) 221 return _nestedformatter(self._ui, self._converter, data)
217 def end(self): 222 def end(self):
218 '''end output for the formatter''' 223 '''end output for the formatter'''
219 if self._item is not None: 224 if self._item is not None:
220 self._showitem() 225 self._showitem()
240 class _plainconverter(object): 245 class _plainconverter(object):
241 '''convert non-primitive data types to text''' 246 '''convert non-primitive data types to text'''
242 247
243 storecontext = False 248 storecontext = False
244 249
250 @staticmethod
251 def wrapnested(data, tmpl, sep):
252 raise error.ProgrammingError('plainformatter should never be nested')
245 @staticmethod 253 @staticmethod
246 def formatdate(date, fmt): 254 def formatdate(date, fmt):
247 '''stringify date tuple in the given format''' 255 '''stringify date tuple in the given format'''
248 return dateutil.datestr(date, fmt) 256 return dateutil.datestr(date, fmt)
249 @staticmethod 257 @staticmethod
288 self._write(deftext % fielddata, **opts) 296 self._write(deftext % fielddata, **opts)
289 def plain(self, text, **opts): 297 def plain(self, text, **opts):
290 self._write(text, **opts) 298 self._write(text, **opts)
291 def isplain(self): 299 def isplain(self):
292 return True 300 return True
293 def nested(self, field): 301 def nested(self, field, tmpl=None, sep=''):
294 # nested data will be directly written to ui 302 # nested data will be directly written to ui
295 return self 303 return self
296 def end(self): 304 def end(self):
297 pass 305 pass
298 306
347 class _templateconverter(object): 355 class _templateconverter(object):
348 '''convert non-primitive data types to be processed by templater''' 356 '''convert non-primitive data types to be processed by templater'''
349 357
350 storecontext = True 358 storecontext = True
351 359
360 @staticmethod
361 def wrapnested(data, tmpl, sep):
362 '''wrap nested data by templatable type'''
363 return templateutil.mappinglist(data, tmpl=tmpl, sep=sep)
352 @staticmethod 364 @staticmethod
353 def formatdate(date, fmt): 365 def formatdate(date, fmt):
354 '''return date tuple''' 366 '''return date tuple'''
355 return date 367 return date
356 @staticmethod 368 @staticmethod