2008-03-20 02:57:14 +03:00
|
|
|
# pager.py - display output using a pager
|
|
|
|
#
|
|
|
|
# Copyright 2008 David Soria Parra <dsp@php.net>
|
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2008-03-20 02:57:14 +03:00
|
|
|
#
|
2010-08-28 06:36:35 +04:00
|
|
|
# To load the extension, add it to your configuration file:
|
2008-03-20 02:57:14 +03:00
|
|
|
#
|
|
|
|
# [extension]
|
2009-12-22 02:50:03 +03:00
|
|
|
# pager =
|
2008-03-20 02:57:14 +03:00
|
|
|
#
|
2008-04-03 15:47:05 +04:00
|
|
|
# Run "hg help pager" to get info on configuration.
|
|
|
|
|
2009-06-22 17:48:08 +04:00
|
|
|
'''browse command output with an external pager
|
2008-04-03 15:47:05 +04:00
|
|
|
|
2009-07-23 02:24:03 +04:00
|
|
|
To set the pager that should be used, set the application variable::
|
2008-04-03 15:47:05 +04:00
|
|
|
|
|
|
|
[pager]
|
2012-07-31 17:50:44 +04:00
|
|
|
pager = less -FRX
|
2008-04-03 15:47:05 +04:00
|
|
|
|
2009-07-26 03:56:02 +04:00
|
|
|
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.
|
2008-04-03 15:47:05 +04:00
|
|
|
|
|
|
|
You can disable the pager for certain commands by adding them to the
|
2009-07-23 02:24:03 +04:00
|
|
|
pager.ignore list::
|
2008-04-03 15:47:05 +04:00
|
|
|
|
|
|
|
[pager]
|
|
|
|
ignore = version, help, update
|
|
|
|
|
2009-07-26 03:56:02 +04:00
|
|
|
You can also enable the pager only for certain commands using
|
2009-11-12 18:29:40 +03:00
|
|
|
pager.attend. Below is the default list of commands to be paged::
|
2008-04-03 15:47:05 +04:00
|
|
|
|
|
|
|
[pager]
|
2009-11-12 18:29:40 +03:00
|
|
|
attend = annotate, cat, diff, export, glog, log, qdiff
|
|
|
|
|
|
|
|
Setting pager.attend to an empty value will cause all commands to be
|
|
|
|
paged.
|
2008-04-03 15:47:05 +04:00
|
|
|
|
|
|
|
If pager.attend is present, pager.ignore will be ignored.
|
|
|
|
|
2010-04-22 12:24:49 +04:00
|
|
|
To ignore global commands like :hg:`version` or :hg:`help`, you have
|
2010-08-28 06:36:35 +04:00
|
|
|
to specify them in your user configuration file.
|
2010-10-10 21:21:48 +04:00
|
|
|
|
|
|
|
The --pager=... option can also be used to control when the pager is
|
|
|
|
used. Use a boolean value like yes, no, on, off, or use auto for
|
|
|
|
normal behavior.
|
2008-04-03 15:47:05 +04:00
|
|
|
'''
|
2008-03-20 02:57:14 +03:00
|
|
|
|
2013-04-13 04:35:56 +04:00
|
|
|
import atexit, sys, os, signal, subprocess, errno, shlex
|
2013-10-13 05:51:34 +04:00
|
|
|
from mercurial import commands, dispatch, util, extensions, cmdutil
|
2010-10-10 21:21:48 +04:00
|
|
|
from mercurial.i18n import _
|
2008-03-20 02:57:14 +03:00
|
|
|
|
2012-05-15 23:37:49 +04:00
|
|
|
testedwith = 'internal'
|
|
|
|
|
2012-07-07 02:06:52 +04:00
|
|
|
def _pagerfork(ui, p):
|
|
|
|
if not util.safehasattr(os, 'fork'):
|
|
|
|
sys.stdout = util.popen(p, 'wb')
|
|
|
|
if ui._isatty(sys.stderr):
|
|
|
|
sys.stderr = sys.stdout
|
|
|
|
return
|
|
|
|
fdin, fdout = os.pipe()
|
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
os.close(fdin)
|
|
|
|
os.dup2(fdout, sys.stdout.fileno())
|
|
|
|
if ui._isatty(sys.stderr):
|
|
|
|
os.dup2(fdout, sys.stderr.fileno())
|
|
|
|
os.close(fdout)
|
|
|
|
return
|
|
|
|
os.dup2(fdin, sys.stdin.fileno())
|
|
|
|
os.close(fdin)
|
|
|
|
os.close(fdout)
|
|
|
|
try:
|
|
|
|
os.execvp('/bin/sh', ['/bin/sh', '-c', p])
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
# no /bin/sh, try executing the pager directly
|
|
|
|
args = shlex.split(p)
|
|
|
|
os.execvp(args[0], args)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def _pagersubprocess(ui, p):
|
2012-05-11 17:45:37 +04:00
|
|
|
pager = subprocess.Popen(p, shell=True, bufsize=-1,
|
|
|
|
close_fds=util.closefds, stdin=subprocess.PIPE,
|
|
|
|
stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
|
|
|
|
stdout = os.dup(sys.stdout.fileno())
|
|
|
|
stderr = os.dup(sys.stderr.fileno())
|
|
|
|
os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
|
2012-05-20 23:33:49 +04:00
|
|
|
if ui._isatty(sys.stderr):
|
2012-05-11 17:45:37 +04:00
|
|
|
os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
|
|
|
|
|
|
|
|
@atexit.register
|
|
|
|
def killpager():
|
2013-02-23 01:17:33 +04:00
|
|
|
if util.safehasattr(signal, "SIGINT"):
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
2012-05-11 17:45:37 +04:00
|
|
|
pager.stdin.close()
|
|
|
|
os.dup2(stdout, sys.stdout.fileno())
|
|
|
|
os.dup2(stderr, sys.stderr.fileno())
|
|
|
|
pager.wait()
|
2010-05-03 23:00:34 +04:00
|
|
|
|
2012-07-07 02:06:52 +04:00
|
|
|
def _runpager(ui, p):
|
|
|
|
# The subprocess module shipped with Python <= 2.4 is buggy (issue3533).
|
|
|
|
# The compat version is buggy on Windows (issue3225), but has been shipping
|
|
|
|
# with hg for a long time. Preserve existing functionality.
|
|
|
|
if sys.version_info >= (2, 5):
|
|
|
|
_pagersubprocess(ui, p)
|
|
|
|
else:
|
|
|
|
_pagerfork(ui, p)
|
|
|
|
|
2008-03-20 02:57:14 +03:00
|
|
|
def uisetup(ui):
|
2012-05-20 23:33:49 +04:00
|
|
|
if '--debugger' in sys.argv or not ui.formatted():
|
2010-06-19 20:20:12 +04:00
|
|
|
return
|
|
|
|
|
2008-10-23 02:34:52 +04:00
|
|
|
def pagecmd(orig, ui, options, cmd, cmdfunc):
|
2008-03-29 21:41:50 +03:00
|
|
|
p = ui.config("pager", "pager", os.environ.get("PAGER"))
|
2011-06-02 01:43:34 +04:00
|
|
|
|
|
|
|
if p:
|
2009-11-12 18:29:40 +03:00
|
|
|
attend = ui.configlist('pager', 'attend', attended)
|
2010-10-10 21:21:48 +04:00
|
|
|
auto = options['pager'] == 'auto'
|
|
|
|
always = util.parsebool(options['pager'])
|
2013-10-13 05:51:34 +04:00
|
|
|
|
|
|
|
cmds, _ = cmdutil.findcmd(cmd, commands.table)
|
|
|
|
|
|
|
|
ignore = ui.configlist('pager', 'ignore')
|
|
|
|
for cmd in cmds:
|
|
|
|
if (always or auto and
|
|
|
|
(cmd in attend or
|
|
|
|
(cmd not in ignore and not attend))):
|
2014-03-19 05:45:14 +04:00
|
|
|
ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
|
|
|
|
ui.setconfig('ui', 'interactive', False, 'pager')
|
2013-10-13 05:51:34 +04:00
|
|
|
if util.safehasattr(signal, "SIGPIPE"):
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
_runpager(ui, p)
|
|
|
|
break
|
2008-10-23 02:34:52 +04:00
|
|
|
return orig(ui, options, cmd, cmdfunc)
|
2008-03-29 21:41:50 +03:00
|
|
|
|
2008-10-23 02:34:52 +04:00
|
|
|
extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
|
2009-11-12 18:29:40 +03:00
|
|
|
|
2010-10-10 21:21:48 +04:00
|
|
|
def extsetup(ui):
|
|
|
|
commands.globalopts.append(
|
|
|
|
('', 'pager', 'auto',
|
|
|
|
_("when to paginate (boolean, always, auto, or never)"),
|
|
|
|
_('TYPE')))
|
|
|
|
|
2009-11-12 18:29:40 +03:00
|
|
|
attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
|