44import logging
55import os
66import re
7+ import subprocess
8+ import sys
79
810from lib .core .convert import stdoutencode
911
12+ if subprocess .mswindows :
13+ import ctypes
14+ import ctypes .wintypes
15+
16+ # Reference: https://gist.github.com/vsajip/758430
17+ # https://github.com/ipython/ipython/issues/4252
18+ # https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
19+ ctypes .windll .kernel32 .SetConsoleTextAttribute .argtypes = [ctypes .wintypes .HANDLE , ctypes .wintypes .WORD ]
20+ ctypes .windll .kernel32 .SetConsoleTextAttribute .restype = ctypes .wintypes .BOOL
21+
22+
1023class ColorizingStreamHandler (logging .StreamHandler ):
1124 # color names to indices
1225 color_map = {
@@ -21,22 +34,13 @@ class ColorizingStreamHandler(logging.StreamHandler):
2134 }
2235
2336 # levels to (background, foreground, bold/intense)
24- if os .name == 'nt' :
25- level_map = {
26- logging .DEBUG : (None , 'blue' , False ),
27- logging .INFO : (None , 'green' , False ),
28- logging .WARNING : (None , 'yellow' , False ),
29- logging .ERROR : (None , 'red' , False ),
30- logging .CRITICAL : ('red' , 'white' , False )
31- }
32- else :
33- level_map = {
34- logging .DEBUG : (None , 'blue' , False ),
35- logging .INFO : (None , 'green' , False ),
36- logging .WARNING : (None , 'yellow' , False ),
37- logging .ERROR : (None , 'red' , False ),
38- logging .CRITICAL : ('red' , 'white' , False )
39- }
37+ level_map = {
38+ logging .DEBUG : (None , 'blue' , False ),
39+ logging .INFO : (None , 'green' , False ),
40+ logging .WARNING : (None , 'yellow' , False ),
41+ logging .ERROR : (None , 'red' , False ),
42+ logging .CRITICAL : ('red' , 'white' , False )
43+ }
4044 csi = '\x1b ['
4145 reset = '\x1b [0m'
4246 disable_coloring = False
@@ -67,7 +71,7 @@ def emit(self, record):
6771 except :
6872 self .handleError (record )
6973
70- if os . name != 'nt' :
74+ if not subprocess . mswindows :
7175 def output_colorized (self , message ):
7276 self .stream .write (message )
7377 else :
@@ -85,8 +89,6 @@ def output_colorized(self, message):
8589 }
8690
8791 def output_colorized (self , message ):
88- import ctypes
89-
9092 parts = self .ansi_esc .split (message )
9193 write = self .stream .write
9294 h = None
0 commit comments