hgext/pager.py
author Augie Fackler <augie@google.com>
Wed, 15 Feb 2017 17:47:51 -0500
changeset 30992 61b4122019d3
parent 30897 253d5c0f3a2f
child 30993 9c2977ceaa46
permissions -rw-r--r--
pager: move pager-initiating code into core No functionality change. A previous version of this API had a category argument on ui.pager(). As I migrated the commands in core, I couldn't come up with good enough consistency in any categorization scheme so I just scrapped the whole idea. It may be worth revisiting in the future.
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
#
29967
bd55d98027ee pager: use single quotes in use warning
timeless <timeless@mozdev.org>
parents: 29841
diff changeset
    13
# Run 'hg help pager' to get info on configuration.
6462
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]
17305
e66fa4d587dc pager: drop -S option for less in example for pager configuration
Thomas Arendsen Hein <thomas@intevation.de>
parents: 17250
diff changeset
    20
  pager = less -FRX
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
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    25
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
    26
pager.ignore list::
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
  ignore = version, help, update
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    30
9267
bd9e5d200186 pager: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9212
diff changeset
    31
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
    32
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
    33
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    34
  [pager]
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    35
  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
    36
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    37
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
    38
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
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
    41
21281
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    42
Lastly, you can enable and disable paging for individual commands with
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    43
the attend-<command> option. This setting takes precedence over
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    44
existing attend and ignore options and defaults::
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    45
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    46
  [pager]
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    47
  attend-cat = false
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    48
10973
49a07f441496 Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents: 10516
diff changeset
    49
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
    50
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
    51
27128
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    52
To control whether the pager is used at all for an individual command,
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    53
you can use --pager=<value>::
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    54
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    55
  - use as needed: `auto`.
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    56
  - require the pager: `yes` or `on`.
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    57
  - suppress the pager: `no` or `off` (any unrecognized value
efceacd6a0c6 pager: improve help for --pager=
timeless <timeless@mozdev.org>
parents: 26454
diff changeset
    58
  will also work).
21281
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    59
6462
6c4e12682fb9 pager: make config info accessible with "hg help pager"
Christian Ebert <blacktrash@gmx.net>
parents: 6457
diff changeset
    60
'''
28320
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    61
from __future__ import absolute_import
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    62
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28554
diff changeset
    63
from mercurial.i18n import _
28320
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    64
from mercurial import (
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    65
    cmdutil,
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    66
    commands,
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    67
    dispatch,
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    68
    extensions,
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    69
    util,
63c2864af25f pager: use absolute_import
Augie Fackler <augie@google.com>
parents: 27128
diff changeset
    70
    )
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    71
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29205
diff changeset
    72
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24067
diff changeset
    73
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24067
diff changeset
    74
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24067
diff changeset
    75
# leave the attribute unspecified.
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29205
diff changeset
    76
testedwith = 'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16652
diff changeset
    77
6323
6e1308a09ffd Use the pager given by the environment to display long output
David Soria Parra <dsp@php.net>
parents:
diff changeset
    78
def uisetup(ui):
30722
117e15c30e6c pager: wrap ui._runpager
Jun Wu <quark@fb.com>
parents: 30635
diff changeset
    79
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
    80
    def pagecmd(orig, ui, options, cmd, cmdfunc):
21277
2bc778e2f9b3 pager: break pager invocation out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 20790
diff changeset
    81
        usepager = False
21278
e1f5f38fd944 pager: break always out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21277
diff changeset
    82
        always = util.parsebool(options['pager'])
21279
19b8cfe4396f pager: break auto out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21278
diff changeset
    83
        auto = options['pager'] == 'auto'
14515
76f295eaed86 util: add helper function isatty(fd) to check for tty-ness
Idan Kamara <idankk86@gmail.com>
parents: 13642
diff changeset
    84
30992
61b4122019d3 pager: move pager-initiating code into core
Augie Fackler <augie@google.com>
parents: 30897
diff changeset
    85
        if always:
21278
e1f5f38fd944 pager: break always out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21277
diff changeset
    86
            usepager = True
21279
19b8cfe4396f pager: break auto out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21278
diff changeset
    87
        elif not auto:
19b8cfe4396f pager: break auto out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21278
diff changeset
    88
            usepager = False
21277
2bc778e2f9b3 pager: break pager invocation out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 20790
diff changeset
    89
        else:
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
    90
            attend = ui.configlist('pager', 'attend', attended)
21280
71028188970e pager: variable reorder
Matt Mackall <mpm@selenic.com>
parents: 21279
diff changeset
    91
            ignore = ui.configlist('pager', 'ignore')
19940
7d99bff0f77c pager: honour internal aliases
David Soria Parra <dsp@experimentalworks.net>
parents: 18923
diff changeset
    92
            cmds, _ = cmdutil.findcmd(cmd, commands.table)
7d99bff0f77c pager: honour internal aliases
David Soria Parra <dsp@experimentalworks.net>
parents: 18923
diff changeset
    93
7d99bff0f77c pager: honour internal aliases
David Soria Parra <dsp@experimentalworks.net>
parents: 18923
diff changeset
    94
            for cmd in cmds:
21281
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    95
                var = 'attend-%s' % cmd
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    96
                if ui.config('pager', var):
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    97
                    usepager = ui.configbool('pager', var)
bcddddcf0b54 pager: add attend-<command> option
Matt Mackall <mpm@selenic.com>
parents: 21280
diff changeset
    98
                    break
21279
19b8cfe4396f pager: break auto out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21278
diff changeset
    99
                if (cmd in attend or
19b8cfe4396f pager: break auto out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 21278
diff changeset
   100
                     (cmd not in ignore and not attend)):
21277
2bc778e2f9b3 pager: break pager invocation out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 20790
diff changeset
   101
                    usepager = True
19940
7d99bff0f77c pager: honour internal aliases
David Soria Parra <dsp@experimentalworks.net>
parents: 18923
diff changeset
   102
                    break
21277
2bc778e2f9b3 pager: break pager invocation out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 20790
diff changeset
   103
2bc778e2f9b3 pager: break pager invocation out of command check loop
Matt Mackall <mpm@selenic.com>
parents: 20790
diff changeset
   104
        if usepager:
30992
61b4122019d3 pager: move pager-initiating code into core
Augie Fackler <augie@google.com>
parents: 30897
diff changeset
   105
            ui.pager('extension-via-attend-' + cmd)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
   106
        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
   107
24067
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   108
    # Wrap dispatch._runcommand after color is loaded so color can see
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   109
    # ui.pageractive. Otherwise, if we loaded first, color's wrapped
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   110
    # dispatch._runcommand would run without having access to ui.pageractive.
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   111
    def afterloaded(loaded):
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   112
        extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
0baf41e02a4d pager: ensure wrapped dispatch._runcommand runs before color's
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24066
diff changeset
   113
    extensions.afterloaded('color', afterloaded)
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
   114
12694
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   115
def extsetup(ui):
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   116
    commands.globalopts.append(
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   117
        ('', 'pager', 'auto',
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   118
         _("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
   119
         _('TYPE')))
04f6de46bf3a pager: add global --pager=<auto/boolean> option
Brodie Rao <brodie@bitheap.org>
parents: 12083
diff changeset
   120
9841
7cd6dee6fe37 pager: provide a default attend list
Brodie Rao <me+hg@dackz.net>
parents: 9267
diff changeset
   121
attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']