-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcolor.py
37 lines (33 loc) · 904 Bytes
/
pcolor.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
import sys
def printc(*s, color="grey", hl=None, bg=None, file=sys.stderr):
"""
Prints some text with some color, using Terminal escape sequences
>>> printc("Hello world", color="blue")
\033[1;34mHello world\033[1;m
>>> printc("Hello world", color="blue", hl=True)
\033[1;44mHello world\033[1;m
"""
colors = {
'grey': 30,
'black': 31,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'purple': 35,
'cyan': 36,
}
if color == "grey":
hl = True
code = colors.get(color)
text = ' '.join(str(x) for x in s)
if code:
hl = 1 if hl else 0
if bg:
code += 10
file.write("\r\033[{hl};{color}m{text}\033[1;m\n".format(
hl=hl, text=text, color=code))
else:
file.write('\r' + text + '\n')
file.flush()