From 303fff34f6ff370ce75ae9efaba60b1edc603b0d Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 19 Apr 2016 12:53:26 +0300 Subject: [PATCH] init.py: pipe output to pager --- lib/python/init.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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() -- 2.39.2