-
Notifications
You must be signed in to change notification settings - Fork 2
/
termio.py
149 lines (110 loc) · 2.84 KB
/
termio.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
from collections import namedtuple
from ctypes import *
T = cdll.LoadLibrary(os.path.dirname(os.path.realpath(__file__)) + "/libtermio.so")
class Size:
def __init__(self, w, h):
assert type(w) == int and type(h) == int
self.w = w
self.h = h
class Style:
def __init__(self, fg, bg, bold, ul):
assert type(fg) == int and type(bg) == int and \
type(bold) == bool and type(ul) == bool
self.fg = fg
self.bg = bg
self.bold = bold
self.ul = ul
def initkeyboard(nosigkeys):
T.initkeyboard(c_bool(nosigkeys))
def endkeyboard():
T.endkeyboard()
def initscreen():
T.initscreen()
def endscreen():
T.endscreen()
def installCLhandler(install):
T.installCLhandler(c_bool(install))
# TODO: void installredrawhandler(void (*handler)(bool));
def clearscreen():
T.clearscreen()
class _TSize(Structure):
_fields_ = [("w", c_int), ("h", c_int)]
T.gettermsize.restype = _TSize
def gettermsize():
ts = T.gettermsize()
return Size(ts.w, ts.h)
def setstyle(style):
class TStyle(Structure):
_fields_ = [("fg", c_int), ("bg", c_int), ("bold", c_bool), ("ul", c_bool)]
assert type(style) == Style
ts = TStyle(style.fg, style.bg, style.bold, style.ul)
T.setstyle(byref(ts))
def setfg(fg):
T.setfg(c_int(fg))
def setbg(bg):
T.setbg(c_int(bg))
def setbold(bold):
T.setbold(c_bool(bold))
def setul(ul):
T.setul(c_bool(ul))
def tputc(c):
T.tputc(c_int(c))
T.tprintf.restype = c_int
def tprint(s):
if type(s) == str:
s = bytes(s, "utf-8")
else:
assert type(s) == bytes
return T.tprintf(b"%s", s)
def fillrect(x, y, w, h, c):
T.fillrect(c_int(x), c_int(y), c_int(w), c_int(h), c_int(ord(c)))
def redraw():
T.redraw()
def redrawfull():
T.redrawfull()
def scrollterm(x, y, w, h, amount):
T.scrollterm(c_int(x), c_int(y), c_int(w), c_int(h), c_int(amount))
T.getbufferchar.restype = c_int
def _getbufferchar(x, y):
return T.getbufferchar(c_int(x), c_int(y))
def getbufferchar(x, y):
return chr(_getbufferchar(x, y))
def moveto(x, y):
T.moveto(c_int(x), c_int(y))
def pushcursor():
T.pushcursor()
def popcursor():
T.popcursor()
def bel():
T.bel()
def cursorvisible(visible):
T.cursorvisible(c_bool(visible))
T.tgetkey.restype = c_int
def tgetkey():
return T.tgetkey()
T.tgetline.restype = c_char_p
def tgetline():
byt = T.tgetline()
if byt is None:
return None
else:
return byt.decode("utf-8")
KEY_TAB = 9
KEY_LF = 10
KEY_CR = 13
KEY_ESC = 27
KEY_BACKSPACE = 127
KEY_RIGHT = 1001
KEY_UP = 1002
KEY_LEFT = 1003
KEY_DOWN = 1004
KEY_PAGEUP = 1021
KEY_PAGEDOWN = 1022
KEY_DELETE = 1100
KEY_SHIFTTAB = 1200
KEY_CTRL = -64
KEY_SHIFT = 1000000
KEY_ALT = 120000
KEY_CTRLALT = KEY_CTRL + KEY_ALT
KEY_CTRLSHIFT = KEY_CTRL + KEY_SHIFT