大量使用带有pyfiglet集成的ansi转义序列的打印实用程序集合
printtools的Python项目详细描述
打印
重载python的print
函数以添加新的令人兴奋的功能。
依赖关系
- python3(python2不允许print语句重载)
- Pyfiglet(阿西艺术,有人吗?)
安装
使用安装
pip install printtools
对于最新版本,请克隆此存储库并使用
python3 setup.py install
用法
首先,import *
从print访问所有常量。然后,调用print语句。第一个参数是要打印的对象;所有其他参数都被视为修饰符。例如:
>>> from print import *
>>> print("You can print normally")
You can print normally
>>> print(12345678)
12345678
>>> print("or with ASCII art", SM)
_ _ _ _ ___ ___ ___ ___ _
___ _ _ __ __ _(_) |_| |_ /_\ / __|/ __|_ _|_ _| __ _ _ _| |_
/ _ \ '_| \ V V / | _| ' \ / _ \\__ \ (__ | | | | / _` | '_| _|
\___/_| \_/\_/|_|\__|_||_| /_/ \_\___/\___|___|___| \__,_|_| \__|
>>> print("you can combine attributes", RED, BLUE+BG, BOLD)
you can combine attributes
<this will be bold red on a blue background on supported systems>
日志记录
Print
带有内置日志记录。使用set_log
设置所需的文件,并将追加行:
fromprintimport*set_log('log.txt')print('asdf')# is appended to log.txtprint('Test',SLANT)# is also appended to log.txt as ASCII artprint('Red',RED)# ANSI escape sequences not written
若要禁用日志记录,请使用set_log()
或set_log(None)
将set_log
设置为“无”。
分隔器
div
模块可用于创建美观的分隔符:
>>> div.div('-')
----------------------------------------- # stretches all the way across the terminal
>>> div.div('*', 'Example')
** Example ******************************
>>> div.div('= =', 'Example')
=========================================
Example
=========================================
图案包括:
- 单个字符:在屏幕上重复该字符。如果提供了消息,则显示两个字符,后跟空格、消息、空格,然后是占据屏幕其余部分的字符
- 字符重复,中间有空格:使用该字符在屏幕上绘制两条水平线;在这两条线之间,标签文本在中心绘制。等于空格数的空行将添加到标签文本的顶部和下方。
表格
使用table
模块打印表。表格由二维数组组成:
>>> from print import *
>>> t = [["Entry 1", 0.0001, "Value 1"], ["Entry 2", 12345, "Value 2"]]
>>> table.table(t)
+-------+------+-------+
|Entry 1|0.0001|Value 1|
+-------+------+-------+
|Entry 2|12345 |Value 2|
+-------+------+-------+
table.render
和render
可以组合起来添加格式:
>>> from print import *
>>> t = [
[render("Entry 1", RED), 0.0001, "Value 1"],
[render("Entry 2", GREEN), 12345, "Value 2"]]
>>> print(table.render(t), BOLD)
自定义异常
使用RenderedException
类提供更丰富多彩的基本异常类。
>>> class YouFuckedUpError(RenderedException):
... pass
...
>>> raise YouFuckedUpError("You've really fucked it up this time. Yeah you.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.YouFuckedUpError:
__ __ ___ _ _ _ _ ___
\ \ / /__ _ _| __| _ __| |_____ __| | | | |_ __| __|_ _ _ _ ___ _ _
\ V / _ \ || | _| || / _| / / -_) _` | |_| | '_ \ _|| '_| '_/ _ \ '_|
|_|\___/\_,_|_| \_,_\__|_\_\___\__,_|\___/| .__/___|_| |_| \___/_|
|_|
You've really fucked it up this time. Yeah you.
常数
所有常量都可以通过from print import *
或通过调用print.<CONST>
访问。
基本格式
RESET=0BOLD=1FAINT=2ITALIC=3UNDERLINE=4REVERSE=7CONCEAL=8STRIKEOUT=9
颜色
BLACK=30RED=31GREEN=32YELLOW=33BLUE=34MAGENTA=35CYAN=36WHITE=37
修饰语
BRIGHT=60BR=60BACKGROUND=10BG=10
BRIGHT
/BR
:使用较亮的颜色(并非所有终端都提供;您的里程可能会有所不同)BACKGROUND
/BG
:设置背景色而不是文本颜色
字体
只有这些字体包含为常量。其他figlet字体(参见figlet的website)可以作为字符串参数传递。
# Standard fontsSM="small"STD="standard"BIG="big"# Isometric fontsISO1="isometric1"ISO2="isometric2"ISO3="isometric3"ISO4="isometric4"# Other fontsSA="contessa"DOOM="doom"DP="drpepper"L3D="larry3d"SMISO="smisome1"KB="smkeyboard"SLANT="slant"SMSLANT="smslant"