From b8c34c3ee25e6ee4d721e80db6f89a9320e5937b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Sep 2017 13:20:15 +0530 Subject: [PATCH] Move implementation of screen_line() into C --- kitty/char_grid.py | 8 +------- kitty/screen.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/kitty/char_grid.py b/kitty/char_grid.py index 06139224c..b500b0515 100644 --- a/kitty/char_grid.py +++ b/kitty/char_grid.py @@ -301,13 +301,7 @@ class CharGrid: def screen_line(self, y): ' Return the Line object corresponding to the yth line on the rendered screen ' - if y >= 0 and y < self.screen.lines: - if self.scrolled_by: - if y < self.scrolled_by: - return self.screen.historybuf.line(self.scrolled_by - 1 - y) - return self.screen.line(y - self.scrolled_by) - else: - return self.screen.line(y) + return self.screen.visual_line(y, self.scrolled_by) def multi_click(self, count, x, y): x, y = self.cell_for_pos(x, y) diff --git a/kitty/screen.c b/kitty/screen.c index 0fa922507..37abe4261 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1078,6 +1078,25 @@ line(Screen *self, PyObject *val) { return (PyObject*) self->linebuf->line; } +static PyObject* +visual_line(Screen *self, PyObject *args) { + // The line corresponding to the yth visual line, taking into account scrolling + unsigned int y, scrolled_by; + if (!PyArg_ParseTuple(args, "II", &y, &scrolled_by)) return NULL; + if (y >= self->lines) { Py_RETURN_NONE; } + if (scrolled_by) { + if (y < scrolled_by) { + historybuf_init_line(self->historybuf, scrolled_by - 1 - y, self->historybuf->line); + Py_INCREF(self->historybuf->line); + return (PyObject*) self->historybuf->line; + } + y -= scrolled_by; + } + linebuf_init_line(self->linebuf, y); + Py_INCREF(self->linebuf->line); + return (PyObject*) self->linebuf->line; +} + static PyObject* draw(Screen *self, PyObject *src) { if (!PyUnicode_Check(src)) { PyErr_SetString(PyExc_TypeError, "A unicode string is required"); return NULL; } @@ -1271,6 +1290,7 @@ COUNT_WRAP(cursor_forward) static PyMethodDef methods[] = { MND(line, METH_O) + MND(visual_line, METH_VARARGS) MND(draw, METH_O) MND(cursor_position, METH_VARARGS) MND(set_mode, METH_VARARGS)