4
4
import logging
5
5
import os
6
6
import re
7
+ import subprocess
8
+ import sys
7
9
8
10
from lib .core .convert import stdoutencode
9
11
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
+
10
23
class ColorizingStreamHandler (logging .StreamHandler ):
11
24
# color names to indices
12
25
color_map = {
@@ -21,22 +34,13 @@ class ColorizingStreamHandler(logging.StreamHandler):
21
34
}
22
35
23
36
# 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
+ }
40
44
csi = '\x1b ['
41
45
reset = '\x1b [0m'
42
46
disable_coloring = False
@@ -67,7 +71,7 @@ def emit(self, record):
67
71
except :
68
72
self .handleError (record )
69
73
70
- if os . name != 'nt' :
74
+ if not subprocess . mswindows :
71
75
def output_colorized (self , message ):
72
76
self .stream .write (message )
73
77
else :
@@ -85,8 +89,6 @@ def output_colorized(self, message):
85
89
}
86
90
87
91
def output_colorized (self , message ):
88
- import ctypes
89
-
90
92
parts = self .ansi_esc .split (message )
91
93
write = self .stream .write
92
94
h = None
0 commit comments