1 # -*- coding: utf-8 -*- |
|
2 |
|
3 (BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, BOLD) = range(0,9) |
|
4 |
|
5 names = ['black','red','green','yellow','blue','magenta','cyan','white'] |
|
6 |
|
7 disabled = False # set True to disable all colors |
|
8 |
|
9 def highlight(enable, fg=None, bg=None): |
|
10 ''' |
|
11 |
|
12 highlight(1) -- switch to bold |
|
13 highlight(1,1) -- red foreground |
|
14 highlight(1,3,4) -- red on blue |
|
15 highlight(0) -- reset |
|
16 |
|
17 ''' |
|
18 global disabled |
|
19 if disabled: |
|
20 return '' |
|
21 |
|
22 if enable: |
|
23 code = '1' |
|
24 if fg is not None: |
|
25 code = '' |
|
26 if fg >= 8: |
|
27 fg -= 8 |
|
28 code += '1;' |
|
29 code += str(30 + fg) |
|
30 if bg is not None: |
|
31 code += ';' |
|
32 if bg >= 8: |
|
33 bg -= 8 |
|
34 code += '1;' |
|
35 code += str(40 + bg) |
|
36 return "\033[" + code + "m" |
|
37 else: |
|
38 return "\033[0m" |
|
39 |
|
40 def sethighlight(enable, fg=None, bg=None): |
|
41 print(highlight(enable, fg, bg), end='') |
|
42 |
|
43 |
|
44 if __name__ == '__main__': |
|
45 for c in range(0,8): |
|
46 print( |
|
47 highlight(1,c), names[c].ljust(20), |
|
48 highlight(1,8+c), names[c].ljust(20), |
|
49 highlight(0), sep='') |
|
50 |
|