Implement a Line class
This commit is contained in:
parent
1566407c3d
commit
f3944e6289
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
extern PyTypeObject LineBuf_Type;
|
extern PyTypeObject LineBuf_Type;
|
||||||
extern PyTypeObject Cursor_Type;
|
extern PyTypeObject Cursor_Type;
|
||||||
|
extern PyTypeObject Line_Type;
|
||||||
|
|
||||||
static PyMethodDef module_methods[] = {
|
static PyMethodDef module_methods[] = {
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
@ -29,6 +30,7 @@ PyInit_fast_data_types(void) {
|
|||||||
|
|
||||||
if (PyType_Ready(&LineBuf_Type) < 0) return NULL;
|
if (PyType_Ready(&LineBuf_Type) < 0) return NULL;
|
||||||
if (PyType_Ready(&Cursor_Type) < 0) return NULL;
|
if (PyType_Ready(&Cursor_Type) < 0) return NULL;
|
||||||
|
if (PyType_Ready(&Line_Type) < 0) return NULL;
|
||||||
m = PyModule_Create(&module);
|
m = PyModule_Create(&module);
|
||||||
if (m == NULL) return NULL;
|
if (m == NULL) return NULL;
|
||||||
|
|
||||||
@ -37,6 +39,8 @@ PyInit_fast_data_types(void) {
|
|||||||
PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type);
|
PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type);
|
||||||
Py_INCREF(&Cursor_Type);
|
Py_INCREF(&Cursor_Type);
|
||||||
PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type);
|
PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type);
|
||||||
|
Py_INCREF(&Line_Type);
|
||||||
|
PyModule_AddObject(m, "Line", (PyObject *)&Line_Type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
|
|||||||
@ -37,6 +37,16 @@ typedef unsigned int index_type;
|
|||||||
#define COL_SHIFT 32
|
#define COL_SHIFT 32
|
||||||
#define HAS_BG_MASK (0xFF << COL_SHIFT)
|
#define HAS_BG_MASK (0xFF << COL_SHIFT)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PyObject_HEAD
|
||||||
|
|
||||||
|
char_type *chars;
|
||||||
|
color_type *colors;
|
||||||
|
decoration_type *decoration_fg;
|
||||||
|
combining_type *combining_chars;
|
||||||
|
index_type xnum, ynum;
|
||||||
|
} Line;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
@ -45,6 +55,7 @@ typedef struct {
|
|||||||
index_type xnum, ynum, *line_map;
|
index_type xnum, ynum, *line_map;
|
||||||
index_type block_size;
|
index_type block_size;
|
||||||
uint8_t *continued_map;
|
uint8_t *continued_map;
|
||||||
|
Line *line;
|
||||||
|
|
||||||
// Pointers into buf
|
// Pointers into buf
|
||||||
char_type *chars;
|
char_type *chars;
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "data-types.h"
|
#include "data-types.h"
|
||||||
|
extern PyTypeObject Line_Type;
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
clear_chars_to_space(LineBuf* linebuf, index_type y) {
|
clear_chars_to_space(LineBuf* linebuf, index_type y) {
|
||||||
@ -38,9 +39,10 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
|
|||||||
self->buf = PyMem_Calloc(xnum * ynum, CELL_SIZE);
|
self->buf = PyMem_Calloc(xnum * ynum, CELL_SIZE);
|
||||||
self->line_map = PyMem_Calloc(ynum, sizeof(index_type));
|
self->line_map = PyMem_Calloc(ynum, sizeof(index_type));
|
||||||
self->continued_map = PyMem_Calloc(ynum, sizeof(uint8_t));
|
self->continued_map = PyMem_Calloc(ynum, sizeof(uint8_t));
|
||||||
if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL) {
|
self->line = (Line*)PyType_GenericAlloc(&Line_Type, 0);
|
||||||
|
if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL || self->line == NULL) {
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map);
|
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); Py_XDECREF(self->line);
|
||||||
Py_DECREF(self);
|
Py_DECREF(self);
|
||||||
self = NULL;
|
self = NULL;
|
||||||
} else {
|
} else {
|
||||||
@ -48,6 +50,7 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
|
|||||||
self->colors = (color_type*)(self->chars + self->block_size);
|
self->colors = (color_type*)(self->chars + self->block_size);
|
||||||
self->decoration_fg = (decoration_type*)(self->colors + self->block_size);
|
self->decoration_fg = (decoration_type*)(self->colors + self->block_size);
|
||||||
self->combining_chars = (combining_type*)(self->decoration_fg + self->block_size);
|
self->combining_chars = (combining_type*)(self->decoration_fg + self->block_size);
|
||||||
|
self->line->xnum = xnum;
|
||||||
for(index_type i = 0; i < ynum; i++) {
|
for(index_type i = 0; i < ynum; i++) {
|
||||||
self->line_map[i] = i;
|
self->line_map[i] = i;
|
||||||
clear_chars_to_space(self, i);
|
clear_chars_to_space(self, i);
|
||||||
@ -61,44 +64,31 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
|
|||||||
static void
|
static void
|
||||||
LineBuf_dealloc(LineBuf* self) {
|
LineBuf_dealloc(LineBuf* self) {
|
||||||
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map);
|
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map);
|
||||||
|
Py_XDECREF(self->line);
|
||||||
Py_TYPE(self)->tp_free((PyObject*)self);
|
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
text_at(LineBuf* self, PyObject *args) {
|
line(LineBuf *self, PyObject *y) {
|
||||||
index_type ynum, xnum, idx;
|
unsigned long idx = PyLong_AsUnsignedLong(y);
|
||||||
char_type ch;
|
if (idx >= self->ynum) {
|
||||||
combining_type cc;
|
PyErr_SetString(PyExc_ValueError, "Line number too large");
|
||||||
PyObject * ans;
|
return NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "II", &ynum, &xnum)) return NULL;
|
|
||||||
if (ynum >= self->ynum) { PyErr_SetString(PyExc_ValueError, "Line number out of bounds"); return NULL; }
|
|
||||||
if (xnum >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Column number out of bounds"); return NULL; }
|
|
||||||
|
|
||||||
idx = xnum + ynum * self->xnum;
|
|
||||||
ch = self->chars[idx] & CHAR_MASK;
|
|
||||||
cc = self->combining_chars[idx];
|
|
||||||
if (cc == 0) {
|
|
||||||
ans = PyUnicode_New(1, ch);
|
|
||||||
if (ans == NULL) return PyErr_NoMemory();
|
|
||||||
PyUnicode_WriteChar(ans, 0, ch);
|
|
||||||
} else {
|
|
||||||
Py_UCS4 cc1 = cc & 0xFFFF, cc2 = cc >> 16;
|
|
||||||
Py_UCS4 maxc = (ch > cc1) ? MAX(ch, cc2) : MAX(cc1, cc2);
|
|
||||||
ans = PyUnicode_New(cc2 ? 3 : 2, maxc);
|
|
||||||
if (ans == NULL) return PyErr_NoMemory();
|
|
||||||
PyUnicode_WriteChar(ans, 0, ch);
|
|
||||||
PyUnicode_WriteChar(ans, 1, cc1);
|
|
||||||
if (cc2) PyUnicode_WriteChar(ans, 2, cc2);
|
|
||||||
}
|
}
|
||||||
|
self->line->ynum = self->line_map[idx];
|
||||||
return ans;
|
size_t off = self->line->ynum * self->xnum;
|
||||||
|
self->line->chars = self->chars + off;
|
||||||
|
self->line->colors = self->colors + off;
|
||||||
|
self->line->decoration_fg = self->decoration_fg + off;
|
||||||
|
self->line->combining_chars = self->combining_chars + off;
|
||||||
|
Py_INCREF(self->line);
|
||||||
|
return (PyObject*)self->line;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Boilerplate {{{
|
// Boilerplate {{{
|
||||||
static PyMethodDef LineBuf_methods[] = {
|
static PyMethodDef LineBuf_methods[] = {
|
||||||
{"text_at", (PyCFunction)text_at, METH_VARARGS,
|
{"line", (PyCFunction)line, METH_O,
|
||||||
"Return the text in the specified cell"
|
"Return the specified line as a Line object. Note the Line Object is a live view into the underlying buffer. And only a single line object can be used at a time."
|
||||||
},
|
},
|
||||||
{NULL} /* Sentinel */
|
{NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|||||||
100
kitty/line.c
Normal file
100
kitty/line.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* line.c
|
||||||
|
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
*
|
||||||
|
* Distributed under terms of the GPL3 license.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "data-types.h"
|
||||||
|
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
new(PyTypeObject UNUSED *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Line objects cannot be instantiated directly, create them using LineBuf.line()");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
dealloc(LineBuf* self) {
|
||||||
|
Py_TYPE(self)->tp_free((PyObject*)self);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
text_at(Line* self, PyObject *x) {
|
||||||
|
unsigned long xval = PyLong_AsUnsignedLong(x);
|
||||||
|
char_type ch;
|
||||||
|
combining_type cc;
|
||||||
|
PyObject *ans;
|
||||||
|
|
||||||
|
if (xval >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Column number out of bounds"); return NULL; }
|
||||||
|
|
||||||
|
ch = self->chars[xval] & CHAR_MASK;
|
||||||
|
cc = self->combining_chars[xval];
|
||||||
|
if (cc == 0) {
|
||||||
|
ans = PyUnicode_New(1, ch);
|
||||||
|
if (ans == NULL) return PyErr_NoMemory();
|
||||||
|
PyUnicode_WriteChar(ans, 0, ch);
|
||||||
|
} else {
|
||||||
|
Py_UCS4 cc1 = cc & 0xFFFF, cc2 = cc >> 16;
|
||||||
|
Py_UCS4 maxc = (ch > cc1) ? MAX(ch, cc2) : MAX(cc1, cc2);
|
||||||
|
ans = PyUnicode_New(cc2 ? 3 : 2, maxc);
|
||||||
|
if (ans == NULL) return PyErr_NoMemory();
|
||||||
|
PyUnicode_WriteChar(ans, 0, ch);
|
||||||
|
PyUnicode_WriteChar(ans, 1, cc1);
|
||||||
|
if (cc2) PyUnicode_WriteChar(ans, 2, cc2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Boilerplate {{{
|
||||||
|
static PyMethodDef methods[] = {
|
||||||
|
{"text_at", (PyCFunction)text_at, METH_O,
|
||||||
|
"Return the text in the specified cell"
|
||||||
|
},
|
||||||
|
{NULL} /* Sentinel */
|
||||||
|
};
|
||||||
|
|
||||||
|
PyTypeObject Line_Type = {
|
||||||
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
|
"fast_data_types.Line",
|
||||||
|
sizeof(Line),
|
||||||
|
0, /* tp_itemsize */
|
||||||
|
(destructor)dealloc, /* tp_dealloc */
|
||||||
|
0, /* tp_print */
|
||||||
|
0, /* tp_getattr */
|
||||||
|
0, /* tp_setattr */
|
||||||
|
0, /* tp_reserved */
|
||||||
|
0, /* tp_repr */
|
||||||
|
0, /* tp_as_number */
|
||||||
|
0, /* tp_as_sequence */
|
||||||
|
0, /* tp_as_mapping */
|
||||||
|
0, /* tp_hash */
|
||||||
|
0, /* tp_call */
|
||||||
|
0, /* tp_str */
|
||||||
|
0, /* tp_getattro */
|
||||||
|
0, /* tp_setattro */
|
||||||
|
0, /* tp_as_buffer */
|
||||||
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||||
|
"Lines", /* tp_doc */
|
||||||
|
0, /* tp_traverse */
|
||||||
|
0, /* tp_clear */
|
||||||
|
0, /* tp_richcompare */
|
||||||
|
0, /* tp_weaklistoffset */
|
||||||
|
0, /* tp_iter */
|
||||||
|
0, /* tp_iternext */
|
||||||
|
methods, /* tp_methods */
|
||||||
|
0, /* tp_members */
|
||||||
|
0, /* tp_getset */
|
||||||
|
0, /* tp_base */
|
||||||
|
0, /* tp_dict */
|
||||||
|
0, /* tp_descr_get */
|
||||||
|
0, /* tp_descr_set */
|
||||||
|
0, /* tp_dictoffset */
|
||||||
|
0, /* tp_init */
|
||||||
|
0, /* tp_alloc */
|
||||||
|
new, /* tp_new */
|
||||||
|
};
|
||||||
|
// }}
|
||||||
|
|
||||||
@ -16,12 +16,13 @@ class TestDataTypes(BaseTest):
|
|||||||
def test_line_buf(self):
|
def test_line_buf(self):
|
||||||
lb = LineBuf(2, 3)
|
lb = LineBuf(2, 3)
|
||||||
for y in range(2):
|
for y in range(2):
|
||||||
|
line = lb.line(y)
|
||||||
for x in range(3):
|
for x in range(3):
|
||||||
self.ae(lb.text_at(y, x), ' ')
|
self.ae(line.text_at(x), ' ')
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
lb.text_at(1, 5)
|
lb.line(5)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
lb.text_at(5, 1)
|
lb.line(0).text_at(5)
|
||||||
|
|
||||||
def test_line_ops(self):
|
def test_line_ops(self):
|
||||||
t = 'Testing with simple text'
|
t = 'Testing with simple text'
|
||||||
|
|||||||
2
setup.py
2
setup.py
@ -68,4 +68,4 @@ if __name__ == '__main__':
|
|||||||
if sys.version_info < (3, 5):
|
if sys.version_info < (3, 5):
|
||||||
raise SystemExit('python >= 3.5 required')
|
raise SystemExit('python >= 3.5 required')
|
||||||
init_env()
|
init_env()
|
||||||
compile_c_extension('kitty/fast_data_types', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c')
|
compile_c_extension('kitty/fast_data_types', 'kitty/line.c', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user