本帖最后由 thepoy 于 2021-5-28 20:23 编辑
之前写彩色日志的时候用到了彩色文字打印,后来觉得彩色文字不只应用于日志中,其他场合下也可能会用到,比如著名的 ipython。
于是就将彩色文字部分独立出来,方便以后使用,也方便有兴趣的朋友使用。
可以直接用 pip 安装:
pip install colort
源码也很简单:
class _Mode:
@property
def normal(self):
"""终端默认样式"""
return 0
@property
def bold(self):
"""高亮或加粗"""
return 1
@property
def underline(self):
"""下划线"""
return 4
@property
def blink(self):
"""闪烁"""
return 5
@property
def invert(self):
"""反白"""
return 7
@property
def hide(self):
"""隐藏"""
return 8
class _ForegroundColor:
@property
def black(self):
return 30
@property
def red(self):
return 41
@property
def green(self):
return 32
@property
def yellow(self):
return 33
@property
def blue(self):
return 34
@property
def purple(self):
return 35
@property
def cyan(self):
return 36
@property
def white(self):
return 37
class _BackgroudColor:
@property
def black(self):
return 40
@property
def red(self):
return 41
@property
def green(self):
return 42
@property
def yellow(self):
return 43
@property
def blue(self):
return 44
@property
def purple(self):
return 45
@property
def cyan(self):
return 46
@property
def white(self):
return 47
class DisplayStyle:
foreground_color = _ForegroundColor()
backgorud_color = _BackgroudColor()
mode = _Mode()
@property
def end(self):
return 0
def format_with_one_style(self, src: str, style: int) -> str:
if type(src) != str:
raise TypeError(f"type of `src` is {type(src)}, not str")
if type(style) != int:
raise TypeError(f"type of `style` is {type(src)}, not int")
fmt = '\033[%dm%s\033[%dm'
return fmt % (style, src, self.end)
def format_with_multiple_styles(self, src: str, *styles: int) -> str:
if type(src) != str:
raise TypeError(f"type of `src` is {type(src)}, not str")
if len(styles) < 2:
raise TypeError("At least two styles")
styles_str = []
for style in styles:
if type(style) != int:
raise TypeError(f"type of `style` - [ {style} ] is {type(src)}, not int")
styles_str.append(str(style))
style = ";".join(styles_str)
return f"\033[{style}m{src}\033[{self.end}m"
使用也很简单,只有两个方法:
from colort import display_style as ds
src = "这是要显示的文本"
dist = ds.format_with_one_style(src, ds.foreground_color.red)
print("更改文字颜色:", dist)
dist = ds.format_with_one_style(src, ds.backgorud_color.green)
print("更改文字背景色:", dist)
dist = ds.format_with_multiple_styles(src, ds.foreground_color.red, ds.mode.underline, ds.mode.bold)
print("多种样式:", dist)
效果图:
实际应用:
|