Show More
@@ -1,107 +1,103 | |||
|
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 | If you notice "BROKEN PIPE" error messages, you can disable them by | |
|
26 | setting:: | |
|
27 | ||
|
28 | [pager] | |
|
29 | quiet = True | |
|
30 | ||
|
31 | 25 | You can disable the pager for certain commands by adding them to the |
|
32 | 26 | pager.ignore list:: |
|
33 | 27 | |
|
34 | 28 | [pager] |
|
35 | 29 | ignore = version, help, update |
|
36 | 30 | |
|
37 | 31 | You can also enable the pager only for certain commands using |
|
38 | 32 | pager.attend. Below is the default list of commands to be paged:: |
|
39 | 33 | |
|
40 | 34 | [pager] |
|
41 | 35 | attend = annotate, cat, diff, export, glog, log, qdiff |
|
42 | 36 | |
|
43 | 37 | Setting pager.attend to an empty value will cause all commands to be |
|
44 | 38 | paged. |
|
45 | 39 | |
|
46 | 40 | If pager.attend is present, pager.ignore will be ignored. |
|
47 | 41 | |
|
48 | 42 | To ignore global commands like :hg:`version` or :hg:`help`, you have |
|
49 | 43 | to specify them in your user configuration file. |
|
50 | 44 | |
|
51 | 45 | The --pager=... option can also be used to control when the pager is |
|
52 | 46 | used. Use a boolean value like yes, no, on, off, or use auto for |
|
53 | 47 | normal behavior. |
|
54 | 48 | ''' |
|
55 | 49 | |
|
56 | 50 | import atexit, sys, os, signal, subprocess |
|
57 | 51 | from mercurial import commands, dispatch, util, extensions |
|
58 | 52 | from mercurial.i18n import _ |
|
59 | 53 | |
|
60 | 54 | def _runpager(p): |
|
61 | 55 | pager = subprocess.Popen(p, shell=True, bufsize=-1, |
|
62 | 56 | close_fds=util.closefds, stdin=subprocess.PIPE, |
|
63 | 57 | stdout=sys.stdout, stderr=sys.stderr) |
|
64 | 58 | |
|
65 | 59 | stdout = os.dup(sys.stdout.fileno()) |
|
66 | 60 | stderr = os.dup(sys.stderr.fileno()) |
|
67 | 61 | os.dup2(pager.stdin.fileno(), sys.stdout.fileno()) |
|
68 | 62 | if util.isatty(sys.stderr): |
|
69 | 63 | os.dup2(pager.stdin.fileno(), sys.stderr.fileno()) |
|
70 | 64 | |
|
71 | 65 | @atexit.register |
|
72 | 66 | def killpager(): |
|
73 | 67 | pager.stdin.close() |
|
74 | 68 | os.dup2(stdout, sys.stdout.fileno()) |
|
75 | 69 | os.dup2(stderr, sys.stderr.fileno()) |
|
76 | 70 | pager.wait() |
|
77 | 71 | |
|
78 | 72 | def uisetup(ui): |
|
79 | 73 | if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout): |
|
80 | 74 | return |
|
81 | 75 | |
|
82 | 76 | def pagecmd(orig, ui, options, cmd, cmdfunc): |
|
83 | 77 | p = ui.config("pager", "pager", os.environ.get("PAGER")) |
|
84 | 78 | |
|
85 | 79 | if p: |
|
86 | 80 | attend = ui.configlist('pager', 'attend', attended) |
|
87 | 81 | auto = options['pager'] == 'auto' |
|
88 | 82 | always = util.parsebool(options['pager']) |
|
89 | 83 | if (always or auto and |
|
90 | 84 | (cmd in attend or |
|
91 | 85 | (cmd not in ui.configlist('pager', 'ignore') and not attend))): |
|
92 | 86 | ui.setconfig('ui', 'formatted', ui.formatted()) |
|
93 | 87 | ui.setconfig('ui', 'interactive', False) |
|
88 | try: | |
|
89 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | |
|
90 | except ValueError: | |
|
91 | pass | |
|
94 | 92 | _runpager(p) |
|
95 | if ui.configbool('pager', 'quiet'): | |
|
96 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | |
|
97 | 93 | return orig(ui, options, cmd, cmdfunc) |
|
98 | 94 | |
|
99 | 95 | extensions.wrapfunction(dispatch, '_runcommand', pagecmd) |
|
100 | 96 | |
|
101 | 97 | def extsetup(ui): |
|
102 | 98 | commands.globalopts.append( |
|
103 | 99 | ('', 'pager', 'auto', |
|
104 | 100 | _("when to paginate (boolean, always, auto, or never)"), |
|
105 | 101 | _('TYPE'))) |
|
106 | 102 | |
|
107 | 103 | attended = ['annotate', 'cat', 'diff', 'export', 'glog', 'log', 'qdiff'] |
General Comments 0
You need to be logged in to leave comments.
Login now