-
Notifications
You must be signed in to change notification settings - Fork 7
/
colour.py
217 lines (172 loc) · 6.29 KB
/
colour.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
"""
Colour (colour.py)
A module for colourizing text output to the terminal.
Patrick Lazarus, August 28th, 2009
"""
import optparse
import types
import config
# Default colour (reset to this colour)
DEFAULT_CODE = "\033[0;39;49m"
# Dictionary for translating keywords to colour codes
preset_codes = {"default": DEFAULT_CODE, \
"reset": DEFAULT_CODE, \
"infohdr": "\033[1;34m", \
"info": "\033[2;36m", \
"debughdr": "\033[1;35m", \
"debug": "\033[0;35m", \
"warning": "\033[1;33m", \
"error": "\033[1;31m", \
"success": "\033[1;32m"}
attributes = {"reset": 0, \
"bold": 1, \
"dim": 2, \
"underline": 4, \
"blink": 5, \
"reverse": 7, \
"hidden": 8}
fg_colours = {"black": 30, \
"red": 31, \
"green": 32, \
"brown": 33, \
"blue": 34, \
"purple": 35, \
"cyan": 36, \
"white": 37, \
"default": 39}
bg_colours = {"black": 40, \
"red": 41, \
"green": 42, \
"brown": 43, \
"blue": 44, \
"purple": 45, \
"cyan": 46, \
"white": 47, \
"default": 49}
# Current colour
current_code = DEFAULT_CODE
def cset(preset=None, fg='default', bg='default', **attr):
"""Set current colour code.
If a preset colour code is provided other arguments will
be ignored.
"""
global current_code, DEFAULT_CODE
if preset is not None:
if preset in preset_codes:
current_code = preset_codes[preset]
else:
print "Unrecognized preset color code:", preset
else:
set_attr = []
error = False
for a in attr.keys():
if (a in attributes):
if (attr[a]):
set_attr.append(str(attributes[a]))
else:
print "Unrecognized attribute:", a
error = True
if len(set_attr) == 0:
set_attr = ['0']
if fg in fg_colours:
fg_val = fg_colours[fg]
elif type(fg) == types.IntType or fg.isdigit():
fg_val = str(fg)
else:
print "Unrecognized foreground colour:", fg
error = True
if bg in bg_colours:
bg_val = bg_colours[bg]
elif type(bg) == types.IntType or bg.isdigit():
bg_val = str(bg)
else:
print "Unrecognized background colour:", bg
error = True
if error:
# Don't update current colour code
pass
else:
current_code = '\033[%s;%s;%sm' % (";".join(set_attr), \
fg_val, bg_val)
def creset():
"""
Reset current colour code to DEFAULT_CODE.
"""
cset('default')
def cstring(s, *override, **kwoverride):
"""
Return the string s with appropriate colour codes
inserted.
Keyword is optional. It will override the current
colour code.
"""
if not config.colour:
return s
global current_code, DEFAULT_CODE
# Assume there are overrides
# Eventually we can check...
temp_code = current_code
cset(*override, **kwoverride)
coloured_s = current_code + str(s) + DEFAULT_CODE
current_code = temp_code
return coloured_s
def cprint(s, *override, **kwoverride):
"""
Print the string s with appropriate colour codes
inserted.
Keyword is optional. It will override the current
colour code.
"""
print cstring(s, *override, **kwoverride)
def show_dictionary():
raise NotImplementedError("colours.show_dictionary needs to be implemented")
def show_colours():
raise NotImplementedError("colours.show_colours needs to be implemented")
def show_status():
"""Display status of colours module.
Print global variables.
"""
# Should we set colour to default?
print "DEFAULT_CODE:", repr(DEFAULT_CODE)
print "current_code:", repr(current_code)
# Should we print a list of all keywords that
# match the current code?
def parse_attributes(option, opt_str, value, parser):
"""Parse text attributes from command line.
"""
if not hasattr(parser.values, 'attributes'):
# Create empty dictionary for text attributes
setattr(parser.values, 'attributes', {})
parser.values.attributes[value] = True
class ColourizedOutput(object):
"""A 'file' wrapper class that colourizes its output.
"""
def __init__(self, fileobject, *cargs, **ckwargs):
self.fileobject = fileobject
self.cargs = cargs
self.ckwargs = ckwargs
def __getattr__(self, name):
return getattr(self.fileobject, name)
def write(self, s):
print "Writing"
self.fileobject.write(colour.cstring(s), *cargs, **ckwargs)
def main():
# String to print is left over command line arguments
s = " ".join(args)
cprint(s, preset=options.preset, fg=options.fg, bg=options.bg, \
**options.attributes)
if __name__ == '__main__':
presets = [cstring(pre, preset=pre) for pre in sorted(preset_codes.keys()) \
if pre not in ("default", "reset")]
parser = optparse.OptionParser()
parser.add_option('-s', '--set', dest='toset', type='string', action='callback', callback=parse_attributes, help="Set text attributes. Possible attributes to set are: defaut, bold, dim, underline, blink, reverse and hidden.")
parser.add_option('-f', '--fg', dest='fg', action='store', help='Forground text colour.', default='default')
parser.add_option('-b', '--bg', dest='bg', action='store', help='Background text colour.', default='default')
parser.add_option('-p', '--preset', dest='preset', action='store', help="Use a preset colour scheme. Other options will be ignored. Presets are: '%s'" % "', '".join(presets), default=None)
options, args = parser.parse_args()
# Ensure that options.attributes exists even if not attributes are set
if not hasattr(parser.values, 'attributes'):
# Create empty dictionary for text attributes
setattr(parser.values, 'attributes', {})
main()