-
Notifications
You must be signed in to change notification settings - Fork 45
/
font_color.py
100 lines (86 loc) · 2.89 KB
/
font_color.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
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import platform
class Color(object):
"""
reference from https://gist.github.com/Jossef/0ee20314577925b4027f and modified bit.
"""
def __init__(self, text, **user_styles):
styles = {
# styles
'reset': '\033[0m',
'bold': '\033[01m',
'disabled': '\033[02m',
'underline': '\033[04m',
'reverse': '\033[07m',
'strike_through': '\033[09m',
'invisible': '\033[08m',
# text colors
'fg_black': '\033[30m',
'fg_red': '\033[31m',
'fg_green': '\033[32m',
'fg_orange': '\033[33m',
'fg_blue': '\033[34m',
'fg_purple': '\033[35m',
'fg_cyan': '\033[36m',
'fg_light_grey': '\033[37m',
'fg_dark_grey': '\033[90m',
'fg_light_red': '\033[91m',
'fg_light_green': '\033[92m',
'fg_yellow': '\033[93m',
'fg_light_blue': '\033[94m',
'fg_pink': '\033[95m',
'fg_light_cyan': '\033[96m',
'fg_white': '\033[97m',
'fg_default': '\033[99m',
# background colors
'bg_black': '\033[40m',
'bg_red': '\033[41m',
'bg_green': '\033[42m',
'bg_orange': '\033[43m',
'bg_blue': '\033[44m',
'bg_purple': '\033[45m',
'bg_cyan': '\033[46m',
'bg_light_grey': '\033[47m'
}
self.color_text = ''
for style in user_styles:
try:
self.color_text += styles[style]
except KeyError:
raise KeyError('def color: parameter `{}` does not exist'.format(style))
self.color_text += text
if platform == 'win32':
self.color_text = text
def __format__(self):
if platform == 'win32':
return "{}".format(self.color_text)
return '\033[0m{}\033[0m'.format(self.color_text)
@classmethod
def bold(clazz, text):
cls = clazz(text, bold=True)
return cls.__format__()
@classmethod
def red(clazz, text):
cls = clazz(text, bold=True, fg_red=True)
return cls.__format__()
@classmethod
def orange(clazz, text):
cls = clazz(text, bold=True, fg_orange=True)
return cls.__format__()
@classmethod
def blue(clazz, text):
cls = clazz(text, bold=True, fg_light_blue=True)
return cls.__format__()
@classmethod
def green(clazz, text):
cls = clazz(text, bold=True, fg_green=True)
return cls.__format__()
@classmethod
def purple(clazz, text):
cls = clazz(text, bold=True, fg_purple=True)
return cls.__format__()
@classmethod
def custom(clazz, text, **custom_styles):
cls = clazz(text, **custom_styles)
return cls.__format__()