Python3 API 使用颜色和样式格式化消息
项目描述
PyTput
TL;DR:您可以使用
{message:bold,underline,purple}
简单的可执行文件和Python3 API使用以下颜色和样式格式化消息tput:
pytput可在任何类似 shell 的脚本中使用的可执行文件- Python3模块直接在任何 python 应用程序中格式化消息
安装
通过安装pip:
# Install pip for python3
$ sudo apt install python3 python3-pip python3-setuptools
# Install from PyPI
$ pip3 install --user pytput
# ... Or directly from the sources
$ pip3 install --user git+https://github.com/essembeh/pytput
要设置开发环境:
$ git clone https://github.com/essembeh/pytput
$ cd pytput
$ make venv
$ make install
$ source venv/bin/activate
(venv) $ pytput --help
用法
使用tput_format和tput_print
tput_format允许使用颜色和样式格式化消息。这无非是一个使用的捷径TputFormatter。
tput_print只是在格式化后打印消息。
from pytput import tput_format, tput_print
# Create a format message
fmt = "Hello {who:red,bold}"
# You can print it
print(fmt)
# When formatting, styles are applied
a = tput_format(fmt, who="World")
print(a)
# Equivalent to
tput_print(fmt, who="World")
# And you can also use *constant* like {'today':bold} will be the string "today" with bold style applied
fmt += ", how are you {'today':green,underline}"
tput_print(fmt, who="User")
样式可以与
','like组合{message:underline,bold,yellow}
您还可以重用
string.Formatter格式规范,例如{myint:05d,underline,bold,yellow}或{mystr:.10,underline,dim,red}
{'today':bold}您可以使用或将样式应用于常量字符串{"today":bold}
使用TputFormatter类
您可以使用TputFormatter该类来装饰带有样式的变量。
from pytput import TputFormatter
tf = TputFormatter()
# Build a colored string and print it
print(tf.format("{'Hello':bg_yellow,bold} {who:cyan,underline}!", who="World"))
# You can combine multiple styles and common str.format spec
print(tf.format("{'Hello':.2,bg_yellow,underline} {myvalue:04d,cyan}!", myvalue=42))
这是可用样式的列表
| 格式化关键字 | 输入命令 |
|---|---|
| 大胆的 | tput bold |
| 暗淡 | tput dim |
| 强调 | tput smul |
| 眨 | tput blink |
| 站出来 | tput smso |
| 撤销 | tput rev |
| 重置 | tput sgr0 |
| 黑色的 | tput setaf 0 |
| 红色的 | tput setaf 1 |
| 绿色 | tput setaf 2 |
| 黄色 | tput setaf 3 |
| 蓝色的 | tput setaf 4 |
| 紫色的 | tput setaf 5 |
| 青色 | tput setaf 6 |
| 白色的 | tput setaf 7 |
| bg_black | tput setab 0 |
| bg_red | tput setab 1 |
| bg_green | tput setab 2 |
| bg_黄色 | tput setab 3 |
| bg_blue | tput setab 4 |
| bg_purple | tput setab 5 |
| bg_cyan | tput setab 6 |
| bg_white | tput setab 7 |
直接使用样式
from pytput import Style
print(Style.RED.apply("Hello"), Style.GREEN.apply("World"))
使用pytput可执行文件
PyTput带有一个方便的可执行文件,可以直接从命令行使用颜色和样式。例如,这对于自定义 shell 脚本中的消息很有用。
$ pytput '{"Hello":red} {0:green}!' 'World'
$ pytput "{'This is':bold,red} {0:underline,dim,yellow} {1:bg_purple,yellow,bold}" "a message" "with styles ;)"
$ pytput "{0:red,bold,underline}" "Simple error message"
$ pytput "{'Another message':bg_purple,white,bold}"
有关
pytput --help更多详细信息,请参阅。
禁用pytput
默认情况下,如果is , pytput API是禁用的,因此如果您通过管道输出或将其重定向到文件中,您将不会编写任何颜色或样式。sys.stdout.isatty()False
使用pytput可执行文件,您可以使用--force参数强制颜色和样式。
# You get colors by default
$ pytput '{0:red} {"World":green}!' 'Hello'
# Colors will be disabled
$ pytput '{0:red} {"World":green}!' 'Hello' | cat
$ pytput '{0:red} {"World":green}!' 'Hello' > /tmp/pytput.txt
$ cat /tmp/pytput.txt
# Colors will be enabled
$ pytput --force '{0:red} {"World":green}!' 'Hello' | cat
$ pytput --force '{0:red} {"World":green}!' 'Hello' > /tmp/pytput.txt
$ cat /tmp/pytput.txt
使用pytput python3 API,即使sys.stdout不是 TTY ,您也可以强制样式和颜色TputFormatter:
from pytput import TputFormatter, tput_print
# These lines won't have colors if you redirect stdout to a file
print(TputFormatter().format("{0:red} {'World':green}!", "Hello"))
tput_print("{0:red} {'World':green}!", "Hello"))
# These line will have colors even if stdout is redirected
print(TputFormatter(check_tty=False).format("{0:red} {'World':green}!", "Hello"))
tput_print("{0:red} {'World':green}!", "Hello", check_tty=False))
pytput您可以通过在环境中设置PYTPUT_DISABLE变量来完全禁用。
# Colors will be enabled
$ pytput '{0:red} {"World":green}!' 'Hello'
# Colors will be disabled
$ PYTPUT_DISABLE=1 pytput '{0:red} {"World":green}!' 'Hello'
注意:使用pytput API (
tput_print,TputFormatter...) 时,设置环境变量PYTPUT_DISABLE=1也会禁用所有颜色和样式。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。