summaryrefslogtreecommitdiffstats
path: root/tests/modules/lib/vterm.py
blob: 1984e1b9128842072b08fd9ca9b340671183fb34 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import ctypes

from collections import namedtuple

from powerline.lib.unicode import unicode, unichr, tointiter


Dimensions = namedtuple('Dimensions', ('rows', 'cols'))


class CTypesFunction(object):
	def __init__(self, library, name, rettype, args):
		self.name = name
		self.prototype = ctypes.CFUNCTYPE(rettype, *[
			arg[1] for arg in args
		])
		self.args = args
		self.func = self.prototype((name, library), tuple((
			(1, arg[0]) for arg in args
		)))

	def __call__(self, *args, **kwargs):
		return self.func(*args, **kwargs)

	def __repr__(self):
		return '{cls}(<library>, {name!r}, {rettype!r}, {args!r})'.format(
			cls=self.__class__.__name__,
			**self.__dict__
		)


class CTypesLibraryFuncsCollection(object):
	def __init__(self, lib, **kwargs):
		self.lib = lib
		library_loader = ctypes.LibraryLoader(ctypes.CDLL)
		library = library_loader.LoadLibrary(lib)
		self.library = library
		for name, args in kwargs.items():
			self.__dict__[name] = CTypesFunction(library, name, *args)


class VTermPos_s(ctypes.Structure):
	_fields_ = (
		('row', ctypes.c_int),
		('col', ctypes.c_int),
	)


class VTermColor_s(ctypes.Structure):
	_fields_ = (
		('red', ctypes.c_uint8),
		('green', ctypes.c_uint8),
		('blue', ctypes.c_uint8),
	)


class VTermScreenCellAttrs_s(ctypes.Structure):
	_fields_ = (
		('bold', ctypes.c_uint, 1),
		('underline', ctypes.c_uint, 2),
		('italic', ctypes.c_uint, 1),
		('blink', ctypes.c_uint, 1),
		('reverse', ctypes.c_uint, 1),
		('strike', ctypes.c_uint, 1),
		('font', ctypes.c_uint, 4),
		('dwl', ctypes.c_uint, 1),
		('dhl', ctypes.c_uint, 2),
	)


VTERM_MAX_CHARS_PER_CELL = 6


class VTermScreenCell_s(ctypes.Structure):
	_fields_ = (
		('chars', ctypes.ARRAY(ctypes.c_uint32, VTERM_MAX_CHARS_PER_CELL)),
		('width', ctypes.c_char),
		('attrs', VTermScreenCellAttrs_s),
		('fg', VTermColor_s),
		('bg', VTermColor_s),
	)


VTerm_p = ctypes.c_void_p
VTermScreen_p = ctypes.c_void_p


def get_functions(lib):
	return CTypesLibraryFuncsCollection(
		lib,
		vterm_new=(VTerm_p, (
			('rows', ctypes.c_int),
			('cols', ctypes.c_int)
		)),
		vterm_obtain_screen=(VTermScreen_p, (('vt', VTerm_p),)),
		vterm_set_size=(None, (
			('vt', VTerm_p),
			('rows', ctypes.c_int),
			('cols', ctypes.c_int)
		)),
		vterm_screen_reset=(None, (
			('screen', VTermScreen_p),
			('hard', ctypes.c_int)
		)),
		vterm_input_write=(ctypes.c_size_t, (
			('vt', VTerm_p),
			('bytes', ctypes.POINTER(ctypes.c_char)),
			('size', ctypes.c_size_t),
		)),
		vterm_screen_get_cell=(ctypes.c_int, (
			('screen', VTermScreen_p),
			('pos', VTermPos_s),
			('cell', ctypes.POINTER(VTermScreenCell_s))
		)),
		vterm_free=(None, (('vt', VTerm_p),)),
		vterm_set_utf8=(None, (('vt', VTerm_p), ('is_utf8', ctypes.c_int))),
	)


class VTermColor(object):
	__slots__ = ('red', 'green', 'blue')

	def __init__(self, color):
		self.red = color.red
		self.green = color.green
		self.blue = color.blue

	@property
	def color_key(self):
		return (self.red, self.green, self.blue)


class VTermScreenCell(object):
	def __init__(self, vtsc):
		for field in VTermScreenCellAttrs_s._fields_:
			field_name = field[0]
			setattr(self, field_name, getattr(vtsc.attrs, field_name))
		self.text = ''.join((
			unichr(vtsc.chars[i]) for i in range(VTERM_MAX_CHARS_PER_CELL)
		)).rstrip('\x00')
		self.width = next(tointiter(vtsc.width))
		self.fg = VTermColor(vtsc.fg)
		self.bg = VTermColor(vtsc.bg)
		self.cell_properties_key = (
			self.fg.color_key,
			self.bg.color_key,
			self.bold,
			self.underline,
			self.italic,
		)


class VTermScreen(object):
	def __init__(self, functions, screen):
		self.functions = functions
		self.screen = screen

	def __getitem__(self, position):
		pos = VTermPos_s(*position)
		cell = VTermScreenCell_s()
		ret = self.functions.vterm_screen_get_cell(self.screen, pos, cell)
		if ret != 1:
			raise ValueError('vterm_screen_get_cell returned {0}'.format(ret))
		return VTermScreenCell(cell)

	def reset(self, hard):
		self.functions.vterm_screen_reset(self.screen, int(bool(hard)))


class VTerm(object):
	def __init__(self, lib, dim):
		self.functions = get_functions(lib)
		self.vt = self.functions.vterm_new(dim.rows, dim.cols)
		self.functions.vterm_set_utf8(self.vt, 1)
		self.vtscreen = VTermScreen(self.functions, self.functions.vterm_obtain_screen(self.vt))
		self.vtscreen.reset(True)

	def push(self, data):
		if isinstance(data, unicode):
			data = data.encode('utf-8')
		return self.functions.vterm_input_write(self.vt, data, len(data))

	def resize(self, dim):
		self.functions.vterm_set_size(self.vt, dim.rows, dim.cols)

	def __del__(self):
		try:
			self.functions.vterm_free(self.vt)
		except AttributeError:
			pass