More work on native streams

This commit is contained in:
Kovid Goyal
2016-11-13 10:21:54 +05:30
parent 62fc6cc4a0
commit fab2213c25
20 changed files with 652 additions and 161 deletions

View File

@@ -9,50 +9,30 @@
#include <structmember.h>
#define INIT_NONE(x) Py_INCREF(Py_None); x = Py_None;
static PyObject *
new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
Cursor *self;
self = (Cursor *)type->tp_alloc(type, 0);
if (self != NULL) {
self->x = PyLong_FromLong(0);
if (self->x == NULL) { Py_CLEAR(self); return NULL; }
self->y = self->x; Py_INCREF(self->y);
INIT_NONE(self->shape);
INIT_NONE(self->blink);
INIT_NONE(self->color);
self->hidden = Py_False; Py_INCREF(Py_False);
self->bold = 0; self->italic = 0; self->reverse = 0; self->strikethrough = 0; self->decoration = 0;
self->fg = 0; self->bg = 0; self->decoration_fg = 0;
}
return (PyObject*) self;
}
static void
dealloc(Cursor* self) {
Py_CLEAR(self->shape);
Py_CLEAR(self->blink);
Py_CLEAR(self->color);
Py_CLEAR(self->hidden);
Py_CLEAR(self->x);
Py_CLEAR(self->y);
Py_TYPE(self)->tp_free((PyObject*)self);
}
#define EQ(x) (a->x == b->x)
#define PEQ(x) (PyObject_RichCompareBool(a->x, b->x, Py_EQ) == 1)
static int __eq__(Cursor *a, Cursor *b) {
return EQ(bold) && EQ(italic) && EQ(strikethrough) && EQ(reverse) && EQ(decoration) && EQ(fg) && EQ(bg) && EQ(decoration_fg) && PEQ(x) && PEQ(y) && PEQ(shape) && PEQ(blink) && PEQ(color) && PEQ(hidden);
return EQ(bold) && EQ(italic) && EQ(strikethrough) && EQ(reverse) && EQ(decoration) && EQ(fg) && EQ(bg) && EQ(decoration_fg) && EQ(x) && EQ(y) && EQ(shape) && EQ(blink) && EQ(color) && EQ(hidden);
}
#define BOOL(x) ((x) ? Py_True : Py_False)
static PyObject *
repr(Cursor *self) {
return PyUnicode_FromFormat(
"Cursor(x=%R, y=%R, shape=%R, blink=%R, hidden=%R, color=%R, fg=#%08x, bg=#%08x, bold=%R, italic=%R, reverse=%R, strikethrough=%R, decoration=%d, decoration_fg=#%08x)",
self->x, self->y, self->shape, self->blink, self->hidden, self->color, self->fg, self->bg, BOOL(self->bold), BOOL(self->italic), BOOL(self->reverse), BOOL(self->strikethrough), self->decoration, self->decoration_fg
"Cursor(x=%u, y=%u, shape=%d, blink=%R, hidden=%R, color=#%08x, fg=#%08x, bg=#%08x, bold=%R, italic=%R, reverse=%R, strikethrough=%R, decoration=%d, decoration_fg=#%08x)",
self->x, self->y, self->shape, BOOL(self->blink), BOOL(self->hidden), self->color, self->fg, self->bg, BOOL(self->bold), BOOL(self->italic), BOOL(self->reverse), BOOL(self->strikethrough), self->decoration, self->decoration_fg
);
}
@@ -64,29 +44,39 @@ reset_display_attrs(Cursor *self) {
Py_RETURN_NONE;
}
void cursor_reset(Cursor *self) {
self->x = 0; self->y = 0;
self->shape = 0; self->blink = false;
self->color = 0; self->hidden = false;
}
static PyObject*
copy(Cursor *self, PyObject *args);
copy(Cursor *self);
#define copy_doc "Create a clone of this cursor"
static PyObject* color_get(Cursor *self, void UNUSED *closure) {
if (!(self->color & 0xFF)) { Py_RETURN_NONE; }
return Py_BuildValue("BBB", (self->color >> 24) & 0xFF, (self->color >> 16) & 0xFF, (self->color >> 8) & 0xFF);
}
// Boilerplate {{{
BOOL_GETSET(Cursor, bold)
BOOL_GETSET(Cursor, italic)
BOOL_GETSET(Cursor, reverse)
BOOL_GETSET(Cursor, strikethrough)
BOOL_GETSET(Cursor, hidden)
BOOL_GETSET(Cursor, blink)
static PyMemberDef members[] = {
{"x", T_OBJECT_EX, offsetof(Cursor, x), 0, "x"},
{"y", T_OBJECT_EX, offsetof(Cursor, y), 0, "y"},
{"shape", T_OBJECT_EX, offsetof(Cursor, shape), 0, "shape"},
{"blink", T_OBJECT_EX, offsetof(Cursor, blink), 0, "blink"},
{"color", T_OBJECT_EX, offsetof(Cursor, color), 0, "color"},
{"hidden", T_OBJECT_EX, offsetof(Cursor, hidden), 0, "hidden"},
{"x", T_INT, offsetof(Cursor, x), 0, "x"},
{"y", T_INT, offsetof(Cursor, y), 0, "y"},
{"shape", T_UBYTE, offsetof(Cursor, shape), 0, "shape"},
{"color", T_ULONG, offsetof(Cursor, color), 0, "color"},
{"decoration", T_UBYTE, offsetof(Cursor, decoration), 0, "decoration"},
{"fg", T_UINT, offsetof(Cursor, fg), 0, "fg"},
{"bg", T_UINT, offsetof(Cursor, bg), 0, "bg"},
{"decoration_fg", T_UINT, offsetof(Cursor, decoration_fg), 0, "decoration_fg"},
{"fg", T_ULONG, offsetof(Cursor, fg), 0, "fg"},
{"bg", T_ULONG, offsetof(Cursor, bg), 0, "bg"},
{"decoration_fg", T_ULONG, offsetof(Cursor, decoration_fg), 0, "decoration_fg"},
{NULL} /* Sentinel */
};
@@ -95,6 +85,9 @@ static PyGetSetDef getseters[] = {
GETSET(italic)
GETSET(reverse)
GETSET(strikethrough)
GETSET(hidden)
GETSET(blink)
{"color", (getter) color_get, NULL, "color", NULL},
{NULL} /* Sentinel */
};
@@ -127,16 +120,20 @@ RICHCMP(Cursor)
// }}}
static PyObject*
copy(Cursor *self, PyObject UNUSED *args) {
#define CPY(x) ans->x = self->x; Py_XINCREF(self->x);
Cursor*
cursor_copy(Cursor *self) {
#define CCY(x) ans->x = self->x;
Cursor* ans;
ans = alloc_cursor();
if (ans == NULL) { PyErr_NoMemory(); return NULL; }
CPY(x); CPY(y); CPY(shape); CPY(blink); CPY(color); CPY(hidden);
CCY(x); CCY(y); CCY(shape); CCY(blink); CCY(color); CCY(hidden);
CCY(bold); CCY(italic); CCY(strikethrough); CCY(reverse); CCY(decoration); CCY(fg); CCY(bg); CCY(decoration_fg);
return (PyObject*)ans;
return ans;
}
static PyObject*
copy(Cursor *self) {
return (PyObject*)cursor_copy(self);
}
Cursor *alloc_cursor() {