Mercurial > public > mercurial-scm > hg-stable
changeset 12695:05077896ffe2
pager: don't run pager if nothing is written to stdout/stderr
This decides when to run the pager based on the first call to
ui.write() and ui.write_err(). This has the side effect of not the
output of subprocesses that write output before hg does.
author | Brodie Rao <brodie@bitheap.org> |
---|---|
date | Sun, 10 Oct 2010 12:21:49 -0500 |
parents | 04f6de46bf3a |
children | ef969e58a394 |
files | hgext/pager.py |
diffstat | 1 files changed, 31 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/hgext/pager.py Sun Oct 10 12:21:48 2010 -0500 +++ b/hgext/pager.py Sun Oct 10 12:21:49 2010 -0500 @@ -22,6 +22,12 @@ If no pager is set, the pager extensions uses the environment variable $PAGER. If neither pager.pager, nor $PAGER is set, no pager is used. +By default, the pager is only executed if a command has output. To +force the pager to run even if a command prints nothing, set:: + + [pager] + force = True + If you notice "BROKEN PIPE" error messages, you can disable them by setting:: @@ -57,7 +63,7 @@ from mercurial import commands, dispatch, util, extensions from mercurial.i18n import _ -def _runpager(p): +def _runpager(p, sigpipe=False): if not hasattr(os, 'fork'): sys.stderr = sys.stdout = util.popen(p, 'wb') return @@ -68,6 +74,8 @@ os.dup2(fdout, sys.stdout.fileno()) os.dup2(fdout, sys.stderr.fileno()) os.close(fdout) + if sigpipe: + signal.signal(signal.SIGPIPE, signal.SIG_DFL) return os.dup2(fdin, sys.stdin.fileno()) os.close(fdin) @@ -86,6 +94,23 @@ if ui.plain(): return + class pagerui(ui.__class__): + _pager = None + _pagerstarted = False + + def write(self, *args, **opts): + if self._pager and not self._pagerstarted: + self._pagerstarted = True + self._pager() + return super(pagerui, self).write(*args, **opts) + + def write_err(self, *args, **opts): + if self._pager and not self._pagerstarted: + self._pagerstarted = True + self._pager() + return super(pagerui, self).write(*args, **opts) + ui.__class__ = pagerui + def pagecmd(orig, ui, options, cmd, cmdfunc): p = ui.config("pager", "pager", os.environ.get("PAGER")) if p and sys.stdout.isatty() and '--debugger' not in sys.argv: @@ -97,9 +122,11 @@ (cmd not in ui.configlist('pager', 'ignore') and not attend))): ui.setconfig('ui', 'formatted', ui.formatted()) ui.setconfig('ui', 'interactive', False) - _runpager(p) - if ui.configbool('pager', 'quiet'): - signal.signal(signal.SIGPIPE, signal.SIG_DFL) + sigpipe = ui.configbool('pager', 'quiet') + if ui.configbool('pager', 'force'): + _runpager(p, sigpipe) + else: + ui._pager = lambda: _runpager(p, sigpipe) return orig(ui, options, cmd, cmdfunc) extensions.wrapfunction(dispatch, '_runcommand', pagecmd)