依靠终端的显示机制,不需要安装其他支持库,拿来即可用
简单设置一下颜色和显示方式,就可以定义属于自己的color_print
了
color = {
'b': 0,
'r': 1,
'g': 2,
'y': 3,
'blue': 4,
'p': 5,
'c': 6,
'w': 7
}
method_lst = {
'default': 0,
'highlight': 1,
'bold': 2,
'non_bold': 22,
'underline': 4,
'non_underline': 24,
'blink': 5,
'reverse': 7
}
def color_print(s, method='default', front_color='w', back_color='b'):
start = f'\033[{method_lst[method]};3{color[front_color]};4{color[back_color]}m'
end = '\033[0m'
print(start+s+end)
if __name__ == '__main__':
s = "this is a test text\n"
color_print(s, 'underline')
color_print(s, front_color='c')
color_print(s, 'highlight','r','y')
运行结果: