From 730fa707b0b1b283134739043446964cb0bcd328 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 1 Nov 2016 22:29:47 +0530 Subject: [PATCH] Implement cursor_from() --- kitty/data-types.h | 7 +++++++ kitty/line.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/kitty/data-types.h b/kitty/data-types.h index b3df118c1..f9055c352 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -43,6 +43,13 @@ typedef unsigned int index_type; (w | (((c->decoration & 3) << DECORATION_SHIFT) | ((c->bold & 1) << BOLD_SHIFT) | \ ((c->italic & 1) << ITALIC_SHIFT) | ((c->reverse & 1) << REVERSE_SHIFT) | ((c->strikethrough & 1) << STRIKE_SHIFT))) << ATTRS_SHIFT +#define ATTRS_TO_CURSOR(a, c) \ + c->decoration = a & DECORATION_MASK; c->bold = a & BOLD_MASK; c->italic = a & ITALIC_MASK; \ + c->reverse = a & REVERSE_MASK; c->strikethrough = a & STRIKE_MASK; + +#define COLORS_TO_CURSOR(col, c) \ + c->fg = col & COL_MASK; c->bg = (col >> COL_SHIFT) + #define METHOD(name, arg_type) {#name, (PyCFunction)name, arg_type, name##_doc}, typedef struct { diff --git a/kitty/line.c b/kitty/line.c index eccacb1f4..3f50dc039 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -113,7 +113,7 @@ set_text(Line* self, PyObject *args) { return NULL; } x = PyLong_AsUnsignedLong(cursor->x); - attrs = CURSOR_TO_ATTRS(cursor, 1) + attrs = CURSOR_TO_ATTRS(cursor, 1); color_type col = (cursor->fg & COL_MASK) | ((color_type)(cursor->bg & COL_MASK) << COL_SHIFT); decoration_type dfg = cursor->decoration_fg & COL_MASK; @@ -127,11 +127,40 @@ set_text(Line* self, PyObject *args) { Py_RETURN_NONE; } +static PyObject* +cursor_from(Line* self, PyObject *args) { +#define cursor_from_doc "cursor_from(x, y=0) -> Create a cursor object based on the formatting attributes at the specified x position. The y value of the cursor is set as specified." + unsigned long x, y = 0; + PyObject *xo, *yo; + Cursor* ans; + if (!PyArg_ParseTuple(args, "k|k", &x, &y)) return NULL; + if (x >= self->xnum) { + PyErr_SetString(PyExc_ValueError, "Out of bounds x"); + return NULL; + } + ans = PyObject_New(Cursor, &Cursor_Type); + if (ans == NULL) { PyErr_NoMemory(); return NULL; } + xo = PyLong_FromUnsignedLong(x); yo = PyLong_FromUnsignedLong(y); + if (xo == NULL || yo == NULL) { + Py_DECREF(ans); Py_XDECREF(xo); Py_XDECREF(yo); + PyErr_NoMemory(); return NULL; + } + Py_XDECREF(ans->x); Py_XDECREF(ans->y); + ans->x = xo; ans->y = yo; + char_type attrs = self->chars[x] >> ATTRS_SHIFT; + ATTRS_TO_CURSOR(attrs, ans); + COLORS_TO_CURSOR(self->colors[x], ans); + ans->decoration_fg = self->decoration_fg[x] & COL_MASK; + + return (PyObject*)ans; +} + // Boilerplate {{{ static PyMethodDef methods[] = { METHOD(text_at, METH_O) METHOD(add_combining_char, METH_VARARGS) METHOD(set_text, METH_VARARGS) + METHOD(cursor_from, METH_VARARGS) {NULL} /* Sentinel */ };