hgext/pager.py
author Brodie Rao <brodie@bitheap.org>
Mon, 03 May 2010 14:00:34 -0500
branchstable
changeset 11182 3c368a1c962d
parent 10516 80a1161bc3b5
child 11186 a890cc501501
child 11240 ccb4057e19e6
permissions -rw-r--r--
pager: fork and exec pager as parent process With the pager as the child process instead of the parent process, the termination of the parent Mercurial process can cause the terminal to return before the pager exits. Inverting the relationship prevents that issue. Platforms without fork() will continue to use util.popen().
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
#
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
     8
# To load the extension, add it to your .hgrc 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]
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    20
  pager = LESS='FSRX' less
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
9267
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    48
To ignore global commands like "hg version" or "hg help", you have to
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    49
specify them in the global .hgrc
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    50
'''
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    51
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    52
import sys, os, signal, shlex
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    53
from mercurial import dispatch, util, extensions
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    54
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    55
def _runpager(p):
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    56
    if not hasattr(os, 'fork'):
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    57
        sys.stderr = sys.stdout = util.popen(p, 'wb')
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    58
        return
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    59
    fdin, fdout = os.pipe()
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    60
    pid = os.fork()
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    61
    if pid == 0:
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    62
        os.close(fdin)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    63
        os.dup2(fdout, sys.stdout.fileno())
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    64
        os.dup2(fdout, sys.stderr.fileno())
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    65
        os.close(fdout)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    66
        return
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    67
    os.dup2(fdin, sys.stdin.fileno())
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    68
    os.close(fdin)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    69
    os.close(fdout)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    70
    args = shlex.split(p)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    71
    os.execvp(args[0], args)
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    72
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    73
def uisetup(ui):
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    74
    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
    75
        p = ui.config("pager", "pager", os.environ.get("PAGER"))
6457
7ef281e78c64 Merge from crew-stable.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6456 6417
diff changeset
    76
        if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    77
            attend = ui.configlist('pager', 'attend', attended)
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
    78
            if (cmd in attend or
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    79
                (cmd not in ui.configlist('pager', 'ignore') and not attend)):
10516
80a1161bc3b5 pager: set ui.interactive=False when enabled
Brodie Rao <me+hg@dackz.net>
parents: 10263
diff changeset
    80
                ui.setconfig('ui', 'interactive', False)
11182
3c368a1c962d pager: fork and exec pager as parent process
Brodie Rao <brodie@bitheap.org>
parents: 10516
diff changeset
    81
                _runpager(p)
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
    82
                if ui.configbool('pager', 'quiet'):
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    83
                    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    84
        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
    85
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    86
    extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    87
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    88
attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']