-
Notifications
You must be signed in to change notification settings - Fork 1
/
cprint.py
87 lines (84 loc) · 2.61 KB
/
cprint.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
#!/urs/bin/env python3
"""
cprint. An easy way to colorize your terminal!
"""
generated = ['\033[{}m'.format(str(i)) for i in range(0,99)]
colors = {
'end': generated[0],
'endall': generated[0],
'error': generated[1],
'bold': generated[1],
'italic': generated[3],
'underline': generated[4],
'strike': generated[9],
'dblack': generated[30],
'dred': generated[31],
'dgreen': generated[32],
'dyellow': generated[33],
'dblue': generated[34],
'dpurple': generated[35],
'dteal': generated[36],
'dwhite': generated[37],
'_black': generated[40],
'_red': generated[41],
'_green': generated[42],
'_yellow': generated[43],
'_blue': generated[44],
'_purple': generated[45],
'_teal': generated[46],
'_white': generated[47],
'black': generated[90],
'red': generated[91],
'green': generated[92],
'yellow': generated[93],
'blue': generated[94],
'purple': generated[95],
'teal': generated[96],
'white': generated[97]
}
def cprint(data, prefix='', suffix='[.endall]\n'):
"""cprint
Converts simple color names to terminal color values.
params:
- data - The data to print
- prefix - something to add to the start of data.
- suffix - something to add to the end. Defaults to [end]\n
Which ends the color and creates a newline.
"""
data = prefix + data + suffix
c = []
output = ''
for i in data.split('[.'):
color = i[:i.find(']')]
text = i[i.find(']')+1:]
if color.startswith('/'):
remove = color[1:]
c.reverse()
try:
c.remove(remove)
except ValueError:
continue
c.reverse()
output += '[.end]' + ''.join(['[.' + x + ']' for x in c])
else:
if color == '':
continue
if color not in colors:
output += color
pass
else:
output += '[.' + color + ']'
c.append(color)
output += text
for color in colors:
output = output.replace('[.' + color + ']', colors[color])
print( output, end='')
return None
if __name__ == '__main__':
cprint('[.green][.bold]Thank you[./bold] for using my software!')
cprint('[.yellow]With [.italic]this[./italic] software you can easily'
' color the output of your terminal')
cprint('[._green][.white]You can even color the [.bold]background of'
' text! :D')
cprint('[.red]You [.underline]can[./underline] make [.bold]errors'
'[./bold] look nice!')