15
15
16
16
from __future__ import print_function , division , absolute_import
17
17
18
+ import re
19
+ import sys
18
20
from os .path import basename
19
21
20
22
from . import Notifier
21
- from ..settings import PRINT_COMPILER_OUTPUT_AS_LINK
23
+ from ..settings import (PRINT_COMPILER_OUTPUT_AS_LINK ,
24
+ CLI_COLOR_MAP , COLOR )
22
25
23
26
class TerminalNotifier (Notifier ):
24
27
"""
25
- Writes notifications to a terminal based on a silent and verbose flag .
28
+ Writes notifications to a terminal based on silent, verbose and color flags .
26
29
"""
27
30
28
- def __init__ (self , verbose = False , silent = False ):
31
+ def __init__ (self , verbose = False , silent = False , color = False ):
29
32
self .verbose = verbose
30
33
self .silent = silent
31
34
self .output = ""
35
+ self .color = color or COLOR
36
+ if self .color :
37
+ from colorama import init , Fore , Back , Style
38
+ init ()
39
+ self .COLORS = {
40
+ 'none' : "" ,
41
+ 'default' : Style .RESET_ALL ,
42
+
43
+ 'black' : Fore .BLACK ,
44
+ 'red' : Fore .RED ,
45
+ 'green' : Fore .GREEN ,
46
+ 'yellow' : Fore .YELLOW ,
47
+ 'blue' : Fore .BLUE ,
48
+ 'magenta' : Fore .MAGENTA ,
49
+ 'cyan' : Fore .CYAN ,
50
+ 'white' : Fore .WHITE ,
51
+
52
+ 'on_black' : Back .BLACK ,
53
+ 'on_red' : Back .RED ,
54
+ 'on_green' : Back .GREEN ,
55
+ 'on_yellow' : Back .YELLOW ,
56
+ 'on_blue' : Back .BLUE ,
57
+ 'on_magenta' : Back .MAGENTA ,
58
+ 'on_cyan' : Back .CYAN ,
59
+ 'on_white' : Back .WHITE ,
60
+ }
32
61
33
62
def get_output (self ):
34
63
return self .output
@@ -40,7 +69,10 @@ def notify(self, event):
40
69
msg = self .print_notify (event )
41
70
if msg :
42
71
if not self .silent :
43
- print (msg )
72
+ if self .color :
73
+ self .print_in_color (event , msg )
74
+ else :
75
+ print (msg )
44
76
self .output += msg + "\n "
45
77
46
78
def print_notify (self , event ):
@@ -87,3 +119,33 @@ def print_notify_verbose(self, event):
87
119
88
120
elif event ['type' ] == 'progress' :
89
121
return self .print_notify (event ) # standard handle
122
+
123
+ COLOR_MATCHER = re .compile (r"(\w+)(\W+on\W+\w+)?" )
124
+ def colorstring_to_escapecode (self , color_string ):
125
+ """ Convert a color string from a string into an ascii escape code that
126
+ will print that color on the terminal.
127
+
128
+ Positional arguments:
129
+ color_string - the string to parse
130
+ """
131
+ match = re .match (self .COLOR_MATCHER , color_string )
132
+ if match :
133
+ return self .COLORS [match .group (1 )] + \
134
+ (self .COLORS [match .group (2 ).strip ().replace (" " , "_" )]
135
+ if match .group (2 ) else "" )
136
+ else :
137
+ return self .COLORS ['default' ]
138
+
139
+ def print_in_color (self , event , msg ):
140
+ """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap
141
+ notifications in a color if the severity matches a color in the
142
+ CLI_COLOR_MAP.
143
+ """
144
+ """The notification function inself"""
145
+ if sys .stdout .isatty () and event .get ('severity' , None ) in CLI_COLOR_MAP :
146
+ sys .stdout .write (self .colorstring_to_escapecode (
147
+ CLI_COLOR_MAP [event ['severity' ]]))
148
+ print (msg )
149
+ sys .stdout .write (self .colorstring_to_escapecode ('default' ))
150
+ else :
151
+ print (msg )
0 commit comments