Show More
@@ -1,113 +1,140 | |||||
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='FSRX' less |
|
20 | pager = LESS='FSRX' less | |
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 | By default, the pager is only executed if a command has output. To | |||
|
26 | force the pager to run even if a command prints nothing, set:: | |||
|
27 | ||||
|
28 | [pager] | |||
|
29 | force = True | |||
|
30 | ||||
25 | If you notice "BROKEN PIPE" error messages, you can disable them by |
|
31 | If you notice "BROKEN PIPE" error messages, you can disable them by | |
26 | setting:: |
|
32 | setting:: | |
27 |
|
33 | |||
28 | [pager] |
|
34 | [pager] | |
29 | quiet = True |
|
35 | quiet = True | |
30 |
|
36 | |||
31 | You can disable the pager for certain commands by adding them to the |
|
37 | You can disable the pager for certain commands by adding them to the | |
32 | pager.ignore list:: |
|
38 | pager.ignore list:: | |
33 |
|
39 | |||
34 | [pager] |
|
40 | [pager] | |
35 | ignore = version, help, update |
|
41 | ignore = version, help, update | |
36 |
|
42 | |||
37 | You can also enable the pager only for certain commands using |
|
43 | You can also enable the pager only for certain commands using | |
38 | pager.attend. Below is the default list of commands to be paged:: |
|
44 | pager.attend. Below is the default list of commands to be paged:: | |
39 |
|
45 | |||
40 | [pager] |
|
46 | [pager] | |
41 | attend = annotate, cat, diff, export, glog, log, qdiff |
|
47 | attend = annotate, cat, diff, export, glog, log, qdiff | |
42 |
|
48 | |||
43 | Setting pager.attend to an empty value will cause all commands to be |
|
49 | Setting pager.attend to an empty value will cause all commands to be | |
44 | paged. |
|
50 | paged. | |
45 |
|
51 | |||
46 | If pager.attend is present, pager.ignore will be ignored. |
|
52 | If pager.attend is present, pager.ignore will be ignored. | |
47 |
|
53 | |||
48 | To ignore global commands like :hg:`version` or :hg:`help`, you have |
|
54 | To ignore global commands like :hg:`version` or :hg:`help`, you have | |
49 | to specify them in your user configuration file. |
|
55 | to specify them in your user configuration file. | |
50 |
|
56 | |||
51 | The --pager=... option can also be used to control when the pager is |
|
57 | 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 |
|
58 | used. Use a boolean value like yes, no, on, off, or use auto for | |
53 | normal behavior. |
|
59 | normal behavior. | |
54 | ''' |
|
60 | ''' | |
55 |
|
61 | |||
56 | import sys, os, signal, shlex, errno |
|
62 | import sys, os, signal, shlex, errno | |
57 | from mercurial import commands, dispatch, util, extensions |
|
63 | from mercurial import commands, dispatch, util, extensions | |
58 | from mercurial.i18n import _ |
|
64 | from mercurial.i18n import _ | |
59 |
|
65 | |||
60 | def _runpager(p): |
|
66 | def _runpager(p, sigpipe=False): | |
61 | if not hasattr(os, 'fork'): |
|
67 | if not hasattr(os, 'fork'): | |
62 | sys.stderr = sys.stdout = util.popen(p, 'wb') |
|
68 | sys.stderr = sys.stdout = util.popen(p, 'wb') | |
63 | return |
|
69 | return | |
64 | fdin, fdout = os.pipe() |
|
70 | fdin, fdout = os.pipe() | |
65 | pid = os.fork() |
|
71 | pid = os.fork() | |
66 | if pid == 0: |
|
72 | if pid == 0: | |
67 | os.close(fdin) |
|
73 | os.close(fdin) | |
68 | os.dup2(fdout, sys.stdout.fileno()) |
|
74 | os.dup2(fdout, sys.stdout.fileno()) | |
69 | os.dup2(fdout, sys.stderr.fileno()) |
|
75 | os.dup2(fdout, sys.stderr.fileno()) | |
70 | os.close(fdout) |
|
76 | os.close(fdout) | |
|
77 | if sigpipe: | |||
|
78 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | |||
71 | return |
|
79 | return | |
72 | os.dup2(fdin, sys.stdin.fileno()) |
|
80 | os.dup2(fdin, sys.stdin.fileno()) | |
73 | os.close(fdin) |
|
81 | os.close(fdin) | |
74 | os.close(fdout) |
|
82 | os.close(fdout) | |
75 | try: |
|
83 | try: | |
76 | os.execvp('/bin/sh', ['/bin/sh', '-c', p]) |
|
84 | os.execvp('/bin/sh', ['/bin/sh', '-c', p]) | |
77 | except OSError, e: |
|
85 | except OSError, e: | |
78 | if e.errno == errno.ENOENT: |
|
86 | if e.errno == errno.ENOENT: | |
79 | # no /bin/sh, try executing the pager directly |
|
87 | # no /bin/sh, try executing the pager directly | |
80 | args = shlex.split(p) |
|
88 | args = shlex.split(p) | |
81 | os.execvp(args[0], args) |
|
89 | os.execvp(args[0], args) | |
82 | else: |
|
90 | else: | |
83 | raise |
|
91 | raise | |
84 |
|
92 | |||
85 | def uisetup(ui): |
|
93 | def uisetup(ui): | |
86 | if ui.plain(): |
|
94 | if ui.plain(): | |
87 | return |
|
95 | return | |
88 |
|
96 | |||
|
97 | class pagerui(ui.__class__): | |||
|
98 | _pager = None | |||
|
99 | _pagerstarted = False | |||
|
100 | ||||
|
101 | def write(self, *args, **opts): | |||
|
102 | if self._pager and not self._pagerstarted: | |||
|
103 | self._pagerstarted = True | |||
|
104 | self._pager() | |||
|
105 | return super(pagerui, self).write(*args, **opts) | |||
|
106 | ||||
|
107 | def write_err(self, *args, **opts): | |||
|
108 | if self._pager and not self._pagerstarted: | |||
|
109 | self._pagerstarted = True | |||
|
110 | self._pager() | |||
|
111 | return super(pagerui, self).write(*args, **opts) | |||
|
112 | ui.__class__ = pagerui | |||
|
113 | ||||
89 | def pagecmd(orig, ui, options, cmd, cmdfunc): |
|
114 | def pagecmd(orig, ui, options, cmd, cmdfunc): | |
90 | p = ui.config("pager", "pager", os.environ.get("PAGER")) |
|
115 | p = ui.config("pager", "pager", os.environ.get("PAGER")) | |
91 | if p and sys.stdout.isatty() and '--debugger' not in sys.argv: |
|
116 | if p and sys.stdout.isatty() and '--debugger' not in sys.argv: | |
92 | attend = ui.configlist('pager', 'attend', attended) |
|
117 | attend = ui.configlist('pager', 'attend', attended) | |
93 | auto = options['pager'] == 'auto' |
|
118 | auto = options['pager'] == 'auto' | |
94 | always = util.parsebool(options['pager']) |
|
119 | always = util.parsebool(options['pager']) | |
95 | if (always or auto and |
|
120 | if (always or auto and | |
96 | (cmd in attend or |
|
121 | (cmd in attend or | |
97 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): |
|
122 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): | |
98 | ui.setconfig('ui', 'formatted', ui.formatted()) |
|
123 | ui.setconfig('ui', 'formatted', ui.formatted()) | |
99 | ui.setconfig('ui', 'interactive', False) |
|
124 | ui.setconfig('ui', 'interactive', False) | |
100 | _runpager(p) |
|
125 | sigpipe = ui.configbool('pager', 'quiet') | |
101 |
if ui.configbool('pager', ' |
|
126 | if ui.configbool('pager', 'force'): | |
102 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
|
127 | _runpager(p, sigpipe) | |
|
128 | else: | |||
|
129 | ui._pager = lambda: _runpager(p, sigpipe) | |||
103 | return orig(ui, options, cmd, cmdfunc) |
|
130 | return orig(ui, options, cmd, cmdfunc) | |
104 |
|
131 | |||
105 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) |
|
132 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) | |
106 |
|
133 | |||
107 | def extsetup(ui): |
|
134 | def extsetup(ui): | |
108 | commands.globalopts.append( |
|
135 | commands.globalopts.append( | |
109 | ('', 'pager', 'auto', |
|
136 | ('', 'pager', 'auto', | |
110 | _("when to paginate (boolean, always, auto, or never)"), |
|
137 | _("when to paginate (boolean, always, auto, or never)"), | |
111 | _('TYPE'))) |
|
138 | _('TYPE'))) | |
112 |
|
139 | |||
113 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
|
140 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
General Comments 0
You need to be logged in to leave comments.
Login now