]> git.phdru.name Git - dotfiles.git/commitdiff
init.py: colorize traceback in pager
authorOleg Broytman <phd@phdru.name>
Tue, 19 Apr 2016 10:51:31 +0000 (13:51 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 19 Apr 2016 10:52:32 +0000 (13:52 +0300)
lib/python/init.py

index 69b9ac162c3411e0feb341c9312ca3f8dba28dcd..a5e63231b5cf4fa4445a60f71d9e8f924a2649e6 100644 (file)
@@ -114,16 +114,19 @@ def init():
     else:
         try:
             locale.setlocale(locale.LC_ALL, '')
-
         except (ImportError, locale.Error):
             pass  # no locale support or unsupported locale
 
     # set displayhook
 
     from pprint import pprint
+    pager = os.environ.get("PAGER") or 'more'
 
-    class BasePPrintPager:
-        def pipe(self, value):
+    class BasePager:
+        def write(self, value):
+            self.stdin.write(value)
+
+        def pprint(self, value):
             pprint(value, stream=self.stdin)
 
         def close(self):
@@ -132,35 +135,37 @@ def init():
     try:
         from subprocess import Popen, PIPE
     except ImportError:
-        class PPrintPager(BasePPrintPager):
+        class Pager(BasePager):
             def __init__(self):
                 self.pipe = Popen(pager, shell=True, stdin=PIPE)
                 self.stdin = self.pipe.stdin
 
             def close(self):
-                BasePPrintPager.close(self)
+                BasePager.close(self)
                 self.pipe.wait()
     else:
-        class PPrintPager(BasePPrintPager):
+        class Pager(BasePager):
             def __init__(self):
                 self.stdin = os.popen(pager, 'w')
-    pager = os.environ.get("PAGER") or 'more'
 
     def displayhook(value):
         if value is not None:
             __builtin__._ = value
-        pprint_pager = PPrintPager()
-        pprint_pager.pipe(value)
-        pprint_pager.close()
+        pager = Pager()
+        pager.pprint(value)
+        pager.close()
 
     sys.displayhook = displayhook
 
-    from traceback import print_exception
+    from traceback import format_exception
 
     def excepthook(etype, evalue, etraceback):
-        pprint_pager = PPrintPager()
-        print_exception(etype, evalue, etraceback, file=pprint_pager.stdin)
-        pprint_pager.close()
+        lines = format_exception(etype, evalue, etraceback)
+        pager = Pager()
+        for line in lines:
+            pager.write(
+                '\033[31m' + line.rstrip('\n') + '\033[0m\n')  # red, reset
+        pager.close()
 
     sys.excepthook = excepthook