python3 api使用颜色和样式格式化消息
pytput的Python项目详细描述
Pytput
TL;DR: You can format your message with
{message:bold,underline,purple}
简单的可执行文件和python3 api使用tput
:
pytput
可执行文件,可在任何shell类的脚本中使用- python3模块,用于在任何python应用程序中直接格式化消息
安装
通过pip
:
# Install pip for python3
$ sudo apt install python3-pip
$ pip install pytput
或者您可以克隆项目并通过setup.py
:
# Install python3 $ sudo apt install python3 python3-setuptools # Clone repository $ git clone https://github.com/essembeh/pytput $ cd pytput # If you are in a virtualenv, install it with $ python3 setup.py install # or use --user if you want to install it in userland $ python3 setup.py install --user
设置开发环境:
$ git clone https://github.com/essembeh/pytput $ cd pytput $ make venv $ source venv/bin/activate (venv) $ make install # equivalent to ./setup.py install(venv) $ make # To run the tests, coverage and flake8(venv) $ make watch # to reinstall automatically if you modify the source code
用法
使用tputformatter类
您可以使用TputFormatter
类用styles修饰变量。
frompytputimportTputFormattertf=TputFormatter()# Build a colored string and print ittext=tf.format("{hello:bg_yellow,bold} {world:cyan,underline}!",hello="Hello",world="World")print(text)# You can combine multiple styles and common str.format spectext=tf.format("{hello:.2,bg_yellow,underline} {myvalue:04d,cyan}!",hello="Hello",myvalue=42)print(text)
以下是可用样式的列表
Formatter keyword | tput command |
---|---|
bold | ^{ |
dim | ^{ |
underline | ^{ |
blink | ^{ |
standout | ^{ |
reverse | ^{ |
reset | ^{ |
black | ^{ |
red | ^{ |
green | ^{ |
yellow | ^{ |
blue | ^{ |
purple | ^{ |
cyan | ^{ |
white | ^{ |
bg_black | ^{ |
bg_red | ^{ |
bg_green | ^{ |
bg_yellow | ^{ |
bg_blue | ^{ |
bg_purple | ^{ |
bg_cyan | ^{ |
bg_white | ^{ |
Styles can be combined with
','
like{message:underline,bold,yellow}
You can also reuse
string.Formatter
format specification like{myint:05d,underline,bold,yellow}
or{mystr:.10,underline,dim,red}
使用format like和print like函数
一些实用程序函数可用于简单地打印具有styles
frompytputimportprint_red,print_color,tput_format,tput_print,Color# These functions work like *print* builtin functionprint_red("My message is red")print_color("green","This message is green")print_color(Color.PURPLE,"This message is purple")print_color(4,"This message is blue")# Simply format a message usinf a TputFormattertext=tput_format("Build a string: {hello:bg_yellow} {world:cyan}!",hello="Hello",world="World")print(text)# Or simply use tc_print to replace the two lines abovetput_print("Directly print: {hello:bg_yellow} {world:cyan}!",hello="Hello",world="World")
使用pytput可执行文件
pytput附带了一个方便的可执行文件,可以直接从命令行使用颜色和样式。例如,这用于自定义shell脚本中的消息。
$ pytput '{0:red} {1:green}!''Hello''World' $ pytput "{0:bold,red} {1:underline,dim,yellow} {2:bg_purple,yellow,blink}""This is""a message""with styles ;)" $ pytput "{0:red,bold,underline}""Simple error message" $ pytput "{0:bg_purple,white,bold}""Another message"
See
pytput --help
for more details.
禁用pytput
默认情况下,如果sys.stdout.isatty()
是False
,则pytput api被禁用,因此如果在文件中对输出进行管道传输或重定向,则不会编写任何颜色或样式。
使用pytput
可执行文件,可以使用--force
参数强制使用颜色和样式。
# You get colors by default $ pytput '{0:red} {1:green}!''Hello''World'# Colors will be disabled $ pytput '{0:red} {1:green}!''Hello''World'| cat $ pytput '{0:red} {1:green}!''Hello''World' > /tmp/pytput.txt $ cat /tmp/pytput.txt # Colors will be enabled $ pytput --force '{0:red} {1:green}!''Hello''World'| cat $ pytput --force '{0:red} {1:green}!''Hello''World' > /tmp/pytput.txt $ cat /tmp/pytput.txt
使用pytput python3 api,即使sys.stdout
不是使用TputFormatter
的tty,也可以强制使用样式和颜色:
frompytputimportTputFormatter,tput_print# These lines won't have colors if you redirect stdout to a fileprint(TputFormatter().format("{0:red} {1:green}!\n","Hello","World"))tput_print("{0:red} {1:green}!\n","Hello","World"))# These line will have colors even if stdout is redirected print(TputFormatter(check_tty=False).format("{0:red} {1:green}!\n","Hello","World"))tput_print("{0:red} {1:green}!\n","Hello","World",check_tty=False))
通过在环境中设置PYTPUT_DISABLE
变量,可以完全禁用pytput
。
# Colors will be enabled $ pytput '{0:red} {1:green}!''Hello''World'# Colors will be disabled $ PYTPUT_DISABLE=1 pytput '{0:red} {1:green}!''Hello''World'
Note: If your program uses pytput API like the class
TputFormatter
, setting the environment variablePYTPUT_DISABLE=1
will disable all colors and styles.