From cf5f6e97e8e037e0a5647735e262cc00ee1af022 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 29 Dec 2024 17:22:06 +0530 Subject: [PATCH] Add some basic tests for multicell rewrap --- kitty/rewrap.c | 56 +++++++++++++++++++++++----------------- kitty_tests/multicell.py | 34 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/kitty/rewrap.c b/kitty/rewrap.c index a975b7ead..dc3d334a9 100644 --- a/kitty/rewrap.c +++ b/kitty/rewrap.c @@ -155,30 +155,6 @@ update_tracked_cursors(Rewrap *r, index_type num_cells, index_type y, index_type } } -static void -fast_copy_src_to_dest(Rewrap *r) { - CPUCell *c; index_type mc_width; - while (r->src_x < r->src_x_limit) { - if (r->dest_x >= r->dest_xnum) next_dest_line(r, true); - index_type num = MIN(r->src_x_limit - r->src_x, r->dest_xnum - r->dest_x); - bool do_copy = true; - if (num && (c = &r->src.cpu_cells[r->src_x + num - 1])->is_multicell && c->x != (mc_width = mcd_x_limit(c)) - 1) { - // we have a split multicell at the right edge of the copy region - if (num > mc_width) num -= mc_width; - else { - if (mc_width > r->dest_xnum) do_copy = false; - else { - r->dest_x = r->dest_xnum; - continue; - } - } - } - if (do_copy) copy_range(&r->src, r->src_x, &r->dest, r->dest_x, num); - update_tracked_cursors(r, num, r->src_y, r->src_x_limit); - r->src_x += num; r->dest_x += num; - } -} - static bool find_space_in_dest_line(Rewrap *r, index_type num_cells) { while (r->dest_x + num_cells <= r->dest_xnum) { @@ -210,6 +186,7 @@ copy_multiline_extra_lines(Rewrap *r, CPUCell *src_cell, index_type mc_width) { } } + static void multiline_copy_src_to_dest(Rewrap *r) { CPUCell *c; index_type mc_width; @@ -231,6 +208,37 @@ multiline_copy_src_to_dest(Rewrap *r) { } } + +static void +fast_copy_src_to_dest(Rewrap *r) { + CPUCell *c; index_type mc_width; + while (r->src_x < r->src_x_limit) { + if (r->dest_x >= r->dest_xnum) { + next_dest_line(r, true); + if (r->current_dest_line_has_multiline_cells) { + multiline_copy_src_to_dest(r); + return; + } + } + index_type num = MIN(r->src_x_limit - r->src_x, r->dest_xnum - r->dest_x); + bool do_copy = true; + if (num && (c = &r->src.cpu_cells[r->src_x + num - 1])->is_multicell && c->x != (mc_width = mcd_x_limit(c)) - 1) { + // we have a split multicell at the right edge of the copy region + if (num > mc_width) num = MIN(r->src_x_limit - r->src_x - mc_width, num); + else { + if (mc_width > r->dest_xnum) do_copy = false; + else { + r->dest_x = r->dest_xnum; + continue; + } + } + } + if (do_copy) copy_range(&r->src, r->src_x, &r->dest, r->dest_x, num); + update_tracked_cursors(r, num, r->src_y, r->src_x_limit); + r->src_x += num; r->dest_x += num; + } +} + static index_type rewrap_inner(Rewrap *r) { setup_line(r->text_cache, r->src_xnum, &r->src); setup_line(r->text_cache, r->dest_xnum, &r->dest); diff --git a/kitty_tests/multicell.py b/kitty_tests/multicell.py index 58200b22c..d7da303b7 100644 --- a/kitty_tests/multicell.py +++ b/kitty_tests/multicell.py @@ -459,3 +459,37 @@ def test_multicell(self: TestMulticell) -> None: multicell(s, 'a', scale=3) multicell(s, 'b', scale=2) ta('\x1b]66;w=1:s=3;a\x07\x1b]66;w=1:s=2;b\x07') + + # rewrap with multicells + o = s.lines, s.columns + def reset(): + s.resize(*o) + s.reset() + + reset() + multicell(s, 'a', scale=2) + before = as_ansi() + s.resize(s.lines + 1, s.columns) + self.ae(before.rstrip(), as_ansi().rstrip()) + + reset() + s.draw('a' * (s.columns - 2) + '😛' + 'bb') + s.resize(s.lines, s.columns-1) + self.ae('\x1b[maaaa\x1b[m😛bb', as_ansi().rstrip()) + reset() + s.draw('a' * (s.columns - 2) + '😛' + 'bb') + s.resize(s.lines, s.columns-2) + self.ae('\x1b[maaaa\x1b[m😛bb', as_ansi().rstrip()) + reset() + s.draw('a' * (s.columns - 2) + '😛' + 'bb') + s.resize(s.lines, s.columns-3) + self.ae('\x1b[maaa\x1b[ma😛\x1b[mbb', as_ansi().rstrip()) # ]]]]]]] + + reset() + multicell(s, 'a', scale=3) + s.draw('b'*(s.columns-3)) + s.resize(s.lines, s.columns-1) + self.ae('\x1b[m\x1b]66;w=1:s=3;a\x07bb\x1b[mb', as_ansi().rstrip()) # ]] + ac(0, 0, is_multicell=True) + ac(0, 1, is_multicell=True) + ac(3, 1, is_multicell=False, text='b')