围绕终端着色、样式和定位的一种薄而实用的包装

blessings的Python项目详细描述


音高

祝福解除了一些限制性的假设 代码也很漂亮:

  • 使用样式,颜色,也许是一个小的定位不一定 清理整个 屏幕优先。
  • 在程序完成后,在缓冲区中保留多个scrollback屏幕 退出,就像一个表现良好的命令行应用程序应该退出一样。
  • 把那些嘈杂的,类似于c的呼叫从tigetstr和tparm中去掉,所以 您的代码不会被终端记账挤掉。
  • 当有人将您的输出重定向到文件时,请智能地执行操作,忽略 用户不想看到的终端控制代码(可选)。

前后

如果没有祝福,这就是你在底部打印下划线文本的方式 屏幕的:

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 = ''
print sc  # Save cursor position.
if cup:
    # tigetnum('lines') doesn't always update promptly, hence this:
    height = struct.unpack('hhhh', ioctl(0, TIOCGWINSZ, '\000' * 8))[0]
    print tparm(cup, height - 1, 0)  # Move cursor to bottom.
print 'This is {under}underlined{normal}!'.format(under=underline,
                                                  normal=normal)
print rc  # Restore cursor position.

那是一堆又长又乱的垃圾!我们再试一次,这次 带着祝福:

from blessings import Terminal

term = Terminal()
with term.location(0, term.height - 1):
    print 'This is', term.underline('pretty!')

好多了。

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

推荐PyPI第三方库


热门话题
使用jaxb2annotateplugin和XJC工具的java自定义注释   java组织。xeustechnologies。jcl无法加载WstxInputFactory类   java JUnit在格式化字符串上比较失败   java Bukkit配置部分getKeys   如何关闭Java流?   java Struts2正则表达式配置   链式事务注释的java奇怪行为   java在两个JButton之间使用变量   java签署APK时内容会发生什么变化?   java LWJGL:Slick:3D世界中的绘图字体   如何分解Java数组?   在Java MySql中处理多个过滤器   java如何在Firebase数据库中跳过初始OnChildaded事件触发   java如何在PreviewView中使用CameraX?   在子类#中重写父类后访问父类原始方法的java已解决   java找不到类型的属性   游戏框架游戏!框架+Java