简单介绍:
此模块儿常用来终端着色,终端样式,以及终端定位
终端对象:
blessings.Terminal() -> terminal obj
说明:创建一个终端对象
t.height -> int
说明:获取当前终端高度
t.width -> int
说明:获取当前终端宽度
t.[style1]_...[styleN]_[fore_color]_on_[back_color]
t.[style1]_...[styleN]_[fore_color]_on_bright_[back_color]
1.style :bold,reverse,underline,no_underline,blink,normal
2.fore_color:black, red, green, yellow, blue, magenta, cyan, white
3.back_color:on_black,on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
说明:多重设置终端样式,可以设置多个附加style并用_隔开即可,对于背景色可在on后面加bright来提升亮度,而且每一个style, fore_color, back_color都可以作为方法或是属性使用
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import blessings
def main():
"""Main function."""
term_dict = {}
head_format = '%-*s : %-s'
info_format = '%*s : %-s'
head_message = 'Cur Terminal Info'
# get cur terminal width (real-time changed)
term_dict.update({'width': TERMINAL.width})
# get cur terminal height (real-time changed)
term_dict.update({'height': TERMINAL.height})
# style overlap
term_dict.update({'func_blod_underline_green_on_yellow':
TERMINAL.bold_underline_green_on_yellow('I Love You!')})
term_dict.update({'attr_blod_underline_green_on_yellow':
'%sI Love You!%s' % (
TERMINAL.bold_underline_green_on_yellow,
TERMINAL.normal)})
sorted_dict = sorted(term_dict.iteritems(), key=lambda k: len(k[0]))
head_length = len(sorted_dict[-1][0])
head_message = head_format % (head_length, head_message, '')
# use as attribute or function
print TERMINAL.red(head_message)
for item in sorted_dict:
print info_format % (head_length, item[0], item[1])
if __name__ == '__main__':
TERMINAL = blessings.Terminal()
main()
t.location([x], [y]) -> None
说明:在指定位置然后再返回原来位置,必须和with连用
t.move_up -> escape
t.move_down -> escape
t.move_left -> escape
t.move_right -> escape
t.move_x(x) -> escape
t.move_y(y) -> escape
t.move([x], [y]) -> escape
说明:移动到指定位置,可配合t.location([x], [y])来任意定位输出,必须+字符串实现,字符串放后
t.clear -> escape
说明:清除全屏
t.clear_eol -> escape
说明:清除光标后内容
t.clear_bol -> escape
说明:清除光标前内容
t.clear_eos -> escape
说明:清除光标下内容
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import sys
import datetime
import blessings
def main():
"""Main function."""
date_width = int(TERMINAL.width - 30)
with TERMINAL.location(0, TERMINAL.height-1):
print TERMINAL.clear_eol + 'author: %s' % ('limanman'),
with TERMINAL.location():
print ''.join([
TERMINAL.move_x(date_width),
'cur_date: %s' % (datetime.datetime.now().__str__()[:19])
]),
if __name__ == '__main__':
TERMINAL = blessings.Terminal()
main()
print