Blessing - terminal app library#

Blessed is an easy, practical library for making terminal apps, by providing an elegant, well-documented interface to Colors, Keyboard input, and screen position and Location capabilities.

Learn by examples#

01_height_width.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4term.height, term.width 属性本质是一个函数, 可以动态获取 terminal 的高度和宽度.
 5"""
 6
 7import blessed
 8
 9term = blessed.Terminal()
10print(f"height = {term.height}, width = {term.width}")

02_dynamic_height_width.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4term.height, term.width 属性本质是一个函数, 可以动态获取 terminal 的高度和宽度.
 5你试试看在 Terminal 中运行这个程序, 并不断改变 Terminal 的大小, 你会看到输出的值也会不同.
 6"""
 7
 8import time
 9import blessed
10
11term = blessed.Terminal()
12
13for _ in range(10):
14    time.sleep(1)
15    print(f"height = {term.height}, width = {term.width}")

03_color.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4term.number_of_colors 可以获取当前 terminal 支持的颜色数量. 一般的 terminal 都支持 256,
 5高级的 terminal 例如 Mac 上的 iTerm 支持 16 位颜色.
 6
 7blessed 的牛逼之处在于在使用颜色的时候, 如果指定的颜色在当前 terminal 不支持, 它会自动找到
 8最近的所支持的颜色而不会报错.
 9"""
10
11import blessed
12
13term = blessed.Terminal()
14print(f"number_of_colors = {term.number_of_colors}")
15
16print(term.red("hello"))

04_capabilities.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4现代的 Terminal 会有一些高级功能, 例如闪烁. 但不是所有的 Terminal 都有这些功能. 例如在
 5PyCharm 自带的 terminal 中就不能 blink. blessed 可以允许你用函数使用这些功能, 但如果
 6Terminal 不支持这些功能, 则会自动降级将其变为普通字符串. 这样你就不需要为了兼容不同的 Terminal
 7而写不同的代码了.
 8"""
 9
10import time
11import blessed
12
13term = blessed.Terminal()
14print(term.blink("Insert System disk into drive A:"))
15time.sleep(3)

05_move_cursor.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4Terminal 是有光标的. 从你的界面的左上角起就是光标的位置. 但一般你的光标的起始位置是你的 Shell
 5界面的输入框处. Terminal 有很多方法可以用来移动光标, 注意, 移动光标并不会改变字符. 这些方法
 6都需要被 ``print(..., end="")`` 然后 ``sys.stdout.flush()`` 之后才能在 Terminal 上看到
 7效果.
 8
 9下面我们给出了一个让光标上下左右移动, 并用 home 回到左上角, 然后用 move_xy 定位到具体位置的例子.
10"""
11
12import time
13import sys
14import blessed
15
16term = blessed.Terminal()
17print("hello world")
18print("apple banana")
19print("blessed is awesome")
20time.sleep(1)
21
22print(term.move_up, end="")
23sys.stdout.flush()
24time.sleep(1)
25
26print(term.move_up(2), end="")
27sys.stdout.flush()
28time.sleep(1)
29
30print(term.move_right, end="")
31sys.stdout.flush()
32time.sleep(1)
33
34print(term.move_right(3), end="")
35sys.stdout.flush()
36time.sleep(1)
37
38print(term.move_left(1), end="")
39sys.stdout.flush()
40time.sleep(1)
41
42print(term.move_left(3), end="")
43sys.stdout.flush()
44time.sleep(1)
45
46print(term.move_down, end="")
47sys.stdout.flush()
48time.sleep(1)
49
50print(term.move_down(2), end="")
51sys.stdout.flush()
52time.sleep(1)
53
54print(term.home, end="")
55sys.stdout.flush()
56time.sleep(1)
57
58print(term.move_xy(3, 3), end="")
59sys.stdout.flush()
60time.sleep(1)

06_clear.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4Blessed 有四个跟清除 Terminal 屏幕相关的命令:
 5
 6- clear: 清除整个屏幕.
 7- clear_eol: end of line, 光标所在的行从光标往左边的全部内容. 光标的位置是在两个字符中间的,
 8    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
 9- clear_bol: begin of line, 光标所在的行从光标往右边的全部内容. 光标的位置是在两个字符中间的,
10    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
11- clear_eos: end of screen, 清除从光标往后的所有屏幕, 光标的位置是在两个字符中间的,
12    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
13
14这个例子展示了 clear 的效果.
15"""
16
17import time
18import blessed
19
20t = blessed.Terminal()
21
22print("line1")
23time.sleep(1)
24
25print("line2")
26time.sleep(1)
27
28print(t.clear())
29time.sleep(1)
30
31print("line3")
32time.sleep(1)
33
34print("line4")
35time.sleep(1)
36
37print(t.clear())
38time.sleep(1)

07_clear_eol_bol.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4测试 eol (end of line), bol (begin of line) 的效果.
 5
 6- clear_eol: end of line, 光标所在的行从光标往左边的全部内容. 光标的位置是在两个字符中间的,
 7    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
 8- clear_bol: begin of line, 光标所在的行从光标往右边的全部内容. 光标的位置是在两个字符中间的,
 9    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
10"""
11
12import sys
13import time
14import blessed
15
16t = blessed.Terminal()
17
18print("line1")
19print("line2")
20time.sleep(1)
21
22print(t.move_up, end="")
23print(t.move_right(2), end="")
24sys.stdout.flush()
25time.sleep(1)
26
27print(t.clear_eol, end="")
28sys.stdout.flush()
29time.sleep(1)
30
31print(t.move_up, end="")
32sys.stdout.flush()
33time.sleep(1)
34
35print(t.clear_bol, end="")
36sys.stdout.flush()
37time.sleep(1)

08_clear_eos.py

 1# -*- coding: utf-8 -*-
 2
 3"""
 4测试 eos (end of screen) 的效果.
 5
 6- clear_eos: end of screen, 清除从光标往后的所有屏幕, 光标的位置是在两个字符中间的,
 7    而光标后面如果有字符那么这个字符也会被清除. 注意, 清除内容并不会导致光标移动.
 8"""
 9
10import sys
11import time
12import blessed
13
14t = blessed.Terminal()
15
16print("line1")
17print("line2")
18print("line3")
19time.sleep(1)
20
21print(t.move_up(2), end="")
22print(t.move_right(2), end="")
23sys.stdout.flush()
24time.sleep(1)
25
26print(t.clear_eos, end="")
27sys.stdout.flush()
28time.sleep(1)