From 0797f159adfbf5fd8c645d4b598c0278aba85049 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 12 Dec 2016 12:39:34 +0530 Subject: [PATCH] Only move the cursor a line down on resize if the cursor line was split --- kitty/data-types.h | 1 + kitty/line-buf.c | 3 ++- kitty/line.c | 11 ++++++++++- kitty/screen.c | 13 ++++++++++++- kitty/window.py | 4 ---- kitty_tests/screen.py | 8 ++++---- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/kitty/data-types.h b/kitty/data-types.h index 5938e3513..a87388734 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -331,6 +331,7 @@ void line_set_char(Line *, unsigned int , uint32_t , unsigned int , Cursor *); void line_right_shift(Line *, unsigned int , unsigned int ); void line_add_combining_char(Line *, uint32_t , unsigned int ); index_type line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen); +unsigned int line_length(Line *self); void linebuf_init_line(LineBuf *, index_type); void linebuf_clear(LineBuf *, char_type ch); diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 478c893a5..61876485c 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -432,7 +432,8 @@ copy_old(LineBuf *self, PyObject *y) { #include "rewrap.h" -void linebuf_rewrap(LineBuf *self, LineBuf *other, int *cursor_y_out, HistoryBuf *historybuf) { +void +linebuf_rewrap(LineBuf *self, LineBuf *other, int *cursor_y_out, HistoryBuf *historybuf) { index_type first, i; bool is_empty = true; diff --git a/kitty/line.c b/kitty/line.c index d082a5abf..eda5d271d 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -24,7 +24,16 @@ dealloc(Line* self) { Py_TYPE(self)->tp_free((PyObject*)self); } -PyObject* line_text_at(char_type ch, combining_type cc) { +unsigned int +line_length(Line *self) { + for (int i = self->xnum - 1; i >= 0; i++) { + if ((self->chars[i] & CHAR_MASK) != 32) return i + 1; + } + return 0; +} + +PyObject* +line_text_at(char_type ch, combining_type cc) { PyObject *ans; if (cc == 0) { ans = PyUnicode_New(1, ch); diff --git a/kitty/screen.c b/kitty/screen.c index 06a4e9949..1a4cd0c11 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -108,7 +108,17 @@ screen_resize(Screen *self, unsigned int lines, unsigned int columns) { LineBuf *n = realloc_lb(self->main_linebuf, lines, columns, &cursor_y, self->historybuf); if (n == NULL) return false; Py_CLEAR(self->main_linebuf); self->main_linebuf = n; - if (is_main) self->cursor->y = MAX(0, cursor_y); + bool index_after_resize = false; + if (is_main) { + linebuf_init_line(self->main_linebuf, self->cursor->y); + if (is_x_shrink && (self->main_linebuf->continued_map[self->cursor->y] || line_length(self->main_linebuf->line) > columns)) { + // If the client is in line drawing mode, it will redraw the cursor + // line, this can cause rendering artifacts, so ensure that the + // cursor is on a new line + index_after_resize = true; + } + self->cursor->y = MAX(0, cursor_y); + } cursor_y = -1; n = realloc_lb(self->alt_linebuf, lines, columns, &cursor_y, NULL); if (n == NULL) return false; @@ -129,6 +139,7 @@ screen_resize(Screen *self, unsigned int lines, unsigned int columns) { init_tabstops(self->main_tabstops, self->columns); init_tabstops(self->alt_tabstops, self->columns); tracker_update_screen(self->change_tracker); + if (index_after_resize) screen_index(self); return true; } diff --git a/kitty/window.py b/kitty/window.py index 4a11fa6dd..8ca134678 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -65,10 +65,6 @@ class Window: def set_geometry(self, new_geometry): if self.needs_layout or new_geometry.xnum != self.screen.columns or new_geometry.ynum != self.screen.lines: self.screen.resize(new_geometry.ynum, new_geometry.xnum) - if self.screen.cursor.y > 0: - # We move the cursor once line down to prevent the shell from - # overwriting the contents of the last line - self.screen.index() self.child.resize_pty(self.screen.columns, self.screen.lines) self.char_grid.resize(new_geometry) self.needs_layout = False diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index cb1784250..495cee3b9 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -243,14 +243,14 @@ class TestScreen(BaseTest): s.resize(5, 1) self.ae(str(s.line(0)), '4') hb = s.historybuf - for i in range(s.lines): - self.ae(str(hb.line(i)), '3') - self.ae(str(hb.line(5)), '2') + for i in range(hb.ynum): + self.ae(str(hb.line(i)), '4' if i == 0 else '3') s = self.create_screen(scrollback=6) s.draw(''.join([str(i) * s.columns for i in range(s.lines*2)])) self.ae(str(s.line(4)), '9'*5) s.resize(5, 2) - self.ae(str(s.line(4)), '9 ') + self.ae(str(s.line(3)), '9 ') + self.ae(str(s.line(4)), ' ') def test_tab_stops(self): # Taken from vttest/main.c