Show More
@@ -1,103 +1,101 b'' | |||||
1 | # pager.py - display output using a pager |
|
1 | # pager.py - display output using a pager | |
2 | # |
|
2 | # | |
3 | # Copyright 2008 David Soria Parra <dsp@php.net> |
|
3 | # Copyright 2008 David Soria Parra <dsp@php.net> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 | # |
|
7 | # | |
8 | # To load the extension, add it to your configuration file: |
|
8 | # To load the extension, add it to your configuration file: | |
9 | # |
|
9 | # | |
10 | # [extension] |
|
10 | # [extension] | |
11 | # pager = |
|
11 | # pager = | |
12 | # |
|
12 | # | |
13 | # Run "hg help pager" to get info on configuration. |
|
13 | # Run "hg help pager" to get info on configuration. | |
14 |
|
14 | |||
15 | '''browse command output with an external pager |
|
15 | '''browse command output with an external pager | |
16 |
|
16 | |||
17 | To set the pager that should be used, set the application variable:: |
|
17 | To set the pager that should be used, set the application variable:: | |
18 |
|
18 | |||
19 | [pager] |
|
19 | [pager] | |
20 | pager = less -FRSX |
|
20 | pager = less -FRSX | |
21 |
|
21 | |||
22 | If no pager is set, the pager extensions uses the environment variable |
|
22 | If no pager is set, the pager extensions uses the environment variable | |
23 | $PAGER. If neither pager.pager, nor $PAGER is set, no pager is used. |
|
23 | $PAGER. If neither pager.pager, nor $PAGER is set, no pager is used. | |
24 |
|
24 | |||
25 | You can disable the pager for certain commands by adding them to the |
|
25 | You can disable the pager for certain commands by adding them to the | |
26 | pager.ignore list:: |
|
26 | pager.ignore list:: | |
27 |
|
27 | |||
28 | [pager] |
|
28 | [pager] | |
29 | ignore = version, help, update |
|
29 | ignore = version, help, update | |
30 |
|
30 | |||
31 | You can also enable the pager only for certain commands using |
|
31 | You can also enable the pager only for certain commands using | |
32 | pager.attend. Below is the default list of commands to be paged:: |
|
32 | pager.attend. Below is the default list of commands to be paged:: | |
33 |
|
33 | |||
34 | [pager] |
|
34 | [pager] | |
35 | attend = annotate, cat, diff, export, glog, log, qdiff |
|
35 | attend = annotate, cat, diff, export, glog, log, qdiff | |
36 |
|
36 | |||
37 | Setting pager.attend to an empty value will cause all commands to be |
|
37 | Setting pager.attend to an empty value will cause all commands to be | |
38 | paged. |
|
38 | paged. | |
39 |
|
39 | |||
40 | If pager.attend is present, pager.ignore will be ignored. |
|
40 | If pager.attend is present, pager.ignore will be ignored. | |
41 |
|
41 | |||
42 | To ignore global commands like :hg:`version` or :hg:`help`, you have |
|
42 | To ignore global commands like :hg:`version` or :hg:`help`, you have | |
43 | to specify them in your user configuration file. |
|
43 | to specify them in your user configuration file. | |
44 |
|
44 | |||
45 | The --pager=... option can also be used to control when the pager is |
|
45 | The --pager=... option can also be used to control when the pager is | |
46 | used. Use a boolean value like yes, no, on, off, or use auto for |
|
46 | used. Use a boolean value like yes, no, on, off, or use auto for | |
47 | normal behavior. |
|
47 | normal behavior. | |
48 | ''' |
|
48 | ''' | |
49 |
|
49 | |||
50 | import atexit, sys, os, signal, subprocess |
|
50 | import atexit, sys, os, signal, subprocess | |
51 | from mercurial import commands, dispatch, util, extensions |
|
51 | from mercurial import commands, dispatch, util, extensions | |
52 | from mercurial.i18n import _ |
|
52 | from mercurial.i18n import _ | |
53 |
|
53 | |||
54 | def _runpager(p): |
|
54 | def _runpager(p): | |
55 | pager = subprocess.Popen(p, shell=True, bufsize=-1, |
|
55 | pager = subprocess.Popen(p, shell=True, bufsize=-1, | |
56 | close_fds=util.closefds, stdin=subprocess.PIPE, |
|
56 | close_fds=util.closefds, stdin=subprocess.PIPE, | |
57 | stdout=sys.stdout, stderr=sys.stderr) |
|
57 | stdout=sys.stdout, stderr=sys.stderr) | |
58 |
|
58 | |||
59 | stdout = os.dup(sys.stdout.fileno()) |
|
59 | stdout = os.dup(sys.stdout.fileno()) | |
60 | stderr = os.dup(sys.stderr.fileno()) |
|
60 | stderr = os.dup(sys.stderr.fileno()) | |
61 | os.dup2(pager.stdin.fileno(), sys.stdout.fileno()) |
|
61 | os.dup2(pager.stdin.fileno(), sys.stdout.fileno()) | |
62 | if util.isatty(sys.stderr): |
|
62 | if util.isatty(sys.stderr): | |
63 | os.dup2(pager.stdin.fileno(), sys.stderr.fileno()) |
|
63 | os.dup2(pager.stdin.fileno(), sys.stderr.fileno()) | |
64 |
|
64 | |||
65 | @atexit.register |
|
65 | @atexit.register | |
66 | def killpager(): |
|
66 | def killpager(): | |
67 | pager.stdin.close() |
|
67 | pager.stdin.close() | |
68 | os.dup2(stdout, sys.stdout.fileno()) |
|
68 | os.dup2(stdout, sys.stdout.fileno()) | |
69 | os.dup2(stderr, sys.stderr.fileno()) |
|
69 | os.dup2(stderr, sys.stderr.fileno()) | |
70 | pager.wait() |
|
70 | pager.wait() | |
71 |
|
71 | |||
72 | def uisetup(ui): |
|
72 | def uisetup(ui): | |
73 | if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout): |
|
73 | if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout): | |
74 | return |
|
74 | return | |
75 |
|
75 | |||
76 | def pagecmd(orig, ui, options, cmd, cmdfunc): |
|
76 | def pagecmd(orig, ui, options, cmd, cmdfunc): | |
77 | p = ui.config("pager", "pager", os.environ.get("PAGER")) |
|
77 | p = ui.config("pager", "pager", os.environ.get("PAGER")) | |
78 |
|
78 | |||
79 | if p: |
|
79 | if p: | |
80 | attend = ui.configlist('pager', 'attend', attended) |
|
80 | attend = ui.configlist('pager', 'attend', attended) | |
81 | auto = options['pager'] == 'auto' |
|
81 | auto = options['pager'] == 'auto' | |
82 | always = util.parsebool(options['pager']) |
|
82 | always = util.parsebool(options['pager']) | |
83 | if (always or auto and |
|
83 | if (always or auto and | |
84 | (cmd in attend or |
|
84 | (cmd in attend or | |
85 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): |
|
85 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): | |
86 | ui.setconfig('ui', 'formatted', ui.formatted()) |
|
86 | ui.setconfig('ui', 'formatted', ui.formatted()) | |
87 | ui.setconfig('ui', 'interactive', False) |
|
87 | ui.setconfig('ui', 'interactive', False) | |
88 | try: |
|
88 | if util.safehasattr(signal, "SIGPIPE"): | |
89 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
|
89 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | |
90 | except ValueError: |
|
|||
91 | pass |
|
|||
92 | _runpager(p) |
|
90 | _runpager(p) | |
93 | return orig(ui, options, cmd, cmdfunc) |
|
91 | return orig(ui, options, cmd, cmdfunc) | |
94 |
|
92 | |||
95 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) |
|
93 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) | |
96 |
|
94 | |||
97 | def extsetup(ui): |
|
95 | def extsetup(ui): | |
98 | commands.globalopts.append( |
|
96 | commands.globalopts.append( | |
99 | ('', 'pager', 'auto', |
|
97 | ('', 'pager', 'auto', | |
100 | _("when to paginate (boolean, always, auto, or never)"), |
|
98 | _("when to paginate (boolean, always, auto, or never)"), | |
101 | _('TYPE'))) |
|
99 | _('TYPE'))) | |
102 |
|
100 | |||
103 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
|
101 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
General Comments 0
You need to be logged in to leave comments.
Login now