From 79c3add8dc55db34a78297e37eaa9ee8e16c614c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 28 Dec 2024 09:43:32 +0530 Subject: [PATCH] Decouple some API in historybuf so that we can access more than just the next line eventually --- kitty/history.c | 10 ++++++---- kitty/rewrap.c | 24 +++++++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index f5b93f4d03a..dbbcbea5f69 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -279,9 +279,8 @@ pagerhist_push(HistoryBuf *self, ANSIBuf *as_ansi_buf) { } static index_type -historybuf_push(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *line) { +historybuf_push(HistoryBuf *self, ANSIBuf *as_ansi_buf) { index_type idx = (self->start_of_data + self->count) % self->ynum; - init_line(self, idx, line); if (self->count == self->ynum) { pagerhist_push(self, as_ansi_buf); self->start_of_data = (self->start_of_data + 1) % self->ynum; @@ -291,7 +290,8 @@ historybuf_push(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *line) { void historybuf_add_line(HistoryBuf *self, const Line *line, ANSIBuf *as_ansi_buf) { - index_type idx = historybuf_push(self, as_ansi_buf, self->line); + index_type idx = historybuf_push(self, as_ansi_buf); + init_line(self, idx, self->line); copy_line(line, self->line); *attrptr(self, idx) = line->attrs; } @@ -611,7 +611,9 @@ HistoryBuf *alloc_historybuf(unsigned int lines, unsigned int columns, unsigned index_type historybuf_next_dest_line(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *src_line, index_type dest_y, Line *dest_line, bool continued) { history_buf_set_last_char_as_continuation(self, dest_y, continued); - *attrptr(self, historybuf_push(self, as_ansi_buf, dest_line)) = src_line->attrs; + index_type idx = historybuf_push(self, as_ansi_buf); + *attrptr(self, idx) = src_line->attrs; + init_line(self, idx, dest_line); return dest_y + 1; } diff --git a/kitty/rewrap.c b/kitty/rewrap.c index b9107f17a1b..edd87f766f9 100644 --- a/kitty/rewrap.c +++ b/kitty/rewrap.c @@ -96,7 +96,17 @@ static void init_line(TextCache *tc, index_type xnum, Line *l) { l->text_cache = tc; l->xnum = xnum; +} +static void +next_dest_line(Rewrap *r, bool continued) { + r->dest_y = r->next_dest_line(r->dest_buf, r->historybuf, r->as_ansi_buf, &r->src, r->dest_y, &r->dest, continued); + r->dest_x = 0; +} + +static void +first_dest_line(Rewrap *r) { + r->dest_y = r->first_dest_line(r->dest_buf, r->as_ansi_buf, &r->src, &r->dest); } static index_type @@ -110,13 +120,8 @@ rewrap_inner(Rewrap r) { r.init_line(r.src_buf, r.src_y, &r.src); r.src_x_limit = r.src.xnum; if (!src_line_is_continued) { - r.dest_x = 0; - if (is_first_line) { - is_first_line = false; - r.dest_y = r.first_dest_line(r.dest_buf, r.as_ansi_buf, &r.src, &r.dest); - } else { - r.dest_y = r.next_dest_line(r.dest_buf, r.historybuf, r.as_ansi_buf, &r.src, r.dest_y, &r.dest, false); - } + if (is_first_line) { is_first_line = false; first_dest_line(&r); } + else next_dest_line(&r, false); } src_line_is_continued = r.src.cpu_cells[r.src.xnum-1].next_char_was_wrapped; if (!src_line_is_continued) { @@ -130,10 +135,7 @@ rewrap_inner(Rewrap r) { if (t->is_tracked_line && t->x >= r.src_x_limit) t->x = MAX(1u, r.src_x_limit) - 1; } while (r.src_x < r.src_x_limit) { - if (r.dest_x >= r.dest.xnum) { - r.dest_x = 0; - r.dest_y = r.next_dest_line(r.dest_buf, r.historybuf, r.as_ansi_buf, &r.src, r.dest_y, &r.dest, true); - } + if (r.dest_x >= r.dest.xnum) next_dest_line(&r, true); index_type num = MIN(r.src.xnum - r.src_x, r.dest.xnum - r.dest_x); copy_range(&r.src, r.src_x, &r.dest, r.dest_x, num); for (TrackCursor *t = r.cursors; !t->is_sentinel; t++) {