##// END OF EJS Templates
pager: don't terminate with extreme prejudice on SIGPIPE (BC)...
Simon Farnsworth -
r30867:aaa75158 default
parent child Browse files
Show More
@@ -1,174 +1,172 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 -FRX
20 pager = less -FRX
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 Lastly, you can enable and disable paging for individual commands with
42 Lastly, you can enable and disable paging for individual commands with
43 the attend-<command> option. This setting takes precedence over
43 the attend-<command> option. This setting takes precedence over
44 existing attend and ignore options and defaults::
44 existing attend and ignore options and defaults::
45
45
46 [pager]
46 [pager]
47 attend-cat = false
47 attend-cat = false
48
48
49 To ignore global commands like :hg:`version` or :hg:`help`, you have
49 To ignore global commands like :hg:`version` or :hg:`help`, you have
50 to specify them in your user configuration file.
50 to specify them in your user configuration file.
51
51
52 To control whether the pager is used at all for an individual command,
52 To control whether the pager is used at all for an individual command,
53 you can use --pager=<value>::
53 you can use --pager=<value>::
54
54
55 - use as needed: `auto`.
55 - use as needed: `auto`.
56 - require the pager: `yes` or `on`.
56 - require the pager: `yes` or `on`.
57 - suppress the pager: `no` or `off` (any unrecognized value
57 - suppress the pager: `no` or `off` (any unrecognized value
58 will also work).
58 will also work).
59
59
60 '''
60 '''
61 from __future__ import absolute_import
61 from __future__ import absolute_import
62
62
63 import atexit
63 import atexit
64 import os
64 import os
65 import signal
65 import signal
66 import subprocess
66 import subprocess
67 import sys
67 import sys
68
68
69 from mercurial.i18n import _
69 from mercurial.i18n import _
70 from mercurial import (
70 from mercurial import (
71 cmdutil,
71 cmdutil,
72 commands,
72 commands,
73 dispatch,
73 dispatch,
74 encoding,
74 encoding,
75 extensions,
75 extensions,
76 util,
76 util,
77 )
77 )
78
78
79 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
79 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
80 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
81 # be specifying the version(s) of Mercurial they are tested with, or
81 # be specifying the version(s) of Mercurial they are tested with, or
82 # leave the attribute unspecified.
82 # leave the attribute unspecified.
83 testedwith = 'ships-with-hg-core'
83 testedwith = 'ships-with-hg-core'
84
84
85 def _runpager(ui, p):
85 def _runpager(ui, p):
86 pager = subprocess.Popen(p, shell=True, bufsize=-1,
86 pager = subprocess.Popen(p, shell=True, bufsize=-1,
87 close_fds=util.closefds, stdin=subprocess.PIPE,
87 close_fds=util.closefds, stdin=subprocess.PIPE,
88 stdout=util.stdout, stderr=util.stderr)
88 stdout=util.stdout, stderr=util.stderr)
89
89
90 # back up original file objects and descriptors
90 # back up original file objects and descriptors
91 olduifout = ui.fout
91 olduifout = ui.fout
92 oldstdout = util.stdout
92 oldstdout = util.stdout
93 stdoutfd = os.dup(util.stdout.fileno())
93 stdoutfd = os.dup(util.stdout.fileno())
94 stderrfd = os.dup(util.stderr.fileno())
94 stderrfd = os.dup(util.stderr.fileno())
95
95
96 # create new line-buffered stdout so that output can show up immediately
96 # create new line-buffered stdout so that output can show up immediately
97 ui.fout = util.stdout = newstdout = os.fdopen(util.stdout.fileno(), 'wb', 1)
97 ui.fout = util.stdout = newstdout = os.fdopen(util.stdout.fileno(), 'wb', 1)
98 os.dup2(pager.stdin.fileno(), util.stdout.fileno())
98 os.dup2(pager.stdin.fileno(), util.stdout.fileno())
99 if ui._isatty(util.stderr):
99 if ui._isatty(util.stderr):
100 os.dup2(pager.stdin.fileno(), util.stderr.fileno())
100 os.dup2(pager.stdin.fileno(), util.stderr.fileno())
101
101
102 @atexit.register
102 @atexit.register
103 def killpager():
103 def killpager():
104 if util.safehasattr(signal, "SIGINT"):
104 if util.safehasattr(signal, "SIGINT"):
105 signal.signal(signal.SIGINT, signal.SIG_IGN)
105 signal.signal(signal.SIGINT, signal.SIG_IGN)
106 pager.stdin.close()
106 pager.stdin.close()
107 ui.fout = olduifout
107 ui.fout = olduifout
108 util.stdout = oldstdout
108 util.stdout = oldstdout
109 # close new stdout while it's associated with pager; otherwise stdout
109 # close new stdout while it's associated with pager; otherwise stdout
110 # fd would be closed when newstdout is deleted
110 # fd would be closed when newstdout is deleted
111 newstdout.close()
111 newstdout.close()
112 # restore original fds: stdout is open again
112 # restore original fds: stdout is open again
113 os.dup2(stdoutfd, util.stdout.fileno())
113 os.dup2(stdoutfd, util.stdout.fileno())
114 os.dup2(stderrfd, util.stderr.fileno())
114 os.dup2(stderrfd, util.stderr.fileno())
115 pager.wait()
115 pager.wait()
116
116
117 def uisetup(ui):
117 def uisetup(ui):
118 class pagerui(ui.__class__):
118 class pagerui(ui.__class__):
119 def _runpager(self, pagercmd):
119 def _runpager(self, pagercmd):
120 _runpager(self, pagercmd)
120 _runpager(self, pagercmd)
121
121
122 ui.__class__ = pagerui
122 ui.__class__ = pagerui
123
123
124 def pagecmd(orig, ui, options, cmd, cmdfunc):
124 def pagecmd(orig, ui, options, cmd, cmdfunc):
125 p = ui.config("pager", "pager", encoding.environ.get("PAGER"))
125 p = ui.config("pager", "pager", encoding.environ.get("PAGER"))
126 usepager = False
126 usepager = False
127 always = util.parsebool(options['pager'])
127 always = util.parsebool(options['pager'])
128 auto = options['pager'] == 'auto'
128 auto = options['pager'] == 'auto'
129
129
130 if not p or '--debugger' in sys.argv or not ui.formatted():
130 if not p or '--debugger' in sys.argv or not ui.formatted():
131 pass
131 pass
132 elif always:
132 elif always:
133 usepager = True
133 usepager = True
134 elif not auto:
134 elif not auto:
135 usepager = False
135 usepager = False
136 else:
136 else:
137 attend = ui.configlist('pager', 'attend', attended)
137 attend = ui.configlist('pager', 'attend', attended)
138 ignore = ui.configlist('pager', 'ignore')
138 ignore = ui.configlist('pager', 'ignore')
139 cmds, _ = cmdutil.findcmd(cmd, commands.table)
139 cmds, _ = cmdutil.findcmd(cmd, commands.table)
140
140
141 for cmd in cmds:
141 for cmd in cmds:
142 var = 'attend-%s' % cmd
142 var = 'attend-%s' % cmd
143 if ui.config('pager', var):
143 if ui.config('pager', var):
144 usepager = ui.configbool('pager', var)
144 usepager = ui.configbool('pager', var)
145 break
145 break
146 if (cmd in attend or
146 if (cmd in attend or
147 (cmd not in ignore and not attend)):
147 (cmd not in ignore and not attend)):
148 usepager = True
148 usepager = True
149 break
149 break
150
150
151 setattr(ui, 'pageractive', usepager)
151 setattr(ui, 'pageractive', usepager)
152
152
153 if usepager:
153 if usepager:
154 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
154 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
155 ui.setconfig('ui', 'interactive', False, 'pager')
155 ui.setconfig('ui', 'interactive', False, 'pager')
156 if util.safehasattr(signal, "SIGPIPE"):
157 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
158 ui._runpager(p)
156 ui._runpager(p)
159 return orig(ui, options, cmd, cmdfunc)
157 return orig(ui, options, cmd, cmdfunc)
160
158
161 # Wrap dispatch._runcommand after color is loaded so color can see
159 # Wrap dispatch._runcommand after color is loaded so color can see
162 # ui.pageractive. Otherwise, if we loaded first, color's wrapped
160 # ui.pageractive. Otherwise, if we loaded first, color's wrapped
163 # dispatch._runcommand would run without having access to ui.pageractive.
161 # dispatch._runcommand would run without having access to ui.pageractive.
164 def afterloaded(loaded):
162 def afterloaded(loaded):
165 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
163 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
166 extensions.afterloaded('color', afterloaded)
164 extensions.afterloaded('color', afterloaded)
167
165
168 def extsetup(ui):
166 def extsetup(ui):
169 commands.globalopts.append(
167 commands.globalopts.append(
170 ('', 'pager', 'auto',
168 ('', 'pager', 'auto',
171 _("when to paginate (boolean, always, auto, or never)"),
169 _("when to paginate (boolean, always, auto, or never)"),
172 _('TYPE')))
170 _('TYPE')))
173
171
174 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
172 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
General Comments 0
You need to be logged in to leave comments. Login now