##// END OF EJS Templates
pager: ensure wrapped dispatch._runcommand runs before color's...
Gregory Szorc -
r24067:0baf41e0 default
parent child Browse files
Show More
@@ -1,170 +1,175 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 The --pager=... option can also be used to control when the pager is
52 The --pager=... option can also be used to control when the pager is
53 used. Use a boolean value like yes, no, on, off, or use auto for
53 used. Use a boolean value like yes, no, on, off, or use auto for
54 normal behavior.
54 normal behavior.
55
55
56 '''
56 '''
57
57
58 import atexit, sys, os, signal, subprocess, errno, shlex
58 import atexit, sys, os, signal, subprocess, errno, shlex
59 from mercurial import commands, dispatch, util, extensions, cmdutil
59 from mercurial import commands, dispatch, util, extensions, cmdutil
60 from mercurial.i18n import _
60 from mercurial.i18n import _
61
61
62 testedwith = 'internal'
62 testedwith = 'internal'
63
63
64 def _pagerfork(ui, p):
64 def _pagerfork(ui, p):
65 if not util.safehasattr(os, 'fork'):
65 if not util.safehasattr(os, 'fork'):
66 sys.stdout = util.popen(p, 'wb')
66 sys.stdout = util.popen(p, 'wb')
67 if ui._isatty(sys.stderr):
67 if ui._isatty(sys.stderr):
68 sys.stderr = sys.stdout
68 sys.stderr = sys.stdout
69 return
69 return
70 fdin, fdout = os.pipe()
70 fdin, fdout = os.pipe()
71 pid = os.fork()
71 pid = os.fork()
72 if pid == 0:
72 if pid == 0:
73 os.close(fdin)
73 os.close(fdin)
74 os.dup2(fdout, sys.stdout.fileno())
74 os.dup2(fdout, sys.stdout.fileno())
75 if ui._isatty(sys.stderr):
75 if ui._isatty(sys.stderr):
76 os.dup2(fdout, sys.stderr.fileno())
76 os.dup2(fdout, sys.stderr.fileno())
77 os.close(fdout)
77 os.close(fdout)
78 return
78 return
79 os.dup2(fdin, sys.stdin.fileno())
79 os.dup2(fdin, sys.stdin.fileno())
80 os.close(fdin)
80 os.close(fdin)
81 os.close(fdout)
81 os.close(fdout)
82 try:
82 try:
83 os.execvp('/bin/sh', ['/bin/sh', '-c', p])
83 os.execvp('/bin/sh', ['/bin/sh', '-c', p])
84 except OSError, e:
84 except OSError, e:
85 if e.errno == errno.ENOENT:
85 if e.errno == errno.ENOENT:
86 # no /bin/sh, try executing the pager directly
86 # no /bin/sh, try executing the pager directly
87 args = shlex.split(p)
87 args = shlex.split(p)
88 os.execvp(args[0], args)
88 os.execvp(args[0], args)
89 else:
89 else:
90 raise
90 raise
91
91
92 def _pagersubprocess(ui, p):
92 def _pagersubprocess(ui, p):
93 pager = subprocess.Popen(p, shell=True, bufsize=-1,
93 pager = subprocess.Popen(p, shell=True, bufsize=-1,
94 close_fds=util.closefds, stdin=subprocess.PIPE,
94 close_fds=util.closefds, stdin=subprocess.PIPE,
95 stdout=sys.stdout, stderr=sys.stderr)
95 stdout=sys.stdout, stderr=sys.stderr)
96
96
97 stdout = os.dup(sys.stdout.fileno())
97 stdout = os.dup(sys.stdout.fileno())
98 stderr = os.dup(sys.stderr.fileno())
98 stderr = os.dup(sys.stderr.fileno())
99 os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
99 os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
100 if ui._isatty(sys.stderr):
100 if ui._isatty(sys.stderr):
101 os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
101 os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
102
102
103 @atexit.register
103 @atexit.register
104 def killpager():
104 def killpager():
105 if util.safehasattr(signal, "SIGINT"):
105 if util.safehasattr(signal, "SIGINT"):
106 signal.signal(signal.SIGINT, signal.SIG_IGN)
106 signal.signal(signal.SIGINT, signal.SIG_IGN)
107 pager.stdin.close()
107 pager.stdin.close()
108 os.dup2(stdout, sys.stdout.fileno())
108 os.dup2(stdout, sys.stdout.fileno())
109 os.dup2(stderr, sys.stderr.fileno())
109 os.dup2(stderr, sys.stderr.fileno())
110 pager.wait()
110 pager.wait()
111
111
112 def _runpager(ui, p):
112 def _runpager(ui, p):
113 # The subprocess module shipped with Python <= 2.4 is buggy (issue3533).
113 # The subprocess module shipped with Python <= 2.4 is buggy (issue3533).
114 # The compat version is buggy on Windows (issue3225), but has been shipping
114 # The compat version is buggy on Windows (issue3225), but has been shipping
115 # with hg for a long time. Preserve existing functionality.
115 # with hg for a long time. Preserve existing functionality.
116 if sys.version_info >= (2, 5):
116 if sys.version_info >= (2, 5):
117 _pagersubprocess(ui, p)
117 _pagersubprocess(ui, p)
118 else:
118 else:
119 _pagerfork(ui, p)
119 _pagerfork(ui, p)
120
120
121 def uisetup(ui):
121 def uisetup(ui):
122 if '--debugger' in sys.argv or not ui.formatted():
122 if '--debugger' in sys.argv or not ui.formatted():
123 return
123 return
124
124
125 def pagecmd(orig, ui, options, cmd, cmdfunc):
125 def pagecmd(orig, ui, options, cmd, cmdfunc):
126 p = ui.config("pager", "pager", os.environ.get("PAGER"))
126 p = ui.config("pager", "pager", os.environ.get("PAGER"))
127 usepager = False
127 usepager = False
128 always = util.parsebool(options['pager'])
128 always = util.parsebool(options['pager'])
129 auto = options['pager'] == 'auto'
129 auto = options['pager'] == 'auto'
130
130
131 if not p:
131 if not p:
132 pass
132 pass
133 elif always:
133 elif always:
134 usepager = True
134 usepager = True
135 elif not auto:
135 elif not auto:
136 usepager = False
136 usepager = False
137 else:
137 else:
138 attend = ui.configlist('pager', 'attend', attended)
138 attend = ui.configlist('pager', 'attend', attended)
139 ignore = ui.configlist('pager', 'ignore')
139 ignore = ui.configlist('pager', 'ignore')
140 cmds, _ = cmdutil.findcmd(cmd, commands.table)
140 cmds, _ = cmdutil.findcmd(cmd, commands.table)
141
141
142 for cmd in cmds:
142 for cmd in cmds:
143 var = 'attend-%s' % cmd
143 var = 'attend-%s' % cmd
144 if ui.config('pager', var):
144 if ui.config('pager', var):
145 usepager = ui.configbool('pager', var)
145 usepager = ui.configbool('pager', var)
146 break
146 break
147 if (cmd in attend or
147 if (cmd in attend or
148 (cmd not in ignore and not attend)):
148 (cmd not in ignore and not attend)):
149 usepager = True
149 usepager = True
150 break
150 break
151
151
152 setattr(ui, 'pageractive', usepager)
152 setattr(ui, 'pageractive', usepager)
153
153
154 if usepager:
154 if usepager:
155 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
155 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
156 ui.setconfig('ui', 'interactive', False, 'pager')
156 ui.setconfig('ui', 'interactive', False, 'pager')
157 if util.safehasattr(signal, "SIGPIPE"):
157 if util.safehasattr(signal, "SIGPIPE"):
158 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
158 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
159 _runpager(ui, p)
159 _runpager(ui, p)
160 return orig(ui, options, cmd, cmdfunc)
160 return orig(ui, options, cmd, cmdfunc)
161
161
162 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
162 # Wrap dispatch._runcommand after color is loaded so color can see
163 # ui.pageractive. Otherwise, if we loaded first, color's wrapped
164 # dispatch._runcommand would run without having access to ui.pageractive.
165 def afterloaded(loaded):
166 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
167 extensions.afterloaded('color', afterloaded)
163
168
164 def extsetup(ui):
169 def extsetup(ui):
165 commands.globalopts.append(
170 commands.globalopts.append(
166 ('', 'pager', 'auto',
171 ('', 'pager', 'auto',
167 _("when to paginate (boolean, always, auto, or never)"),
172 _("when to paginate (boolean, always, auto, or never)"),
168 _('TYPE')))
173 _('TYPE')))
169
174
170 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
175 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
General Comments 0
You need to be logged in to leave comments. Login now