From ce8b0cf748142b042a5427a8c9dd39c0dbfed9ae Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Feb 2022 21:40:33 +0530 Subject: [PATCH] Allow using line_as_ansi with ranges --- kitty/history.c | 4 ++-- kitty/line-buf.c | 4 ++-- kitty/line.c | 12 ++++++------ kitty/lineops.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index 66198f749..7a6135eaf 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -242,7 +242,7 @@ pagerhist_push(HistoryBuf *self, ANSIBuf *as_ansi_buf) { const GPUCell *prev_cell = NULL; Line l = {.xnum=self->xnum}; init_line(self, self->start_of_data, &l); - line_as_ansi(&l, as_ansi_buf, &prev_cell); + line_as_ansi(&l, as_ansi_buf, &prev_cell, 0, l.xnum); if (ringbuf_bytes_used(ph->ringbuf) && !l.attrs.continued) pagerhist_write_bytes(ph, (const uint8_t*)"\n", 1); pagerhist_write_bytes(ph, (const uint8_t*)"\x1b[m", 3); if (pagerhist_write_ucs4(ph, as_ansi_buf->buf, as_ansi_buf->len)) pagerhist_write_bytes(ph, (const uint8_t*)"\r", 1); @@ -324,7 +324,7 @@ as_ansi(HistoryBuf *self, PyObject *callback) { if (i < self->count - 1) { l.attrs.continued = attrptr(self, index_of(self, i + 1))->continued; } else l.attrs.continued = false; - line_as_ansi(&l, &output, &prev_cell); + line_as_ansi(&l, &output, &prev_cell, 0, l.xnum); if (!l.attrs.continued) { ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false); output.buf[output.len++] = 10; // 10 = \n diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 64bec8a98..6fb65c531 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -413,7 +413,7 @@ as_ansi(LineBuf *self, PyObject *callback) { ANSIBuf output = {0}; do { init_line(self, (&l), self->line_map[ylimit]); - line_as_ansi(&l, &output, &prev_cell); + line_as_ansi(&l, &output, &prev_cell, 0, l.xnum); if (output.len) break; ylimit--; } while(ylimit > 0); @@ -421,7 +421,7 @@ as_ansi(LineBuf *self, PyObject *callback) { for(index_type i = 0; i <= ylimit; i++) { l.attrs.continued = self->line_attrs[(i + 1 < self->ynum) ? i+1 : i].continued; init_line(self, (&l), self->line_map[i]); - line_as_ansi(&l, &output, &prev_cell); + line_as_ansi(&l, &output, &prev_cell, 0, l.xnum); if (!l.attrs.continued) { ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false); output.buf[output.len++] = 10; // 10 = \n diff --git a/kitty/line.c b/kitty/line.c index 89fabd833..cced0cfbd 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -302,14 +302,14 @@ write_hyperlink(hyperlink_id_type hid, ANSIBuf *output) { void -line_as_ansi(Line *self, ANSIBuf *output, const GPUCell** prev_cell) { +line_as_ansi(Line *self, ANSIBuf *output, const GPUCell** prev_cell, index_type start_at, index_type stop_before) { #define ENSURE_SPACE(extra) ensure_space_for(output, buf, Py_UCS4, output->len + extra, capacity, 2048, false); #define WRITE_SGR(val) { ENSURE_SPACE(128); write_sgr(val, output); } #define WRITE_CH(val) { ENSURE_SPACE(1); output->buf[output->len++] = val; } #define WRITE_HYPERLINK(val) { ENSURE_SPACE(2256); write_hyperlink(val, output); } output->len = 0; - index_type limit = xlimit_for_line(self); - if (limit == 0) return; + index_type limit = MIN(stop_before, xlimit_for_line(self)); + if (limit <= start_at) return; char_type previous_width = 0; static const GPUCell blank_cell = { 0 }; @@ -317,7 +317,7 @@ line_as_ansi(Line *self, ANSIBuf *output, const GPUCell** prev_cell) { if (*prev_cell == NULL) *prev_cell = &blank_cell; const CellAttrs mask_for_sgr = {.val=SGR_MASK}; - for (index_type pos=0; pos < limit; pos++) { + for (index_type pos=start_at; pos < limit; pos++) { char_type ch = self->cpu_cells[pos].ch; if (ch == 0) { if (previous_width == 2) { previous_width = 0; continue; } @@ -365,7 +365,7 @@ as_ansi(Line* self, PyObject *a UNUSED) { #define as_ansi_doc "Return the line's contents with ANSI (SGR) escape codes for formatting" const GPUCell *prev_cell = NULL; ANSIBuf output = {0}; - line_as_ansi(self, &output, &prev_cell); + line_as_ansi(self, &output, &prev_cell, 0, self->xnum); PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output.buf, output.len); free(output.buf); return ans; @@ -834,7 +834,7 @@ as_text_generic(PyObject *args, void *container, get_line_func get_line, index_t // get around to writing a nice pager kitten. // see https://github.com/kovidgoyal/kitty/issues/2381 prev_cell = NULL; - line_as_ansi(line, ansibuf, &prev_cell); + line_as_ansi(line, ansibuf, &prev_cell, 0, line->xnum); t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, ansibuf->buf, ansibuf->len); if (t && ansibuf->len > 0) APPEND(sgr_reset); } else { diff --git a/kitty/lineops.h b/kitty/lineops.h index 9c01b7e82..89537776f 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -84,7 +84,7 @@ void line_add_combining_char(Line *, uint32_t , unsigned int ); index_type line_url_start_at(Line *self, index_type x); index_type line_url_end_at(Line *self, index_type x, bool, char_type, bool); bool line_startswith_url_chars(Line*); -void line_as_ansi(Line *self, ANSIBuf *output, const GPUCell**) __attribute__((nonnull)); +void line_as_ansi(Line *self, ANSIBuf *output, const GPUCell**, index_type start_at, index_type stop_before) __attribute__((nonnull)); unsigned int line_length(Line *self); size_t cell_as_unicode(CPUCell *cell, bool include_cc, Py_UCS4 *buf, char_type); size_t cell_as_unicode_for_fallback(CPUCell *cell, Py_UCS4 *buf);