print in terminal with colors:
# -----------------colorama模块的一些常量---------------------------
# Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Style: DIM, NORMAL, BRIGHT, RESET_ALL
# author ppluer 2018/10/20
# ver:1.0
from colorama import init, Fore, Back, Style
class Colored(object):
def __init__(self):
init(autoreset=True)
self.color_data=[
{'func':self.green,'kw':['success','successfully','succeeded','enabled','true']},
{'func':self.red,'kw':['fail','no','error','failed','false']},
{'func':self.yellow,'kw':['warnning','warn','disabled','unknown','unable','cannot']},
{'func':self.cyan,'kw':['info','information','starting','creating']},
{'func':self.blue,'kw':['pass','passed']},
{'func':self.magenta,'kw':['none','localhost','null']}
]
#将函数名作为参数存起来 我真是机智, 关键词全部使用小写 匹配的时候不分大小写
# 前景色:红色 背景色:默认
def red(self, s):
return Style.BRIGHT+Fore.RED + s + Style.RESET_ALL #
# 前景色:绿色 背景色:默认
def green(self, s):
return Style.BRIGHT+Fore.GREEN + s + Style.RESET_ALL
# 前景色:黄色 背景色:默认
def yellow(self, s):
return Style.BRIGHT+Fore.YELLOW + s + Style.RESET_ALL
# 前景色:蓝色 背景色:默认
def blue(self, s):
return Style.BRIGHT+Fore.BLUE + s + Style.RESET_ALL
# 前景色:洋红色 背景色:默认
def magenta(self, s):
return Style.BRIGHT+Fore.MAGENTA + s + Style.RESET_ALL
# 前景色:青色 背景色:默认
def cyan(self, s):
return Style.BRIGHT+Fore.CYAN + s + Style.RESET_ALL
# 前景色:白色 背景色:默认
def white(self, s):
return Style.BRIGHT+Fore.WHITE + s + Style.RESET_ALL
# 前景色:黑色 背景色:默认
def black(self, s):
return Style.BRIGHT+Fore.BLACK
# 前景色:白色 背景色:绿色
def white_green(self, s):
return Style.BRIGHT+Fore.WHITE + Back.GREEN + s + Style.RESET_ALL
def fill_color(self,wd): #将wd与color_data内的kw对比 匹配则调用相应的function处理
for x in self.color_data:
if wd.lower() in x['kw']: #转换成小写匹配
return x['func'](wd)
else:
pass
return wd
def capture_word(self,ba,startabc,stopabc,ba_copy):
#ba:原字符数组
#startabc:字母开始位置
#stopabc:字母结束位置+1
#ba_copy:新生成的字符数组
length=len(ba)
ba_clip=bytearray()
if startabc==stopabc:
print('error1')
return
if startabc>stopabc:
print('error2')
return
if stopabc>length:
print('error3')
return
for i in range(startabc,stopabc):
ba_clip.append(ba[i]) #将ba内 range(start-stop)索引的byte 组成一个新的字符数组:ba_clip
wd=ba_clip.decode(encoding='utf-8') #字节数组ba_clip转换成字符串wd
wd=self.fill_color(wd) #wd 传递给fill_color
for b in bytearray(wd,'utf-8'): #将返回的带color字符串wd 转换成字节数组 再合并入ba_copy
ba_copy.append(b)
def main(self,s):
#自动根据关键字自动添加颜色
ba=bytearray(s,'utf-8')
ba_copy=bytearray()
length=len(ba)
startabc=0
stopabc=0
foundabc=False
for i in range(length):
if self.isabc(ba[i]): #如果是字母
if not foundabc: #且之前尚未发现字母的情况下 标记开始位置 标记发现字母 ,
startabc=i #已发现字母的情况下不再更新start位置
foundabc=True
if i==length-1: #最后一个字母被发现 需要触发stop标记 触发capture_word
stopabc=length
self.capture_word(ba,startabc,stopabc,ba_copy)
else: #发现非字母 触发标记stop
stopabc=i
if foundabc: self.capture_word(ba,startabc,stopabc,ba_copy) #如前面发现字母那就触发capture_word
ba_copy.append(ba[i]) #将非字母copy走
foundabc=False #发现字母标记为false
s=ba_copy.decode(encoding='utf-8') #拷贝完转换为字符串
return s
def isabc(self,c): # my function like isalpha 处理unicode的时候比isalpha准确
if c in range(65,91) or c in range(97,123):
return True
if __name__=="__main__":
color = Colored()
print(color.red('I am red!'))
print(color.green('I am green!'))
print(color.yellow('I am yellow!'))
print(color.blue('I am blue!'))
print(color.magenta('I am magenta!'))
print(color.cyan('I am cyan!'))
print(color.white('I am white!'))
print(color.white_green('I am white green!'))
print(color.main('i am pass passed Pass success successful SUCCESS ERROR INFO WARNnING e'))
print(color.main('i am successful hahaha SUCCESSED!succeeded null NONE False cannot unable True'))
print(color.main('i am successful fail error SUCCESS success'))
print(color.main('123!@#¥%……&*()-=-=——+【】[]{}`!@#$%^&*()'))
sss='我是张大爷123!@#¥%……&*()-=-=——+【】[]{}`!@#$%^&*()'
ba=bytearray(sss,'utf-8')
for i in range(len(ba)):
if color.isabc(ba[i]):
print('found abc index number is:',i)
for s in sss:
if s.isalpha():
print('你看吧 内置的函数isaplha 遇到unicode中文发生误判,:',s)