From: Oleg Broytman Date: Tue, 19 Apr 2016 09:53:26 +0000 (+0300) Subject: init.py: pipe output to pager X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=303fff34f6ff370ce75ae9efaba60b1edc603b0d;p=dotfiles.git init.py: pipe output to pager --- diff --git a/lib/python/init.py b/lib/python/init.py index 642eb6c..2450f75 100644 --- a/lib/python/init.py +++ b/lib/python/init.py @@ -120,11 +120,26 @@ def init(): # set displayhook from pprint import pprint + try: + from subprocess import Popen, PIPE + except ImportError: + use_subprocess = False + else: + use_subprocess = True + pager = os.environ.get("PAGER") or 'more' def displayhook(value): if value is not None: __builtin__._ = value - pprint(value) + if use_subprocess: + _pipe = Popen(pager, shell=True, stdin=PIPE) + pipe = _pipe.stdin + else: + pipe = os.popen(pager, 'w') + pprint(value, stream=pipe) + pipe.close() + if use_subprocess: + _pipe.wait() sys.displayhook = displayhook @@ -233,7 +248,6 @@ def init(): return "Usage: pager('filename')" def __call__(self, filename): - pager = os.environ.get("PAGER") or 'more' os.system("%s '%s'" % (pager, filename.replace("'", '"\'"'))) __builtin__.pager = _Pager()