-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansi.py
79 lines (67 loc) · 3.11 KB
/
ansi.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
from collections import namedtuple
colors = namedtuple("colors", "black red green yellow blue magenta cyan white default")(0, 1, 2, 3, 4, 5, 6, 7, 9)
brightcolors = namedtuple("brightcolors", "black red green yellow blue magenta cyan white")(60, 61, 62, 63, 64, 65, 66, 67)
color = namedtuple("color", "r g b")
del namedtuple
def ansi(*, bold=None, underlined=None, inverse=None, foreground=None, background=None):
params = []
if bold is not None:
assert isinstance(bold, bool)
params.append("1" if bold else "22")
if underlined is not None:
assert isinstance(underlined, bool)
params.append("4" if underlined else "24")
if inverse is not None:
assert isinstance(inverse, bool)
params.append("7" if inverse else "27")
if foreground is not None:
if isinstance(foreground, color):
assert isinstance(foreground.r, int) and isinstance(foreground.g, int) and isinstance(foreground.b, int) and 0 <= foreground.r < 256 and 0 <= foreground.g < 256 and 0 <= foreground.b < 256
params.extend(str(i) for i in (38, 2) + foreground)
else:
assert foreground in colors or foreground in brightcolors
params.append(str(foreground + 30))
if background is not None:
if isinstance(background, color):
assert isinstance(background.r, int) and isinstance(background.g, int) and isinstance(background.b, int) and 0 <= background.r < 256 and 0 <= background.g < 256 and 0 <= background.b < 256
params.extend(str(i) for i in (48, 2) + background)
else:
assert background in colors or background in brightcolors
params.append(str(background + 40))
if len(params) > 0:
return "\x1b[" + ";".join(params) + "m"
return ""
def reset():
ansi(bold=False, underlined=False, inverse=False, foreground=colors.default, background=colors.default)
def bold(string):
return ansi(bold=True) + string + ansi(bold=False)
def underlined(string):
return ansi(underlined=True) + string + ansi(underlined=False)
def inverse(string):
return ansi(inverse=True) + string + ansi(inverse=False)
def foreground(color, string):
return ansi(foreground=color) + string + ansi(foreground=colors.default)
def background(color, string):
return ansi(background=color) + string + ansi(background=colors.default)
if __name__ == "__main__":
print("styles:")
print(bold("bold"))
print(underlined("underlined"))
print(inverse("inverse"))
print("\ncolors:")
for name in colors._fields:
print(end=background(getattr(colors, name), " "))
print()
for name in brightcolors._fields:
print(end=background(getattr(brightcolors, name), " "))
print("\n\ncolor cube:")
for g in range(0, 256, 32): # type: int
for r in range(0, 256, 32): # type: int
for b in range(0, 256, 32): # type: int
print(end=background(color(r, g, b), " "))
print(end=" ")
print()
print("\ngrayscale ramp:")
for v in range(0, 256, 2): # type: int
print(end=background(color(v, v, v), " "))
print()