##// END OF EJS Templates
pager: check if signal.SIGPIPE exists...
David Soria Parra -
r16652:2fdd1902 stable
parent child Browse files
Show More
@@ -1,103 +1,101 b''
1 1 # pager.py - display output using a pager
2 2 #
3 3 # Copyright 2008 David Soria Parra <dsp@php.net>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7 #
8 8 # To load the extension, add it to your configuration file:
9 9 #
10 10 # [extension]
11 11 # pager =
12 12 #
13 13 # Run "hg help pager" to get info on configuration.
14 14
15 15 '''browse command output with an external pager
16 16
17 17 To set the pager that should be used, set the application variable::
18 18
19 19 [pager]
20 20 pager = less -FRSX
21 21
22 22 If no pager is set, the pager extensions uses the environment variable
23 23 $PAGER. If neither pager.pager, nor $PAGER is set, no pager is used.
24 24
25 25 You can disable the pager for certain commands by adding them to the
26 26 pager.ignore list::
27 27
28 28 [pager]
29 29 ignore = version, help, update
30 30
31 31 You can also enable the pager only for certain commands using
32 32 pager.attend. Below is the default list of commands to be paged::
33 33
34 34 [pager]
35 35 attend = annotate, cat, diff, export, glog, log, qdiff
36 36
37 37 Setting pager.attend to an empty value will cause all commands to be
38 38 paged.
39 39
40 40 If pager.attend is present, pager.ignore will be ignored.
41 41
42 42 To ignore global commands like :hg:`version` or :hg:`help`, you have
43 43 to specify them in your user configuration file.
44 44
45 45 The --pager=... option can also be used to control when the pager is
46 46 used. Use a boolean value like yes, no, on, off, or use auto for
47 47 normal behavior.
48 48 '''
49 49
50 50 import atexit, sys, os, signal, subprocess
51 51 from mercurial import commands, dispatch, util, extensions
52 52 from mercurial.i18n import _
53 53
54 54 def _runpager(p):
55 55 pager = subprocess.Popen(p, shell=True, bufsize=-1,
56 56 close_fds=util.closefds, stdin=subprocess.PIPE,
57 57 stdout=sys.stdout, stderr=sys.stderr)
58 58
59 59 stdout = os.dup(sys.stdout.fileno())
60 60 stderr = os.dup(sys.stderr.fileno())
61 61 os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
62 62 if util.isatty(sys.stderr):
63 63 os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
64 64
65 65 @atexit.register
66 66 def killpager():
67 67 pager.stdin.close()
68 68 os.dup2(stdout, sys.stdout.fileno())
69 69 os.dup2(stderr, sys.stderr.fileno())
70 70 pager.wait()
71 71
72 72 def uisetup(ui):
73 73 if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout):
74 74 return
75 75
76 76 def pagecmd(orig, ui, options, cmd, cmdfunc):
77 77 p = ui.config("pager", "pager", os.environ.get("PAGER"))
78 78
79 79 if p:
80 80 attend = ui.configlist('pager', 'attend', attended)
81 81 auto = options['pager'] == 'auto'
82 82 always = util.parsebool(options['pager'])
83 83 if (always or auto and
84 84 (cmd in attend or
85 85 (cmd not in ui.configlist('pager', 'ignore') and not attend))):
86 86 ui.setconfig('ui', 'formatted', ui.formatted())
87 87 ui.setconfig('ui', 'interactive', False)
88 try:
88 if util.safehasattr(signal, "SIGPIPE"):
89 89 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
90 except ValueError:
91 pass
92 90 _runpager(p)
93 91 return orig(ui, options, cmd, cmdfunc)
94 92
95 93 extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
96 94
97 95 def extsetup(ui):
98 96 commands.globalopts.append(
99 97 ('', 'pager', 'auto',
100 98 _("when to paginate (boolean, always, auto, or never)"),
101 99 _('TYPE')))
102 100
103 101 attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff']
General Comments 0
You need to be logged in to leave comments. Login now