tColorP-python控制台彩色输出
通过在输出文件开头和结尾添加\033[输出模式;文字颜色;背景色进行改变
使用方法
您可以使用1
pip install tColorP
安装
1 | from TColorP import TColorP, TColor |
源码讲解
程序分为两个Class。
TColor存储支持的颜色和模式。
TColorP进行输出。
将要改变的样式作为列表传入formatColor函数中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
31def formatColor(self,style:list) -> None:
'''
format color,print after this function will changed as arg style.
:param style: list of style,use TColor or number
:return: None
'''
if not len(style):
return
method = -1
foreground = -1
background = -1
for i in style:
if i in [0,1,4,7]:
method = i
if i >=30 and i <= 37:
foreground = i
if i >= 40 and i <= 47:
background = i
if method == -1:
method = 0
if background >= 0:
print(f'\033[{method};{foreground};{background}m',end='')
else:
if foreground >= 0:
print(f'\033[{method};{foreground}m',end='')
else:
print(f'\033[{method}m',end='')
例如默认的成功样式为正常输出,绿色文字1
2
3
4
5
6
7
8
9
10
11
12def success(self,txt:str,**args) -> None:
'''
print any text with normal style and foreground is green,change once with style arg.
:param txt: text you want to print
:param args: end arg like print(),style arg for change print style once
:return: None
'''
style = args.get('style',[TColor.method_normal,TColor.foreground_green])
self.formatColor(style)
end = args.get('end', '\n')
print(txt, end=end)
self.formatColor([TColor.method_normal])







