Skip to content

Commit

Permalink
Add some basic tests for multicell rewrap
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Dec 29, 2024
1 parent 695634a commit 57fd037
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
56 changes: 32 additions & 24 deletions kitty/rewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions kitty_tests/multicell.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,37 @@ def ta(expected):
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')

0 comments on commit 57fd037

Please sign in to comment.