summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xmain.py3
-rwxr-xr-xpager.py38
2 files changed, 38 insertions, 3 deletions
diff --git a/main.py b/main.py
index 386b4f13..a6538c2a 100755
--- a/main.py
+++ b/main.py
@@ -55,7 +55,7 @@ from error import NoSuchProjectError
55from error import RepoChangedException 55from error import RepoChangedException
56import gitc_utils 56import gitc_utils
57from manifest_xml import GitcManifest, XmlManifest 57from manifest_xml import GitcManifest, XmlManifest
58from pager import RunPager 58from pager import RunPager, TerminatePager
59from wrapper import WrapperPath, Wrapper 59from wrapper import WrapperPath, Wrapper
60 60
61from subcmds import all_commands 61from subcmds import all_commands
@@ -542,6 +542,7 @@ def _Main(argv):
542 print('fatal: %s' % e, file=sys.stderr) 542 print('fatal: %s' % e, file=sys.stderr)
543 result = 128 543 result = 128
544 544
545 TerminatePager()
545 sys.exit(result) 546 sys.exit(result)
546 547
547if __name__ == '__main__': 548if __name__ == '__main__':
diff --git a/pager.py b/pager.py
index c6211419..0521c0c7 100755
--- a/pager.py
+++ b/pager.py
@@ -16,19 +16,53 @@
16from __future__ import print_function 16from __future__ import print_function
17import os 17import os
18import select 18import select
19import subprocess
19import sys 20import sys
20 21
22import platform_utils
23
21active = False 24active = False
25pager_process = None
26old_stdout = None
27old_stderr = None
22 28
23def RunPager(globalConfig): 29def 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
41def 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
54def _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
64def _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.