hgext/pager.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 03 Apr 2008 11:16:07 +0200
changeset 6457 7ef281e78c64
parent 6456 db5324d3c257
parent 6417 13fafd8cc4a1
child 6462 6c4e12682fb9
permissions -rw-r--r--
Merge from crew-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
#
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
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]
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    11
#   hgext.pager =
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    12
#
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    13
# To set the pager that should be used, set the application variable:
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    14
#
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    15
#   [pager]
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    16
#   pager = LESS='FSRX' less
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    17
#
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    18
# If no pager is set, the pager extensions uses the environment
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    19
# variable $PAGER. If neither pager.pager, nor $PAGER is set, no pager
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    20
# is used.
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    21
#
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    22
# If you notice "BROKEN PIPE" error messages, you can disable them
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    23
# by setting:
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    24
#
6324
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    25
#   [pager]
ee1077b41d5c pager: further simplify code, clean up comments
Matt Mackall <mpm@selenic.com>
parents: 6323
diff changeset
    26
#   quiet = True
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
    27
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    28
# You can disable the pager for certain commands by adding them to the
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    29
# pager.ignore list:
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    30
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    31
#   [pager]
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    32
#   ignore = version, help, update
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    33
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    34
# You can also enable the pager only for certain commands using pager.attend:
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    35
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    36
#   [pager]
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    37
#   attend = log
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    38
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    39
# If pager.attend is present, pager.ignore will be ignored.
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    40
#
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    41
# To ignore global commands like 'hg version' or 'hg help', you have to specify them
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    42
# in the global .hgrc
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    43
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    44
import sys, os, signal
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
    45
from mercurial import dispatch
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    46
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    47
def uisetup(ui):
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
    48
    def pagecmd(ui, options, cmd, cmdfunc):
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    49
        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
    50
        if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
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
    51
            attend = ui.configlist('pager', 'attend')
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    52
            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
    53
                (cmd not in ui.configlist('pager', 'ignore') and not attend)):
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    54
                sys.stderr = sys.stdout = os.popen(p, "wb")
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    55
                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
    56
                    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    57
        return oldrun(ui, options, cmd, cmdfunc)
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    58
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    59
    oldrun = dispatch._runcommand
13fafd8cc4a1 pager: Add a configuration to enable/disable the pager for certain commands
David Soria Parra <dsp <at> php.net>
parents: 6324
diff changeset
    60
    dispatch._runcommand = pagecmd