-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
executable file
·241 lines (202 loc) · 6.42 KB
/
__init__.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/local/bin/python3
colors = {color: idx for idx, color in enumerate([
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'normal',
'primary'
])}
formats = {frmt: idx for idx, frmt in enumerate([
'normal',
'bold',
'faint',
'italic',
'underline',
'slowblink',
'rapidblink',
'negative',
'conceal',
'crossedout',
'primary']) }
ansi = 'ansi'
rgb = 'rgb'
class ColorPart():
pre = '\x1b['
post = '\x1b[0m'
def __init__(self, text = '', frmt = 'primary', fg = 'primary', bg = 'primary'):
self.text = text
if isinstance(frmt, list):
for f in frmt[:-1]:
self.text = ColorPart(self.text, f)
self.frmt = frmt[-1]
else:
self.frmt = frmt
if isinstance(fg, tuple):
self.text = self.text
self.frmt = rgb
self.fg = fg
self.bg = bg
def __str__(self):
if self.frmt == rgb:
fg = ';'.join((str(i) for i in self.fg))
return self.pre + '38;2;' + fg + 'm' + str(self.text) + self.post
else:
fg = str(self.fg)
bg = str(self.bg)
frmt = str(self.frmt)
return self.pre + frmt + ';' + fg + ';' + bg + 'm' + str(self.text) + self.post
@property
def fg(self):
return self._fg
@fg.setter
def fg(self, x):
if isinstance(x, int):
if x < 39 and x > 29:
self._fg = x
else:
self._fg = 30 + x
elif isinstance(x, tuple):
assert(all((i < 256 and i >= 0 for i in x)) and len(x) == 3)
self._fg = x
else:
assert(x in colors)
self._fg = 30 + colors[x]
@property
def bg(self):
return self._bg
@bg.setter
def bg(self, x):
if isinstance(x, int):
if x < 49 and x > 39:
self._bg = x
else:
self._bg = 40 + x
else:
assert(x in colors)
self._bg = 40 + colors[x]
@property
def frmt(self):
return self._frmt
@frmt.setter
def frmt(self, x):
if isinstance(x, int):
self._frmt = x
elif x == rgb:
self._frmt = rgb
else:
assert(x in formats)
self._frmt = formats[x]
class Color():
def __init__(self, text = '', frmt = 'primary', fg = 'primary', bg = 'primary'):
if isinstance(text, int):
text = str(text)
self.parts = [ColorPart(text, frmt, fg, bg)]
def separated(self):
separated = []
for part in self.parts:
for ch in part.text:
separated.append(ColorPart(ch, part.frmt, part.fg, part.bg))
return separated
def cohesed(self):
cohesed = []
for part in self.parts:
if len(cohesed) == 0:
cohesed.append(part)
continue
last = cohesed[-1]
if cohesed and part.fg == last.fg and part.bg == last.bg and part.frmt == last.frmt:
last.text += part.text
else:
cohesed.append(part)
return list(filter(lambda x: x.text, cohesed))
def __str__(self):
return ''.join([str(i) for i in self.parts])
def __repr__(self):
return ''.join([i.text for i in self.parts])
def __add__(self, x):
if isinstance(x, Color):
self.parts += x.parts
elif isinstance(x, str):
self.parts.append(ColorPart(x))
else:
raise TypeError('Requires either Color or str')
return self
def __len__(self):
return sum([len(part.text) for part in self.parts])
def __getitem__(self, key):
filtered = self.separated()[key]
ret = Color()
ret.parts = [filtered] if not isinstance(filtered, list) else filtered
ret.parts = ret.cohesed()
return ret
def __hash__(self):
return hash(str(self))
def rjust(self, width, fillchar = ' '):
if len(self) >= width:
return self
return Color((width - len(self)) * fillchar) + self
def ljust(self, width, fillchar = ' '):
if len(self) >= width:
return self
return self + Color((width - len(self)) * fillchar)
def center(self, width, fillchar = ' '):
if len(self) >= width:
return self
pad = width - len(self)
right = left = int(pad / 2)
if pad % 2 != 0:
right += 1
return Color(left * fillchar) + self + Color(right * fillchar)
def test():
teststrings = [
# Argument usage
'keywords',
Color('fish', frmt='italic', fg = 'red', bg = 'yellow'),
'positional',
Color('fish', 'bold', 'blue', 'normal'),
'only frmt',
Color('fish', 'faint'),
'numbers',
Color('fish', fg = 1),
'several formats (not colors)',
Color('fish', ['italic', 'bold', 'faint', 'underline'], 'red'),
'rgb',
Color('fish', fg = (0, 255, 0)),
'rgb2',
Color(Color('fish', fg = (0, 255, 0)), 'bold'),
'rgb3',
Color(Color('fish', 'bold'), fg = (255, 0, 0)),
'rgb4 this has a bug',
Color(Color('fish'), fg = (255, 0, 0)),
# Features
'len',
str(len(Color('this string has a length of 30', 'bold'))),
'addition',
Color('one', fg='red') + Color('two', fg='blue'),
'indexing',
(Color('one', fg='red') + Color('two', fg='blue'))[2],
'splice',
(Color('one', fg='red') + Color('two', fg='blue'))[2:5],
'splice 2',
(Color('oioi', fg='red') + Color('apap', fg='blue'))[::2],
'nested strings',
Color(Color('s')+ Color(Color('s') + Color('s'))),
'length of nested strings works',
str(len(Color(Color('s')+ Color(Color(Color('ss', 'faint'), 'bold') + Color('s'), 'italic')))),
'rjust',
Color('fish', frmt='italic', fg = 'red', bg = 'yellow').rjust(30),
'ljust',
Color('fish', frmt='italic', fg = 'red', bg = 'yellow').ljust(30, '-'),
'center',
Color('fi', frmt='italic', fg = 'red', bg = 'yellow').center(5, '-'),
]
for test in zip(teststrings[::2], teststrings[1::2]):
print(test[0])
print(Color('\t') + test[1])
if __name__ == '__main__':
test()