一个易于使用、实用的用于创建终端应用程序的库,通过提供一个优雅、文档齐全的接口来提供颜色、键盘输入和屏幕定位功能。
项目描述
简介
Blessed是一个简单实用的库,用于制作终端应用程序,它通过提供优雅、文档齐全的接口,使您能够轻松使用颜色、键盘输入以及屏幕位置和位置功能。
from blessed import Terminal
term = Terminal()
print(term.home + term.clear + term.move_y(term.height // 2))
print(term.black_on_darkkhaki(term.center('press any key to continue.')))
with term.cbreak(), term.hidden_cursor():
inp = term.inkey()
print(term.move_down(2) + 'You pressed ' + term.bold(repr(inp)))
它的设计旨在让操作变得有趣且简单,使用Python和blessed进行基本的终端图形和样式设计。您只需要导入终端类,这是您需要导入的唯一类,也是实现终端功能所需使用的唯一对象。
无论您是想通过颜色增强CLI应用程序,还是制作全屏应用程序或游戏,blessed都能帮助您快速入门。您的用户会喜欢它,因为它在Windows、Mac和Linux上都能工作,您也会喜欢它,因为它拥有丰富的文档和示例!
完整文档请参阅https://blessed.readthedocs.io/en/latest/
示例
来自我们仓库的示例:x11-colorpicker.py、bounce.py、worms.py和plasma.py。
以下是一些使用blessed的第三方示例:
Voltron是一个用Python编写的可扩展的调试器UI工具包。
cursewords是一个用于在终端中解决填字游戏的“图形化”命令行程序。
GitHeat可以构建git历史记录的交互式热图。
Dashing是一个用于快速创建基于终端的仪表板的库。
Enlighten是一个允许同时输出而不需要重定向的终端进度条库。
macht是(短暂流行)拼图游戏2048的克隆。
要求
Blessed适用于Windows、Mac、Linux和BSD系统,支持Python 2.7、3.4、3.5、3.6、3.7和3.8。
简要概述
Blessed不仅仅是Python的curses包装器
它与Python的新f-strings或任何其他类型的字符串格式化完美配合。
它提供了最新的位置以及终端的高度和宽度,这样您就可以响应终端大小的变化。
如果输出被重定向到非终端,它避免了混乱,可以将输出序列输出到任何文件对象,如StringIO、文件、管道或套接字。
它使用terminfo(5),因此它可以与任何终端类型和功能一起工作:不再需要调用C-like的tigetstr和tparm。
仅对能力数据库的隐蔽调用确保您可以自由地与其他任何您喜欢的curses应用程序代码或库的调用混合匹配。
它提供了上下文管理器Terminal.fullscreen()和Terminal.hidden_cursor(),以安全地表达终端模式,curses开发将不再使您的shell混乱。
当有人将您的输出重定向到文件时,智能处理,省略所有特殊序列颜色,但仍包含所有文本。
祝福 是 blessings 的一个分支,它使用相同的 API 实现了所有上述功能,同时还进行了以下 增强
自 2019 年 12 月起支持 Windows!
简单的键盘处理:在系统首选区域设置中安全解码 Unicode 输入,并支持应用程序/箭头键。
支持 24 位颜色,使用 Terminal.color_rgb() 和 Terminal.on_color_rgb() 以及所有 X11 颜色(按名称,而不是按数字)。
使用 Terminal.get_location() 确定光标位置,使用 Terminal.cbreak() 或 Terminal.raw() 上下文管理器进入逐键输入模式,并使用 Terminal.inkey() 读取定时按键。
允许使用 Terminal.length() 确定包含序列的字符串的可打印长度,支持额外的 Terminal.wrap() 和 Terminal.center() 方法,分别是内置函数 textwrap.wrap() 和方法 str.center() 的终端感知变体。
允许使用 Terminal.strip_seqs() 或 Terminal.strip() 从包含它们的字符串中删除序列。
前后对比
使用内置的 curses 模块,您通常会在屏幕底部打印一些加下划线的文本
from curses import tigetstr, setupterm, tparm
from fcntl import ioctl
from os import isatty
import struct
import sys
from termios import TIOCGWINSZ
# If we want to tolerate having our output piped to other commands or
# files without crashing, we need to do all this branching:
if hasattr(sys.stdout, 'fileno') and isatty(sys.stdout.fileno()):
setupterm()
sc = tigetstr('sc')
cup = tigetstr('cup')
rc = tigetstr('rc')
underline = tigetstr('smul')
normal = tigetstr('sgr0')
else:
sc = cup = rc = underline = normal = ''
# Save cursor position.
print(sc)
if cup:
# tigetnum('lines') doesn't always update promptly, hence this:
height = struct.unpack('hhhh', ioctl(0, TIOCGWINSZ, '\000' * 8))[0]
# Move cursor to bottom.
print(tparm(cup, height - 1, 0))
print('This is {under}underlined{normal}!'
.format(under=underline, normal=normal))
# Restore cursor position.
print(rc)
使用 祝福 的相同程序简单如下
from blessed import Terminal
term = Terminal()
with term.location(0, term.height - 1):
print('This is ' + term.underline('underlined') + '!', end='')
项目详情
下载文件
下载适用于您平台的项目文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分发
构建分发版
blessed-1.20.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2cdd67f8746e048f00df47a2880f4d6acbcdb399031b604e34ba8f71d5787680 |
|
MD5 | a640803116e0273f3ef1178626cb6282 |
|
BLAKE2b-256 | 25ae92e9968ad23205389ec6bd82e2d4fca3817f1cdef34e10aa8d529ef8b1d7 |
blessed-1.20.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0c542922586a265e699188e52d5f5ac5ec0dd517e5a1041d90d2bbf23f906058 |
|
MD5 | c0a8ad3642980dcac189e36abc547cae |
|
BLAKE2b-256 | 7698584f211c3a4bb38f2871fa937ee0cc83c130de50c955d6c7e2334dbf4acb |