python3 api使用颜色和样式格式化消息

pytput的Python项目详细描述


GithubPyPiPython

Pytput

TL;DR: You can format your message with {message:bold,underline,purple}

简单的可执行文件和python3 api使用tput

中的颜色和样式格式化消息
  • pytput可执行文件,可在任何shell类的脚本中使用
  • python3模块,用于在任何python应用程序中直接格式化消息

help

安装

通过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)

TputFormatter

以下是可用样式的列表

Formatter keywordtput 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 likeprint 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")

utils

使用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"

CLI

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

force

使用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'

env

Note: If your program uses pytput API like the class TputFormatter, setting the environment variable PYTPUT_DISABLE=1 will disable all colors and styles.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
保存一段xml是一个新的xml解析器Java   java如何使用断言测试单链表。assertEquals()   java如何将多个选定图像从Gallery复制到Android中的另一个文件夹   重新触发异常时调用序列中的java差异   swing如何在Java中的GridLayout面板的特定单元格中添加标签?   java在更新引用实体之后,有没有办法更新其他实体中的列?   java如何在两个实体之间使用foreach   java方法add(Component)不适用于参数   Apache Tiles和Spring MVC中的java全局异常页面   java kSoap2发送集合   Java存储对象与直接调用其方法的性能对比?   java如何访问selenium中的nowrap元素   使用endpointsframeworktools生成OpenAPI文档时发生java错误   西/东方向的java JLabel不会显示在BorderLayout中   java ActiveMQ Spring客户端:如何更改处理器池?   java不能取消对void的引用;尝试使用生成器模式   javajavax。websocketclient:如何将大型二进制数据从clientendpoint发送到serverendpoint   运行Java代理时Java代理问题   如何将Web应用程序连接到Java/ABAP应用程序   javajackson处理问题