Show More
@@ -1,107 +1,103 | |||||
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 | If you notice "BROKEN PIPE" error messages, you can disable them by |
|
|||
26 | setting:: |
|
|||
27 |
|
||||
28 | [pager] |
|
|||
29 | quiet = True |
|
|||
30 |
|
||||
31 | 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 | |
32 | pager.ignore list:: |
|
26 | pager.ignore list:: | |
33 |
|
27 | |||
34 | [pager] |
|
28 | [pager] | |
35 | ignore = version, help, update |
|
29 | ignore = version, help, update | |
36 |
|
30 | |||
37 | You can also enable the pager only for certain commands using |
|
31 | You can also enable the pager only for certain commands using | |
38 | 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:: | |
39 |
|
33 | |||
40 | [pager] |
|
34 | [pager] | |
41 | attend = annotate, cat, diff, export, glog, log, qdiff |
|
35 | attend = annotate, cat, diff, export, glog, log, qdiff | |
42 |
|
36 | |||
43 | 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 | |
44 | paged. |
|
38 | paged. | |
45 |
|
39 | |||
46 | If pager.attend is present, pager.ignore will be ignored. |
|
40 | If pager.attend is present, pager.ignore will be ignored. | |
47 |
|
41 | |||
48 | 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 | |
49 | to specify them in your user configuration file. |
|
43 | to specify them in your user configuration file. | |
50 |
|
44 | |||
51 | 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 | |
52 | 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 | |
53 | normal behavior. |
|
47 | normal behavior. | |
54 | ''' |
|
48 | ''' | |
55 |
|
49 | |||
56 | import atexit, sys, os, signal, subprocess |
|
50 | import atexit, sys, os, signal, subprocess | |
57 | from mercurial import commands, dispatch, util, extensions |
|
51 | from mercurial import commands, dispatch, util, extensions | |
58 | from mercurial.i18n import _ |
|
52 | from mercurial.i18n import _ | |
59 |
|
53 | |||
60 | def _runpager(p): |
|
54 | def _runpager(p): | |
61 | pager = subprocess.Popen(p, shell=True, bufsize=-1, |
|
55 | pager = subprocess.Popen(p, shell=True, bufsize=-1, | |
62 | close_fds=util.closefds, stdin=subprocess.PIPE, |
|
56 | close_fds=util.closefds, stdin=subprocess.PIPE, | |
63 | stdout=sys.stdout, stderr=sys.stderr) |
|
57 | stdout=sys.stdout, stderr=sys.stderr) | |
64 |
|
58 | |||
65 | stdout = os.dup(sys.stdout.fileno()) |
|
59 | stdout = os.dup(sys.stdout.fileno()) | |
66 | stderr = os.dup(sys.stderr.fileno()) |
|
60 | stderr = os.dup(sys.stderr.fileno()) | |
67 | os.dup2(pager.stdin.fileno(), sys.stdout.fileno()) |
|
61 | os.dup2(pager.stdin.fileno(), sys.stdout.fileno()) | |
68 | if util.isatty(sys.stderr): |
|
62 | if util.isatty(sys.stderr): | |
69 | os.dup2(pager.stdin.fileno(), sys.stderr.fileno()) |
|
63 | os.dup2(pager.stdin.fileno(), sys.stderr.fileno()) | |
70 |
|
64 | |||
71 | @atexit.register |
|
65 | @atexit.register | |
72 | def killpager(): |
|
66 | def killpager(): | |
73 | pager.stdin.close() |
|
67 | pager.stdin.close() | |
74 | os.dup2(stdout, sys.stdout.fileno()) |
|
68 | os.dup2(stdout, sys.stdout.fileno()) | |
75 | os.dup2(stderr, sys.stderr.fileno()) |
|
69 | os.dup2(stderr, sys.stderr.fileno()) | |
76 | pager.wait() |
|
70 | pager.wait() | |
77 |
|
71 | |||
78 | def uisetup(ui): |
|
72 | def uisetup(ui): | |
79 | 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): | |
80 | return |
|
74 | return | |
81 |
|
75 | |||
82 | def pagecmd(orig, ui, options, cmd, cmdfunc): |
|
76 | def pagecmd(orig, ui, options, cmd, cmdfunc): | |
83 | p = ui.config("pager", "pager", os.environ.get("PAGER")) |
|
77 | p = ui.config("pager", "pager", os.environ.get("PAGER")) | |
84 |
|
78 | |||
85 | if p: |
|
79 | if p: | |
86 | attend = ui.configlist('pager', 'attend', attended) |
|
80 | attend = ui.configlist('pager', 'attend', attended) | |
87 | auto = options['pager'] == 'auto' |
|
81 | auto = options['pager'] == 'auto' | |
88 | always = util.parsebool(options['pager']) |
|
82 | always = util.parsebool(options['pager']) | |
89 | if (always or auto and |
|
83 | if (always or auto and | |
90 | (cmd in attend or |
|
84 | (cmd in attend or | |
91 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): |
|
85 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): | |
92 | ui.setconfig('ui', 'formatted', ui.formatted()) |
|
86 | ui.setconfig('ui', 'formatted', ui.formatted()) | |
93 | ui.setconfig('ui', 'interactive', False) |
|
87 | ui.setconfig('ui', 'interactive', False) | |
|
88 | try: | |||
|
89 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | |||
|
90 | except ValueError: | |||
|
91 | pass | |||
94 | _runpager(p) |
|
92 | _runpager(p) | |
95 | if ui.configbool('pager', 'quiet'): |
|
|||
96 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
|
|||
97 | return orig(ui, options, cmd, cmdfunc) |
|
93 | return orig(ui, options, cmd, cmdfunc) | |
98 |
|
94 | |||
99 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) |
|
95 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) | |
100 |
|
96 | |||
101 | def extsetup(ui): |
|
97 | def extsetup(ui): | |
102 | commands.globalopts.append( |
|
98 | commands.globalopts.append( | |
103 | ('', 'pager', 'auto', |
|
99 | ('', 'pager', 'auto', | |
104 | _("when to paginate (boolean, always, auto, or never)"), |
|
100 | _("when to paginate (boolean, always, auto, or never)"), | |
105 | _('TYPE'))) |
|
101 | _('TYPE'))) | |
106 |
|
102 | |||
107 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
|
103 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
General Comments 0
You need to be logged in to leave comments.
Login now