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]
|
2010-10-29 09:57:34 +04:00
|
|
|
pager = less -FRSX
|
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
|
|
|
|
2012-05-11 17:45:37 +04:00
|
|
|
import atexit, sys, os, signal, subprocess
|
2010-10-10 21:21:48 +04:00
|
|
|
from mercurial import commands, dispatch, util, extensions
|
|
|
|
from mercurial.i18n import _
|
2008-03-20 02:57:14 +03:00
|
|
|
|
2010-10-29 16:47:45 +04:00
|
|
|
def _runpager(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())
|
|
|
|
if util.isatty(sys.stderr):
|
|
|
|
os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
|
|
|
|
|
|
|
|
@atexit.register
|
|
|
|
def killpager():
|
|
|
|
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
|
|
|
|
2008-03-20 02:57:14 +03:00
|
|
|
def uisetup(ui):
|
2011-06-02 01:43:34 +04:00
|
|
|
if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout):
|
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'])
|
|
|
|
if (always or auto and
|
|
|
|
(cmd in attend or
|
|
|
|
(cmd not in ui.configlist('pager', 'ignore') and not attend))):
|
2010-06-07 17:34:48 +04:00
|
|
|
ui.setconfig('ui', 'formatted', ui.formatted())
|
2010-02-20 06:16:42 +03:00
|
|
|
ui.setconfig('ui', 'interactive', False)
|
2012-05-12 11:43:12 +04:00
|
|
|
if util.safehasattr(signal, "SIGPIPE"):
|
2010-10-29 16:47:45 +04:00
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
2012-05-11 18:08:49 +04:00
|
|
|
_runpager(p)
|
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']
|