blob: a83b75647766f1aa356706fbd260944509c33e74 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
"""Console helpers for pippy."""
import sys
def clear():
"""Clear screen on console."""
# magic escape sequence
sys.stdout.write('\x1B[H\x1B[J')
def size():
"""Return the number of rows/columns in the current terminal widget."""
# xterm magic! see http://rtfm.etla.org/xterm/ctlseq.html
import os, tty, termios
fd = os.open('/dev/tty', os.O_RDWR|os.O_APPEND)
def read_to_delimit(delimit):
buf = []
while True:
c = os.read(fd, 1)
if c==delimit: break
buf.append(c)
return ''.join(buf)
oldattr = termios.tcgetattr(fd) # make sure we can restore tty state
tty.setraw(fd, termios.TCSANOW) # set to raw mode.
os.write(fd, '\x1B[18t') # write the 'query screen size' command
read_to_delimit('\x1b') # parse response.
read_to_delimit('[')
rows = int(read_to_delimit(';'))
cols = int(read_to_delimit('t'))
termios.tcsetattr(fd, termios.TCSANOW, oldattr) # reset tty
return rows, cols
def normal():
"""Switch to normal text."""
sys.stdout.write('\x1B[0m')
def bold():
"""Switch to bold text."""
sys.stdout.write('\x1B[1m')
def underlined():
"""Switch to underlined text."""
sys.stdout.write('\x1B[4m')
def inverse():
"""Switch to underlined text."""
sys.stdout.write('\x1B[7m')
def black():
"""Change text color to black."""
# magic escape sequence.
sys.stdout.write('\x1B[30m')
def red():
"""Change text color to red."""
# magic escape sequence.
sys.stdout.write('\x1B[31m')
def green():
"""Change text color to green."""
# magic escape sequence.
sys.stdout.write('\x1B[32m')
def yellow():
"""Change text color to yellow."""
# magic escape sequence.
sys.stdout.write('\x1B[33m')
def blue():
"""Change text color to blue."""
# magic escape sequence.
sys.stdout.write('\x1B[34m')
def magenta():
"""Change text color to magenta."""
# magic escape sequence.
sys.stdout.write('\x1B[35m')
def cyan():
"""Change text color to cyan."""
# magic escape sequence.
sys.stdout.write('\x1B[36m')
def white():
"""Change text color to white."""
# magic escape sequence.
sys.stdout.write('\x1B[37m')
def reset():
"""Clear screen and reset text color."""
clear()
sys.stdout.write('\x1B[0;39m')
|