hgext/pager.py
author Matt Mackall <mpm@selenic.com>
Mon, 06 Feb 2012 15:22:43 -0600
changeset 16088 6c1daae0ae7b
parent 14945 11aad09a6370
child 16631 369741ef7253
permissions -rw-r--r--
merge with stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     1
# pager.py - display output using a pager
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     2
#
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     3
# Copyright 2008 David Soria Parra <dsp@php.net>
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7995
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10112
diff changeset
     6
# GNU General Public License version 2 or any later version.
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     7
#
12083
ebfc46929f3e help: refer to user configuration file more consistently
Brodie Rao <brodie@bitheap.org>
parents: 11414
diff changeset
     8
# To load the extension, add it to your configuration file:
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     9
#
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    10
#   [extension]
10112
703db37d186b hgext: enable extensions without "hgext." prefix in help texts
Martin Geisler <mg@lazybytes.net>
parents: 9841
diff changeset
    11
#   pager =
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    12
#
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    13
# Run "hg help pager" to get info on configuration.
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    14
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8225
diff changeset
    15
'''browse command output with an external pager
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    16
9212
dfc1d5da98f0 pager: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9069
diff changeset
    17
To set the pager that should be used, set the application variable::
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    18
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    19
  [pager]
12892
919c440868d9 pager: make example of setting pager less silly
Kevin Bullock <kbullock@ringworld.org>
parents: 12876
diff changeset
    20
  pager = less -FRSX
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    21
9267
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    22
If no pager is set, the pager extensions uses the environment variable
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    23
$PAGER. If neither pager.pager, nor $PAGER is set, no pager is used.
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    24
9267
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    25
If you notice "BROKEN PIPE" error messages, you can disable them by
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    26
setting::
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    27
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    28
  [pager]
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    29
  quiet = True
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    30
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    31
You can disable the pager for certain commands by adding them to the
9212
dfc1d5da98f0 pager: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9069
diff changeset
    32
pager.ignore list::
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    33
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    34
  [pager]
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    35
  ignore = version, help, update
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    36
9267
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    37
You can also enable the pager only for certain commands using
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    38
pager.attend. Below is the default list of commands to be paged::
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    39
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    40
  [pager]
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    41
  attend = annotate, cat, diff, export, glog, log, qdiff
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    42
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    43
Setting pager.attend to an empty value will cause all commands to be
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    44
paged.
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    45
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    46
If pager.attend is present, pager.ignore will be ignored.
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    47
10973
49a07f441496 Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents: 10516
diff changeset
    48
To ignore global commands like :hg:`version` or :hg:`help`, you have
12083
ebfc46929f3e help: refer to user configuration file more consistently
Brodie Rao <brodie@bitheap.org>
parents: 11414
diff changeset
    49
to specify them in your user configuration file.
12694
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    50
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    51
The --pager=... option can also be used to control when the pager is
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    52
used. Use a boolean value like yes, no, on, off, or use auto for
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    53
normal behavior.
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    54
'''
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    55
11240
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    56
import sys, os, signal, shlex, errno
12694
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    57
from mercurial import commands, dispatch, util, extensions
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    58
from mercurial.i18n import _
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    59
12876
a3b182dd548a pager: backout 05077896ffe2 (issue2441)
Martin Geisler <mg@aragost.com>
parents: 12695
diff changeset
    60
def _runpager(p):
14945
11aad09a6370 hgext: replace uses of hasattr with util.safehasattr
Augie Fackler <durin42@gmail.com>
parents: 14515
diff changeset
    61
    if not util.safehasattr(os, 'fork'):
13642
994510694b1d pager: don't page stderr if it's being redirected (issue2541)
Brodie Rao <brodie@bitheap.org>
parents: 12892
diff changeset
    62
        sys.stdout = util.popen(p, 'wb')
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    63
        if util.isatty(sys.stderr):
13642
994510694b1d pager: don't page stderr if it's being redirected (issue2541)
Brodie Rao <brodie@bitheap.org>
parents: 12892
diff changeset
    64
            sys.stderr = sys.stdout
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    65
        return
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    66
    fdin, fdout = os.pipe()
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    67
    pid = os.fork()
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    68
    if pid == 0:
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    69
        os.close(fdin)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    70
        os.dup2(fdout, sys.stdout.fileno())
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    71
        if util.isatty(sys.stderr):
13642
994510694b1d pager: don't page stderr if it's being redirected (issue2541)
Brodie Rao <brodie@bitheap.org>
parents: 12892
diff changeset
    72
            os.dup2(fdout, sys.stderr.fileno())
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    73
        os.close(fdout)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    74
        return
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    75
    os.dup2(fdin, sys.stdin.fileno())
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    76
    os.close(fdin)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    77
    os.close(fdout)
11240
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    78
    try:
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    79
        os.execvp('/bin/sh', ['/bin/sh', '-c', p])
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    80
    except OSError, e:
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    81
        if e.errno == errno.ENOENT:
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    82
            # no /bin/sh, try executing the pager directly
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    83
            args = shlex.split(p)
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    84
            os.execvp(args[0], args)
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    85
        else:
ccb4057e19e6 pager: exec pager using /bin/sh -c
Brodie Rao <brodie@bitheap.org>
parents: 11182
diff changeset
    86
            raise
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    87
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    88
def uisetup(ui):
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    89
    if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout):
11414
0fa4474bdc2f pager: respect HGPLAIN
Yuya Nishihara <yuya@tcha.org>
parents: 11328
diff changeset
    90
        return
0fa4474bdc2f pager: respect HGPLAIN
Yuya Nishihara <yuya@tcha.org>
parents: 11328
diff changeset
    91
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    92
    def pagecmd(orig, ui, options, cmd, cmdfunc):
6417
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    93
        p = ui.config("pager", "pager", os.environ.get("PAGER"))
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    94
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    95
        if p:
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    96
            attend = ui.configlist('pager', 'attend', attended)
12694
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    97
            auto = options['pager'] == 'auto'
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    98
            always = util.parsebool(options['pager'])
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
    99
            if (always or auto and
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   100
                (cmd in attend or
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   101
                 (cmd not in ui.configlist('pager', 'ignore') and not attend))):
11328
d357d147f0d4 pager: set ui.formatted() prior to redirecting stdout.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 11241
diff changeset
   102
                ui.setconfig('ui', 'formatted', ui.formatted())
10516
80a1161bc3b5 pager: set ui.interactive=False when enabled
Brodie Rao <me+hg@dackz.net>
parents: 10263
diff changeset
   103
                ui.setconfig('ui', 'interactive', False)
12876
a3b182dd548a pager: backout 05077896ffe2 (issue2441)
Martin Geisler <mg@aragost.com>
parents: 12695
diff changeset
   104
                _runpager(p)
a3b182dd548a pager: backout 05077896ffe2 (issue2441)
Martin Geisler <mg@aragost.com>
parents: 12695
diff changeset
   105
                if ui.configbool('pager', 'quiet'):
a3b182dd548a pager: backout 05077896ffe2 (issue2441)
Martin Geisler <mg@aragost.com>
parents: 12695
diff changeset
   106
                    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
   107
        return orig(ui, options, cmd, cmdfunc)
6417
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
   108
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
   109
    extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
   110
12694
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   111
def extsetup(ui):
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   112
    commands.globalopts.append(
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   113
        ('', 'pager', 'auto',
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   114
         _("when to paginate (boolean, always, auto, or never)"),
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   115
         _('TYPE')))
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   116
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
   117
attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']