OLD | NEW |
| 1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. |
1 ''' | 2 ''' |
2 This module generates ANSI character codes to printing colors to terminals. | 3 This module generates ANSI character codes to printing colors to terminals. |
3 See: http://en.wikipedia.org/wiki/ANSI_escape_code | 4 See: http://en.wikipedia.org/wiki/ANSI_escape_code |
4 ''' | 5 ''' |
5 | 6 |
6 CSI = '\033[' | 7 CSI = '\033[' |
7 | 8 |
8 def code_to_chars(code): | 9 def code_to_chars(code): |
9 return CSI + str(code) + 'm' | 10 return CSI + str(code) + 'm' |
10 | 11 |
11 class AnsiCodes(object): | 12 class AnsiCodes(object): |
12 def __init__(self): | 13 def __init__(self, codes): |
13 for name in dir(self): | 14 for name in dir(codes): |
14 if not name.startswith('_') and name.upper() == name: | 15 if not name.startswith('_'): |
15 value = getattr(self, name) | 16 value = getattr(codes, name) |
16 setattr(self, name, code_to_chars(value)) | 17 setattr(self, name, code_to_chars(value)) |
17 | 18 |
18 | 19 class AnsiFore: |
19 class AnsiFore(AnsiCodes): | |
20 BLACK = 30 | 20 BLACK = 30 |
21 RED = 31 | 21 RED = 31 |
22 GREEN = 32 | 22 GREEN = 32 |
23 YELLOW = 33 | 23 YELLOW = 33 |
24 BLUE = 34 | 24 BLUE = 34 |
25 MAGENTA = 35 | 25 MAGENTA = 35 |
26 CYAN = 36 | 26 CYAN = 36 |
27 WHITE = 37 | 27 WHITE = 37 |
28 RESET = 39 | 28 RESET = 39 |
29 | 29 |
30 class AnsiBack(AnsiCodes): | 30 class AnsiBack: |
31 BLACK = 40 | 31 BLACK = 40 |
32 RED = 41 | 32 RED = 41 |
33 GREEN = 42 | 33 GREEN = 42 |
34 YELLOW = 43 | 34 YELLOW = 43 |
35 BLUE = 44 | 35 BLUE = 44 |
36 MAGENTA = 45 | 36 MAGENTA = 45 |
37 CYAN = 46 | 37 CYAN = 46 |
38 WHITE = 47 | 38 WHITE = 47 |
39 RESET = 49 | 39 RESET = 49 |
40 | 40 |
41 class AnsiStyle(AnsiCodes): | 41 class AnsiStyle: |
42 BRIGHT = 1 | 42 BRIGHT = 1 |
43 DIM = 2 | 43 DIM = 2 |
44 NORMAL = 22 | 44 NORMAL = 22 |
45 RESET_ALL = 0 | 45 RESET_ALL = 0 |
46 | 46 |
| 47 Fore = AnsiCodes( AnsiFore ) |
| 48 Back = AnsiCodes( AnsiBack ) |
| 49 Style = AnsiCodes( AnsiStyle ) |
47 | 50 |
48 # Constructing the object converts the code into the equivalent ANSI escape | |
49 # string. | |
50 Fore = AnsiFore() | |
51 Back = AnsiBack() | |
52 Style = AnsiStyle() | |
OLD | NEW |