comparison mercurial/formatter.py @ 36633:034a07e60e98

templater: allow dynamically switching the default dict/list formatting '%s' doesn't work nicely on Python 3 because many Python types don't implement __bytes__().
author Yuya Nishihara <yuya@tcha.org>
date Thu, 01 Mar 2018 08:07:22 -0500
parents c6061cadb400
children cafd0586876b
comparison
equal deleted inserted replaced
36632:9e2866065982 36633:034a07e60e98
174 self._showitem() 174 self._showitem()
175 self._item = {} 175 self._item = {}
176 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'): 176 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'):
177 '''convert date tuple to appropriate format''' 177 '''convert date tuple to appropriate format'''
178 return self._converter.formatdate(date, fmt) 178 return self._converter.formatdate(date, fmt)
179 def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '): 179 def formatdict(self, data, key='key', value='value', fmt=None, sep=' '):
180 '''convert dict or key-value pairs to appropriate dict format''' 180 '''convert dict or key-value pairs to appropriate dict format'''
181 return self._converter.formatdict(data, key, value, fmt, sep) 181 return self._converter.formatdict(data, key, value, fmt, sep)
182 def formatlist(self, data, name, fmt='%s', sep=' '): 182 def formatlist(self, data, name, fmt=None, sep=' '):
183 '''convert iterable to appropriate list format''' 183 '''convert iterable to appropriate list format'''
184 # name is mandatory argument for now, but it could be optional if 184 # name is mandatory argument for now, but it could be optional if
185 # we have default template keyword, e.g. {item} 185 # we have default template keyword, e.g. {item}
186 return self._converter.formatlist(data, name, fmt, sep) 186 return self._converter.formatlist(data, name, fmt, sep)
187 def context(self, **ctxs): 187 def context(self, **ctxs):
246 '''stringify date tuple in the given format''' 246 '''stringify date tuple in the given format'''
247 return dateutil.datestr(date, fmt) 247 return dateutil.datestr(date, fmt)
248 @staticmethod 248 @staticmethod
249 def formatdict(data, key, value, fmt, sep): 249 def formatdict(data, key, value, fmt, sep):
250 '''stringify key-value pairs separated by sep''' 250 '''stringify key-value pairs separated by sep'''
251 if fmt is None:
252 fmt = '%s=%s'
251 return sep.join(fmt % (k, v) for k, v in _iteritems(data)) 253 return sep.join(fmt % (k, v) for k, v in _iteritems(data))
252 @staticmethod 254 @staticmethod
253 def formatlist(data, name, fmt, sep): 255 def formatlist(data, name, fmt, sep):
254 '''stringify iterable separated by sep''' 256 '''stringify iterable separated by sep'''
257 if fmt is None:
258 fmt = '%s'
255 return sep.join(fmt % e for e in data) 259 return sep.join(fmt % e for e in data)
256 260
257 class plainformatter(baseformatter): 261 class plainformatter(baseformatter):
258 '''the default text output scheme''' 262 '''the default text output scheme'''
259 def __init__(self, ui, out, topic, opts): 263 def __init__(self, ui, out, topic, opts):