-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
58 lines (50 loc) · 2.03 KB
/
console.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
class Console:
REGULAR=0
BOLD=1
ITALIC=3
UNDERLINE=4
STRIKE=9
def textColor(self, color=None): return ''
def backColor(self, color=None): return ''
def textStyle(self, style=None): return ''
class ColoredConsole(Console):
def __init__(self):
self.textColors = []
self.backColors = []
self.textStyles = []
def __resetAttributes(self):
text = '\033[0m'
if len(self.textColors): text += self.__textColor(self.textColors[-1])
if len(self.backColors): text += self.__backColor(self.backColors[-1])
if len(self.textStyles) and self.textStyles[-1] != self.REGULAR:
text += self.__textStyle(self.textStyles[-1])
return text
def __textColor(self, color): return '\033[38;5;{color}m'.format(color=color)
def __backColor(self, color): return '\033[48;5;{color}m'.format(color=color)
def __textStyle(self, style): return '\033[{style}m'.format(style=style)
def textColor(self, color=None):
if color:
self.textColors.append(color)
return self.__textColor(color)
else:
del self.textColors[-1]
if len(self.textColors): return self.__textColor(self.textColors[-1])
else: return self.__resetAttributes()
def backColor(self, color=None):
if color:
self.backColors.append(color)
return self.__backColor(color)
else:
del self.backColors[-1]
if len(self.backColors): return self.__backColor(self.backColors[-1])
else: return self.__resetAttributes()
def textStyle(self, style=None):
if style:
self.textStyles.append(style)
if style != self.REGULAR: return self.__textStyle(style)
else: return __resetAttributes()
else:
del self.textStyles[-1]
if len(self.textStyles) and self.textStyles[-1] != self.REGULAR:
return self.__textStyle(self.textStyles[-1])
else: return self.__resetAttributes()