diff options
Diffstat (limited to 'pager.py')
-rwxr-xr-x | pager.py | 38 |
1 files changed, 36 insertions, 2 deletions
@@ -16,19 +16,53 @@ | |||
16 | from __future__ import print_function | 16 | from __future__ import print_function |
17 | import os | 17 | import os |
18 | import select | 18 | import select |
19 | import subprocess | ||
19 | import sys | 20 | import sys |
20 | 21 | ||
22 | import platform_utils | ||
23 | |||
21 | active = False | 24 | active = False |
25 | pager_process = None | ||
26 | old_stdout = None | ||
27 | old_stderr = None | ||
22 | 28 | ||
23 | def RunPager(globalConfig): | 29 | def RunPager(globalConfig): |
24 | global active | ||
25 | |||
26 | if not os.isatty(0) or not os.isatty(1): | 30 | if not os.isatty(0) or not os.isatty(1): |
27 | return | 31 | return |
28 | pager = _SelectPager(globalConfig) | 32 | pager = _SelectPager(globalConfig) |
29 | if pager == '' or pager == 'cat': | 33 | if pager == '' or pager == 'cat': |
30 | return | 34 | return |
31 | 35 | ||
36 | if platform_utils.isWindows(): | ||
37 | _PipePager(pager); | ||
38 | else: | ||
39 | _ForkPager(pager) | ||
40 | |||
41 | def TerminatePager(): | ||
42 | global pager_process, old_stdout, old_stderr | ||
43 | if pager_process: | ||
44 | sys.stdout.flush() | ||
45 | sys.stderr.flush() | ||
46 | pager_process.stdin.close() | ||
47 | pager_process.wait(); | ||
48 | pager_process = None | ||
49 | # Restore initial stdout/err in case there is more output in this process | ||
50 | # after shutting down the pager process | ||
51 | sys.stdout = old_stdout | ||
52 | sys.stderr = old_stderr | ||
53 | |||
54 | def _PipePager(pager): | ||
55 | global pager_process, old_stdout, old_stderr | ||
56 | assert pager_process is None, "Only one active pager process at a time" | ||
57 | # Create pager process, piping stdout/err into its stdin | ||
58 | pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr) | ||
59 | old_stdout = sys.stdout | ||
60 | old_stderr = sys.stderr | ||
61 | sys.stdout = pager_process.stdin | ||
62 | sys.stderr = pager_process.stdin | ||
63 | |||
64 | def _ForkPager(pager): | ||
65 | global active | ||
32 | # This process turns into the pager; a child it forks will | 66 | # This process turns into the pager; a child it forks will |
33 | # do the real processing and output back to the pager. This | 67 | # do the real processing and output back to the pager. This |
34 | # is necessary to keep the pager in control of the tty. | 68 | # is necessary to keep the pager in control of the tty. |