More changelog updates
[stgit.git] / stgit / pager.py
blob60fbb9043ecbf314054282688c32553ad62fca13
1 import os
2 import shutil
3 import subprocess
4 import sys
6 from stgit.out import out
9 def pager(msg):
10 pager = _choose_pager()
11 if not sys.stdin.isatty() or not sys.stdout.isatty() or not pager:
12 return out.stdout_bytes(msg)
13 else:
14 return _run_pager(pager, msg)
17 def _run_pager(pager, msg):
18 proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE)
19 try:
20 proc.stdin.write(msg)
21 except (KeyboardInterrupt, OSError):
22 pass
23 finally:
24 proc.stdin.close()
25 while True:
26 try:
27 proc.wait()
28 break
29 except KeyboardInterrupt:
30 pass
33 def _choose_pager():
34 for k in ['GIT_PAGER', 'PAGER', 'MANPAGER']:
35 pager = os.environ.get(k)
36 if pager:
37 return pager
38 for pager in ['less', 'more']:
39 if shutil.which(pager):
40 return pager
41 return None