|
1 import sys |
|
2 |
|
3 class Terminal(object): |
|
4 COLOR = { |
|
5 'red': '31', |
|
6 'green': '32', |
|
7 'blue': '34', |
|
8 'gray': '37' |
|
9 } |
|
10 NORMAL_INTENSITY = '1' |
|
11 BRIGHT_INTENSITY = '2' |
|
12 ESCAPE = '\x1b[' |
|
13 RESET = '0' |
|
14 SEPARATOR = ';' |
|
15 COLOR_CODE = 'm' |
|
16 CLEAR_RIGHT_CODE = 'K' |
|
17 |
|
18 @classmethod |
|
19 def set_color(cls, color): |
|
20 """ |
|
21 color: str - color definition string |
|
22 """ |
|
23 mod = Terminal.NORMAL_INTENSITY |
|
24 if color.startswith('bright'): |
|
25 mod = Terminal.BRIGHT_INTENSITY |
|
26 color = color[len('bright'):] |
|
27 color_code = Terminal.COLOR[color] |
|
28 |
|
29 sys.stdout.write(cls.ESCAPE + color_code + cls.SEPARATOR + mod + cls.COLOR_CODE) |
|
30 |
|
31 @classmethod |
|
32 def reset_color(cls): |
|
33 sys.stdout.write(cls.ESCAPE + cls.RESET + cls.COLOR_CODE) |
|
34 |
|
35 @classmethod |
|
36 def clear_right(cls): |
|
37 sys.stdout.write(cls.ESCAPE + cls.CLEAR_RIGHT_CODE) |