forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_colors_linux.py
62 lines (52 loc) · 1.28 KB
/
lixian_colors_linux.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
__all__ = ['AnsiConsole']
from lixian_colors_console import Console
import sys
colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'purple' : [35, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39]
}
class Render:
def __init__(self, output, code):
self.output = output
self.left, self.right = code
def __enter__(self):
self.output.write(self.left)
self.output.flush()
def __exit__(self, type, value, traceback):
self.output.write(self.right)
self.output.flush()
def mix_styles(styles):
left = []
right = []
for style in styles:
if style in colors:
color = colors[style]
left.append(color[0])
right.append(color[1])
right.reverse()
return [''.join('\033[%dm' % n for n in left), ''.join('\033[%dm' % n for n in right)]
class AnsiConsole(Console):
def __init__(self, output=None, styles=[]):
Console.__init__(self, output, styles)
def write(self, s):
if self.styles:
with self.render(mix_styles(self.styles)):
self.output.write(s)
self.output.flush()
else:
self.output.write(s)
self.output.flush()
def render(self, code):
return Render(self.output, code)