Mercurial > hg
comparison mercurial/formatter.py @ 32579:012e0da5b759
formatter: add option to redirect output to file object
Commands like 'export' have --output=OUTFILESPEC option, so we need a way
to write formatter output optionally to a file.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 18 Jan 2015 18:04:44 +0900 |
parents | 0fd15522a848 |
children | 35985d407d49 |
comparison
equal
deleted
inserted
replaced
32571:b6612d8579e4 | 32579:012e0da5b759 |
---|---|
235 '''stringify iterable separated by sep''' | 235 '''stringify iterable separated by sep''' |
236 return sep.join(fmt % e for e in data) | 236 return sep.join(fmt % e for e in data) |
237 | 237 |
238 class plainformatter(baseformatter): | 238 class plainformatter(baseformatter): |
239 '''the default text output scheme''' | 239 '''the default text output scheme''' |
240 def __init__(self, ui, topic, opts): | 240 def __init__(self, ui, out, topic, opts): |
241 baseformatter.__init__(self, ui, topic, opts, _plainconverter) | 241 baseformatter.__init__(self, ui, topic, opts, _plainconverter) |
242 if ui.debugflag: | 242 if ui.debugflag: |
243 self.hexfunc = hex | 243 self.hexfunc = hex |
244 else: | 244 else: |
245 self.hexfunc = short | 245 self.hexfunc = short |
246 if ui is out: | |
247 self._write = ui.write | |
248 else: | |
249 self._write = lambda s, **opts: out.write(s) | |
246 def startitem(self): | 250 def startitem(self): |
247 pass | 251 pass |
248 def data(self, **data): | 252 def data(self, **data): |
249 pass | 253 pass |
250 def write(self, fields, deftext, *fielddata, **opts): | 254 def write(self, fields, deftext, *fielddata, **opts): |
251 self._ui.write(deftext % fielddata, **opts) | 255 self._write(deftext % fielddata, **opts) |
252 def condwrite(self, cond, fields, deftext, *fielddata, **opts): | 256 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
253 '''do conditional write''' | 257 '''do conditional write''' |
254 if cond: | 258 if cond: |
255 self._ui.write(deftext % fielddata, **opts) | 259 self._write(deftext % fielddata, **opts) |
256 def plain(self, text, **opts): | 260 def plain(self, text, **opts): |
257 self._ui.write(text, **opts) | 261 self._write(text, **opts) |
258 def isplain(self): | 262 def isplain(self): |
259 return True | 263 return True |
260 def nested(self, field): | 264 def nested(self, field): |
261 # nested data will be directly written to ui | 265 # nested data will be directly written to ui |
262 return self | 266 return self |
409 t = templater.templater(cache=cache, aliases=aliases) | 413 t = templater.templater(cache=cache, aliases=aliases) |
410 if tmpl: | 414 if tmpl: |
411 t.cache[topic] = tmpl | 415 t.cache[topic] = tmpl |
412 return t | 416 return t |
413 | 417 |
414 def formatter(ui, topic, opts): | 418 def formatter(ui, out, topic, opts): |
415 template = opts.get("template", "") | 419 template = opts.get("template", "") |
416 if template == "json": | 420 if template == "json": |
417 return jsonformatter(ui, ui, topic, opts) | 421 return jsonformatter(ui, out, topic, opts) |
418 elif template == "pickle": | 422 elif template == "pickle": |
419 return pickleformatter(ui, ui, topic, opts) | 423 return pickleformatter(ui, out, topic, opts) |
420 elif template == "debug": | 424 elif template == "debug": |
421 return debugformatter(ui, ui, topic, opts) | 425 return debugformatter(ui, out, topic, opts) |
422 elif template != "": | 426 elif template != "": |
423 return templateformatter(ui, ui, topic, opts) | 427 return templateformatter(ui, out, topic, opts) |
424 # developer config: ui.formatdebug | 428 # developer config: ui.formatdebug |
425 elif ui.configbool('ui', 'formatdebug'): | 429 elif ui.configbool('ui', 'formatdebug'): |
426 return debugformatter(ui, ui, topic, opts) | 430 return debugformatter(ui, out, topic, opts) |
427 # deprecated config: ui.formatjson | 431 # deprecated config: ui.formatjson |
428 elif ui.configbool('ui', 'formatjson'): | 432 elif ui.configbool('ui', 'formatjson'): |
429 return jsonformatter(ui, ui, topic, opts) | 433 return jsonformatter(ui, out, topic, opts) |
430 return plainformatter(ui, topic, opts) | 434 return plainformatter(ui, out, topic, opts) |