-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathterm_colors.py
163 lines (145 loc) · 5.79 KB
/
term_colors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import sys
class TerminalColors:
'''Simple terminal colors class'''
def __init__(self, enabled = True):
# TODO: discover terminal type from "file" and disable if
# it can't handle the color codes
self.enabled = enabled
def set_file(self, file):
plat = sys.platform
supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
if supported_platform:
self.enabled = hasattr(file, 'isatty') and file.isatty()
else:
self.enable = False
def reset(self):
'''Reset all terminal colors and formatting.'''
if self.enabled:
return "\x1b[0m";
return ''
def faint(self):
'''Enable faint depending on the "on" paramter.'''
if self.enabled:
return "\x1b[2m";
return ''
def bold(self, on = True):
'''Enable or disable bold depending on the "on" paramter.'''
if self.enabled:
if on:
return "\x1b[1m";
else:
return "\x1b[22m";
return ''
def italics(self, on = True):
'''Enable or disable italics depending on the "on" paramter.'''
if self.enabled:
if on:
return "\x1b[3m";
else:
return "\x1b[23m";
return ''
def underline(self, on = True):
'''Enable or disable underline depending on the "on" paramter.'''
if self.enabled:
if on:
return "\x1b[4m";
else:
return "\x1b[24m";
return ''
def inverse(self, on = True):
'''Enable or disable inverse depending on the "on" paramter.'''
if self.enabled:
if on:
return "\x1b[7m";
else:
return "\x1b[27m";
return ''
def strike(self, on = True):
'''Enable or disable strike through depending on the "on" paramter.'''
if self.enabled:
if on:
return "\x1b[9m";
else:
return "\x1b[29m";
return ''
def black(self, fg = True):
'''Set the foreground or background color to black.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[30m";
else:
return "\x1b[40m";
return ''
def red(self, fg = True):
'''Set the foreground or background color to red.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[31m";
else:
return "\x1b[41m";
return ''
def green(self, fg = True):
'''Set the foreground or background color to green.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[32m";
else:
return "\x1b[42m";
return ''
def yellow(self, fg = True):
'''Set the foreground or background color to yellow.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[33m";
else:
return "\x1b[43m";
return ''
def blue(self, fg = True):
'''Set the foreground or background color to blue.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[34m";
else:
return "\x1b[44m";
return ''
def magenta(self, fg = True):
'''Set the foreground or background color to magenta.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[35m";
else:
return "\x1b[45m";
return ''
def cyan(self, fg = True):
'''Set the foreground or background color to cyan.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[36m";
else:
return "\x1b[46m";
return ''
def white(self, fg = True):
'''Set the foreground or background color to white.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[37m";
else:
return "\x1b[47m";
return ''
def default(self, fg = True):
'''Set the foreground or background color to the default.
The foreground color will be set if "fg" tests True. The background color will be set if "fg" tests False.'''
if self.enabled:
if fg:
return "\x1b[39m";
else:
return "\x1b[49m";
return ''