-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpycolor.py
More file actions
132 lines (107 loc) · 4.36 KB
/
pycolor.py
File metadata and controls
132 lines (107 loc) · 4.36 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# Author: Soleyman
# Title: ColorMe python3 colored output
import ctypes
import platform
if platform.system() == 'Windows':
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
color = {
'fg': {
'black': '30',
'red': '31',
'green': '32',
'yellow': '33',
'blue': '34',
'purple': '35',
'cyan': '36',
'white': '37',
'reset': '0'
},
'bg': {
'black': '40',
'red': '41',
'green': '42',
'yellow': '43',
'blue': '44',
'purple': '45',
'cyan': '46',
'white': '47',
'reset': '0'
},
'lab': {
'warning': '31;1m[-]',
'info': '33;1m[!]',
'success': '32;1m[+]',
'question': '34;1m[?]',
'running': '97;1m[~]'
},
'dec': {
'bold': '1m',
'underline': '4m',
'reversed': '7m',
'reset': '0m'
}
}
color_func_key = (
'black', 'red', 'green', 'yellow', 'blue', 'purple', 'cyan', 'white')
label_func_key = ('warning', 'info', 'success', 'question', 'running')
class ColorMe:
pass
def item_iterate(temp):
data = []
for i in temp:
i = i.strip().lower()
data.append(i)
return data
def make_method(name):
def _method(self, string, color_name=name, **kwargs):
if name in label_func_key:
return u"\u001b[{}\u001b[0m".format(color['lab'][color_name]) + string
elif name in color_func_key:
dec = kwargs.get('dec', None)
bg = kwargs.get('bg', None)
args = ['dec', 'bg']
if bool(kwargs):
if len(kwargs) == 2:
dec_list = kwargs[args[0]].split(',')
bg_col_name = kwargs[args[1]]
if len(dec_list) == 2:
temp = item_iterate(dec_list)
return u"\u001b[{}\u001b[{}\u001b[{}m\u001b[{}m".format(color['dec'][temp[0]], color['dec'][temp[1]], color['bg'][bg_col_name], color['fg'][color_name]) + string + "\u001b[0m"
elif len(dec_list) == 3:
temp = item_iterate(dec_list)
return u"\u001b[{}\u001b[{}\u001b[{}\u001b[{}m\u001b[{}m".format(color['dec'][temp[0]], color['dec'][temp[1]], color['dec'][temp[2]], color['bg'][bg_col_name], color['fg'][color_name]) + string + "\u001b[0m"
else:
i = dec_list[0].lower()
return u"\u001b[{}\u001b[{}m\u001b[{}m".format(color['dec'][i], color['bg'][bg_col_name], color['fg'][color_name]) + string + "\u001b[0m"
elif len(kwargs) == 1:
try:
key1 = kwargs['bg'].lower()
return u"\u001b[{}m\u001b[{}m".format(color['bg'][key1], color['fg'][color_name]) + string + "\u001b[0m"
except KeyError:
key1 = None
try:
key2 = kwargs['dec']
key2 = key2.split(',')
if len(key2) == 2:
temp = item_iterate(key2)
return u"\u001b[{}\u001b[{}\u001b[{}m".format(color['dec'][temp[0]], color['dec'][temp[1]], color['fg'][color_name]) + string + "\u001b[0m"
elif len(key2) == 3:
temp = item_iterate(key2)
return u"\u001b[{}\u001b[{}\u001b[{}\u001b[{}m".format(color['dec'][temp[0]], color['dec'][temp[1]], color['dec'][temp[2]], color['fg'][color_name]) + string + "\u001b[0m"
else:
i = key2[0].lower()
return u"\u001b[{}\u001b[{}m".format(color['dec'][i], color['fg'][color_name]) + string + "\u001b[0m"
except KeyError:
key2 = None
else:
return u"\u001b[{}m".format(color['fg'][color_name]) + string + "\u001b[0m"
return _method
for name in color_func_key:
_method = make_method(name)
setattr(ColorMe, name, _method)
for name in label_func_key:
_method = make_method(name)
setattr(ColorMe, name, _method)
ColorMe = ColorMe()