-
Notifications
You must be signed in to change notification settings - Fork 2
/
vt100.py
39 lines (33 loc) · 805 Bytes
/
vt100.py
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
import sys
def get_cursor_position():
height=0
width=0
num=''
sys.stdout.write('\x1b[6n')
while True:
char = sys.stdin.read(1)
if char is 'R':
width = int(num)
break
if char is '\x1b':
width=0
height=0
num=''
continue
if char is '[':
continue
if char is ';':
height=int(num)
num=''
continue
num+=char
return (width, height)
def get_terminal_size():
# Save cursor position
sys.stdout.write('\x1b[s')
#Move cursor outside of screen
sys.stdout.write('\x1b[999B\x1b[999C')
(w, h) = get_cursor_position()
# Restore cursor position
sys.stdout.write('\x1b[u')
return (w, h)