Mercurial > hg > mercurial-source
changeset 33362: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 | b6612d8579e4 |
children | 35985d407d49 |
files | mercurial/formatter.py mercurial/ui.py |
diffstat | 2 files changed, 17 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/formatter.py +++ b/mercurial/formatter.py @@ -237,24 +237,28 @@ class plainformatter(baseformatter): '''the default text output scheme''' - def __init__(self, ui, topic, opts): + def __init__(self, ui, out, topic, opts): baseformatter.__init__(self, ui, topic, opts, _plainconverter) if ui.debugflag: self.hexfunc = hex else: self.hexfunc = short + if ui is out: + self._write = ui.write + else: + self._write = lambda s, **opts: out.write(s) def startitem(self): pass def data(self, **data): pass def write(self, fields, deftext, *fielddata, **opts): - self._ui.write(deftext % fielddata, **opts) + self._write(deftext % fielddata, **opts) def condwrite(self, cond, fields, deftext, *fielddata, **opts): '''do conditional write''' if cond: - self._ui.write(deftext % fielddata, **opts) + self._write(deftext % fielddata, **opts) def plain(self, text, **opts): - self._ui.write(text, **opts) + self._write(text, **opts) def isplain(self): return True def nested(self, field): @@ -411,20 +415,20 @@ t.cache[topic] = tmpl return t -def formatter(ui, topic, opts): +def formatter(ui, out, topic, opts): template = opts.get("template", "") if template == "json": - return jsonformatter(ui, ui, topic, opts) + return jsonformatter(ui, out, topic, opts) elif template == "pickle": - return pickleformatter(ui, ui, topic, opts) + return pickleformatter(ui, out, topic, opts) elif template == "debug": - return debugformatter(ui, ui, topic, opts) + return debugformatter(ui, out, topic, opts) elif template != "": - return templateformatter(ui, ui, topic, opts) + return templateformatter(ui, out, topic, opts) # developer config: ui.formatdebug elif ui.configbool('ui', 'formatdebug'): - return debugformatter(ui, ui, topic, opts) + return debugformatter(ui, out, topic, opts) # deprecated config: ui.formatjson elif ui.configbool('ui', 'formatjson'): - return jsonformatter(ui, ui, topic, opts) - return plainformatter(ui, topic, opts) + return jsonformatter(ui, out, topic, opts) + return plainformatter(ui, out, topic, opts)