##// END OF EJS Templates
pager: inline _pagersubprocess() into _runpager()...
Yuya Nishihara -
r26452:499d5c98 default
parent child Browse files
Show More
@@ -1,145 +1,142 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
58 import atexit, sys, os, signal, subprocess
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 # Note for extension authors: ONLY specify testedwith = 'internal' for
62 # Note for extension authors: ONLY specify testedwith = 'internal' for
63 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
63 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
64 # be specifying the version(s) of Mercurial they are tested with, or
64 # be specifying the version(s) of Mercurial they are tested with, or
65 # leave the attribute unspecified.
65 # leave the attribute unspecified.
66 testedwith = 'internal'
66 testedwith = 'internal'
67
67
68 def _pagersubprocess(ui, p):
68 def _runpager(ui, p):
69 pager = subprocess.Popen(p, shell=True, bufsize=-1,
69 pager = subprocess.Popen(p, shell=True, bufsize=-1,
70 close_fds=util.closefds, stdin=subprocess.PIPE,
70 close_fds=util.closefds, stdin=subprocess.PIPE,
71 stdout=sys.stdout, stderr=sys.stderr)
71 stdout=sys.stdout, stderr=sys.stderr)
72
72
73 stdout = os.dup(sys.stdout.fileno())
73 stdout = os.dup(sys.stdout.fileno())
74 stderr = os.dup(sys.stderr.fileno())
74 stderr = os.dup(sys.stderr.fileno())
75 os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
75 os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
76 if ui._isatty(sys.stderr):
76 if ui._isatty(sys.stderr):
77 os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
77 os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
78
78
79 @atexit.register
79 @atexit.register
80 def killpager():
80 def killpager():
81 if util.safehasattr(signal, "SIGINT"):
81 if util.safehasattr(signal, "SIGINT"):
82 signal.signal(signal.SIGINT, signal.SIG_IGN)
82 signal.signal(signal.SIGINT, signal.SIG_IGN)
83 pager.stdin.close()
83 pager.stdin.close()
84 os.dup2(stdout, sys.stdout.fileno())
84 os.dup2(stdout, sys.stdout.fileno())
85 os.dup2(stderr, sys.stderr.fileno())
85 os.dup2(stderr, sys.stderr.fileno())
86 pager.wait()
86 pager.wait()
87
87
88 def _runpager(ui, p):
89 _pagersubprocess(ui, p)
90
91 def uisetup(ui):
88 def uisetup(ui):
92 if '--debugger' in sys.argv or not ui.formatted():
89 if '--debugger' in sys.argv or not ui.formatted():
93 return
90 return
94
91
95 def pagecmd(orig, ui, options, cmd, cmdfunc):
92 def pagecmd(orig, ui, options, cmd, cmdfunc):
96 p = ui.config("pager", "pager", os.environ.get("PAGER"))
93 p = ui.config("pager", "pager", os.environ.get("PAGER"))
97 usepager = False
94 usepager = False
98 always = util.parsebool(options['pager'])
95 always = util.parsebool(options['pager'])
99 auto = options['pager'] == 'auto'
96 auto = options['pager'] == 'auto'
100
97
101 if not p:
98 if not p:
102 pass
99 pass
103 elif always:
100 elif always:
104 usepager = True
101 usepager = True
105 elif not auto:
102 elif not auto:
106 usepager = False
103 usepager = False
107 else:
104 else:
108 attend = ui.configlist('pager', 'attend', attended)
105 attend = ui.configlist('pager', 'attend', attended)
109 ignore = ui.configlist('pager', 'ignore')
106 ignore = ui.configlist('pager', 'ignore')
110 cmds, _ = cmdutil.findcmd(cmd, commands.table)
107 cmds, _ = cmdutil.findcmd(cmd, commands.table)
111
108
112 for cmd in cmds:
109 for cmd in cmds:
113 var = 'attend-%s' % cmd
110 var = 'attend-%s' % cmd
114 if ui.config('pager', var):
111 if ui.config('pager', var):
115 usepager = ui.configbool('pager', var)
112 usepager = ui.configbool('pager', var)
116 break
113 break
117 if (cmd in attend or
114 if (cmd in attend or
118 (cmd not in ignore and not attend)):
115 (cmd not in ignore and not attend)):
119 usepager = True
116 usepager = True
120 break
117 break
121
118
122 setattr(ui, 'pageractive', usepager)
119 setattr(ui, 'pageractive', usepager)
123
120
124 if usepager:
121 if usepager:
125 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
122 ui.setconfig('ui', 'formatted', ui.formatted(), 'pager')
126 ui.setconfig('ui', 'interactive', False, 'pager')
123 ui.setconfig('ui', 'interactive', False, 'pager')
127 if util.safehasattr(signal, "SIGPIPE"):
124 if util.safehasattr(signal, "SIGPIPE"):
128 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
125 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
129 _runpager(ui, p)
126 _runpager(ui, p)
130 return orig(ui, options, cmd, cmdfunc)
127 return orig(ui, options, cmd, cmdfunc)
131
128
132 # Wrap dispatch._runcommand after color is loaded so color can see
129 # Wrap dispatch._runcommand after color is loaded so color can see
133 # ui.pageractive. Otherwise, if we loaded first, color's wrapped
130 # ui.pageractive. Otherwise, if we loaded first, color's wrapped
134 # dispatch._runcommand would run without having access to ui.pageractive.
131 # dispatch._runcommand would run without having access to ui.pageractive.
135 def afterloaded(loaded):
132 def afterloaded(loaded):
136 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
133 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
137 extensions.afterloaded('color', afterloaded)
134 extensions.afterloaded('color', afterloaded)
138
135
139 def extsetup(ui):
136 def extsetup(ui):
140 commands.globalopts.append(
137 commands.globalopts.append(
141 ('', 'pager', 'auto',
138 ('', 'pager', 'auto',
142 _("when to paginate (boolean, always, auto, or never)"),
139 _("when to paginate (boolean, always, auto, or never)"),
143 _('TYPE')))
140 _('TYPE')))
144
141
145 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
142 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
General Comments 0
You need to be logged in to leave comments. Login now