围绕终端着色、样式和定位的一种薄而实用的包装
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!')
好多了。