跳转到主要内容

一个易于使用、实用的用于创建终端应用程序的库,通过提供一个优雅、文档齐全的接口来提供颜色、键盘输入和屏幕定位功能。

项目描述

Downloads codecov.io Code Coverage Windows supported Linux supported MacOS supported BSD supported

简介

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)))
Animation of running the code example

它的设计旨在让操作变得有趣简单,使用Python和blessed进行基本的终端图形和样式设计。您只需要导入终端类,这是您需要导入的唯一类,也是实现终端功能所需使用的唯一对象。

无论您是想通过颜色增强CLI应用程序,还是制作全屏应用程序或游戏,blessed都能帮助您快速入门。您的用户会喜欢它,因为它在Windows、Mac和Linux上都能工作,您也会喜欢它,因为它拥有丰富的文档和示例!

完整文档请参阅https://blessed.readthedocs.io/en/latest/

示例

Animations of x11-colorpicker.py, bounce.py, worms.py, and plasma.py

来自我们仓库的示例:x11-colorpicker.pybounce.pyworms.pyplasma.py

以下是一些使用blessed的第三方示例:

Screenshot of 'Voltron' (By the author of Voltron, from their README).

Voltron是一个用Python编写的可扩展的调试器UI工具包。

Animation of 'cursewords' (By the author of cursewords, from their README).

cursewords是一个用于在终端中解决填字游戏的“图形化”命令行程序。

Animation of 'githeat.interactive', using blessed repository at the time of capture.

GitHeat可以构建git历史记录的交互式热图。

Animations from 'Dashing' (By the author of Dashing, from their README)

Dashing是一个用于快速创建基于终端的仪表板的库。

Animations from 'Enlighten' (By the author of Enlighten, from their README)

Enlighten是一个允许同时输出而不需要重定向的终端进度条库。

Demonstration of 'macht', a 2048 clone

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的tigetstrtparm

  • 仅对能力数据库的隐蔽调用确保您可以自由地与其他任何您喜欢的curses应用程序代码或库的调用混合匹配。

  • 它提供了上下文管理器Terminal.fullscreen()Terminal.hidden_cursor(),以安全地表达终端模式,curses开发将不再使您的shell混乱。

  • 当有人将您的输出重定向到文件时,智能处理,省略所有特殊序列颜色,但仍包含所有文本。

祝福blessings 的一个分支,它使用相同的 API 实现了所有上述功能,同时还进行了以下 增强

前后对比

使用内置的 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 (6.7 MB 查看哈希值)

上传时间 源代码

构建分发版

blessed-1.20.0-py2.py3-none-any.whl (58.4 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下支持