]> git.phdru.name Git - dotfiles.git/commitdiff
init.py: pipe output to pager
authorOleg Broytman <phd@phdru.name>
Tue, 19 Apr 2016 09:53:26 +0000 (12:53 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 19 Apr 2016 09:53:26 +0000 (12:53 +0300)
lib/python/init.py

index 642eb6c6ab545f3f52443768c2fcebb61c056b08..2450f75a6aeb98a35cee223ac0fbc41d61119933 100644 (file)
@@ -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()