在python中正确地完成了终端字符串样式。
colorful的Python项目详细描述
彩色
用python:tada:
这里有一个笑话
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))
主要功能
- 表现力强且一致的API(文档)
- 支持不同的颜色模式(8 ansi、256 ansi、真彩色)(文档)
- 支持预定义的很棒的样式(solarized,…)(文档)
- 支持自定义调色板(文档)
- 支持嵌套样式(文档)
- 支持不同的平台(在Windows上使用Colorama)
- 用于干净颜色模式、调色板或样式切换的上下文管理器(文档)
- 对彩色字符串支持
len()
(正确支持len协议" rel="nofollow">文档) - 从x11 rgb.txt(文档中支持颜色名称)
- 无依赖关系
用法
彩色支持所有主要的python版本:2.7,3.4,3.5,3.6和3.7。< BR> 我们建议使用最新版本的pypi
pip install colorful
彩色不需要任何特殊设置即可使用:
importcolorfulprint(colorful.italic_coral_on_beige('Hello World'))print(colorful.italic&colorful.coral_on_beige|'Hello World')print('{c.italic_coral_on_beige}Hello World{c.reset}'.format(c=colorful))
有关详细信息,请参阅样式A字符串"部分!
颜色模式
如今,终端不仅支持古老的8种ansi颜色,而且通常支持多达1600万种颜色,其中真彩色为2824位。如果他们不支持真正的颜色,他们可能至少支持256 ANSI调色板。
彩色支持以下颜色模式:
- 无颜色/禁用(<代码>彩色。无颜色)
- 8种颜色->;8种ANSI颜色(
彩色.ansi_8_颜色
) - 256色->;256 ANSI调色板(8位
彩色。ANSI 256色
)
- 16'777'215色->;真彩色(24位,
彩色。真彩色
)
默认情况下,彩色尝试自动检测终端支持的最佳颜色模式。有关详细信息,请参阅colored.terminal
。
但是,有时可以指定应使用什么颜色模式。
彩色提供多种方式来实现此目的:
(1)通过python api全局指定颜色模式
colorful.disable()colorful.use_8_ansi_colors()colorful.use_256_ansi_colors()colorful.use_true_colors()
如果在运行时更改颜色模式,它将立即全局生效。
(2)通过环境变量全局强制颜色模式
COLORFUL_DISABLE=1 python eggs.py # this process will not use ANY coloringCOLORFUL_FORCE_8_COLORS=1 python eggs.py # this process will use 8 ANSI colors by defaultCOLORFUL_FORCE_256_COLORS=1 python eggs.py # this process will use 256 ANSI colors by defaultCOLORFUL_FORCE_TRUE_COLORS=1 python eggs.py # this process will use true colors by default
(3)通过python api(contextmanager)在本地指定颜色模式
withcolorful.with_8_ansi_colors()asc:print(c.italic_coral_on_beige('Hello world'))withcolorful.with_256_ansi_colors()asc:print(c.italic_coral_on_beige('Hello world'))withcolorful.with_true_colors()asc:print(c.italic_coral_on_beige('Hello world'))
调色板
withcolorful.with_8_ansi_colors()asc:print(c.italic_coral_on_beige('Hello world'))withcolorful.with_256_ansi_colors()asc:print(c.italic_coral_on_beige('Hello world'))withcolorful.with_true_colors()asc:print(c.italic_coral_on_beige('Hello world'))
调色板
colored的python api基于颜色名称,如colored.bold_white_on_black('hello')
。在运行时,这些颜色名被转换成使用中的颜色模式支持的适当ansi escape code序列。但是,所有颜色名都注册在调色板中,这基本上是颜色名与其对应的rgb值之间的映射。非常像这样:
color_palette_example={'black':'#000000','white':'#FFFFFF',}
注意:根据使用的颜色模式,RGB值将减小以适合颜色模式的值域。
默认调色板是x11 rgb.txt调色板-它随colour一起提供,因此,您不必提供自己的调色板。
彩色的附带第二个内置的调色板,名为colornames。
这些颜色来自于颜色名称库的管理列表。
您可以通过colored.setup()
方法使用它们,如下所示:
colorful.setup(colorpalette=colorful.COLORNAMES_COLORS)
如果希望将文件中的另一个调色板设置为默认调色板,可以将colornous\u default\u调色板
环境变量设置为此文件:
COLORFUL_DEFAULT_COLOR_PALETTE=/usr/share/X11/rgb.txt python spam.py
文件必须是像x11 rgb.txt这样的txt文件,或者是json文件:
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))0
自定义调色板
彩色支持用自定义颜色更新或替换默认调色板。颜色必须指定为RGB十六进制或通道值:
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))1
样式
彩色支持一些著名的调色板,在彩色中使用所谓的样式:
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))2
已经支持以下样式:
<详情>方法语法可以是:
彩色。<;修饰语>;
彩色。<;修改器1>;<;修改器2>;
彩色。<;fg_color>;
多姿多彩。在背景颜色上
彩色。<;修改器>;<;最终颜色>;
彩色。<;修饰语>;<;背景颜色>;
彩色。<;fg_colors>;&u打开<;bg_color>;
彩色。<;修饰语>;<;fg颜色>;&u on;bg颜色>;
请注意,可以同时指定多个"修饰符"s。
可用的修改器是:
- 重置(明确地重置传递参数之前的所有样式)
- 粗体
- 暗显(不广泛支持)
- 斜体
- 下划线
- 慢速闪烁
- 快速闪烁
- 反转(不广泛支持)
- 隐藏(不广泛支持)
- 快速通过
可用颜色取决于您使用的调色板。默认情况下,所有x11 rgb.txt颜色都可用。
这种样式方法的返回值类型是colour.colorfulstring
。它正确地支持所有str()
方法,包括len()
从节名称中的语法可以看出,彩色支持嵌套样式。请参见嵌套样式。
(2)使用&;和和
设置字符串样式
和
彩色的实现\u或
\u和
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))4
注意:管道的作用与对样式执行方法调用的作用相同。
所以您可以执行(colored.bold&;colored.red)操作('hello world')
(3)用彩色.格式(string,*args,**kwargs)设置字符串样式
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))5
彩色将用相应的样式替换{c.<;style>;}
。不需要将c
的彩色对象传递到format()
-彩色可以处理这个问题。所有其他格式参数({<;name>;}
)都必须传递给colored.format()
调用asargs或kwarg
注意:{c.<;style>;}
中样式的相同语法、修饰符和颜色可用于(1)使用方法调用设置字符串样式
(4)使用彩色打印字符串。print(*strings,sep='',end='\n',file=sys.stdout,flush=false)
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))6
colored.print()
方法接受的参数与python 3.x内置的print()函数相同。
注意:对于python 2,您必须导入打印函数:from uuu future_uuuuu import print_u function
(5)使用str.format()
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))7
注意:在{c.<;style>;}
中,样式的语法、修饰符和颜色可以与(1)使用方法调用设置字符串的样式
嵌套样式
彩色的当将参数嵌套的
设置为真的
时,支持用它的方法调用语法嵌套样式。
如果像下面的第一个示例那样使用str.format()
甚至不需要nested=true
标志!
以下示例显示了该行为:
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))8
正确支持协议
彩色正确支持样式化字符串上的len()
协议(\uu len\uu
)。如上所述,当设置字符串样式时,将返回acolored.colorful string
对象。此对象返回长度(在调用len()
时),因为未样式化字符串将样式化字符串无缝集成到应用程序中。
importcolorful# create a colored string using clever method translationprint(colorful.bold_white('Hello World'))# create a colored string using `str.format()`print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=colorful))# nest colorsprint(colorful.red('red {0} red'.format(colorful.white('white'))))print(colorful.red('red'+colorful.white(' white ',nested=True)+'red'))# combine styles with stringsprint(colorful.bold&colorful.red|'Hello World')# use true colorscolorful.use_true_colors()# extend default color palettecolorful.update_palette({'mint':'#c5e8c8'})print(colorful.mint_on_snow('Wow, this is actually mint'))# choose a predefined stylecolorful.use_style('solarized')# print the official solarized colorsprint(colorful.yellow('yellow'),colorful.orange('orange'),colorful.red('red'),colorful.magenta('magenta'),colorful.violet('violet'),colorful.blue('blue'),colorful.cyan('cyan'),colorful.green('green'))# directly print with colorscolorful.print('{c.bold_blue}Hello World{c.reset}')# choose specific color mode for one blockwithcolorful.with_8_ansi_colors()asc:print(c.bold_green('colorful is awesome!'))# create and choose your own color paletteMY_COMPANY_PALETTE={'companyOrange':'#f4b942','companyBaige':'#e8dcc5'}withcolorful.with_palette(my_company_palette)asc:print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))# use f-string (only Python >= 3.6)print(f'{colorful.bold}Hello World')# support for chineseprint(colorful.red('你好'))9
暂时更改彩色设置
彩色提供了一手方便的上下文管理器来临时更改彩色设置:
(1)更改颜色模式
使用8种ANSI颜色:
pip install colorful0
使用256 ANSI颜色:
pip install colorful1
使用真彩色:
pip install colorful2
(2)更改调色板
pip install colorful
3
(3)改变风格
pip install colorful
4
此项目发布于MIT
atimo furrer项目下。
-:tada:-