From ab97646576abf97b4ca2b51aeb58413356a63a12 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Nov 2018 11:13:49 +0530 Subject: [PATCH] Have line_as_ansi indicate when the output is truncated --- kitty/history.c | 6 ++++-- kitty/line-buf.c | 5 +++-- kitty/line.c | 10 ++++++---- kitty/lineops.h | 5 +++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index 086419e02..4da02eff6 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -161,6 +161,7 @@ static inline void pagerhist_push(HistoryBuf *self) { PagerHistoryBuf *ph = self->pagerhist; if (!ph) return; + bool truncated; Line l = {.xnum=self->xnum}; init_line(self, self->start_of_data, &l); if (ph->start != ph->end && !l.continued) { @@ -169,7 +170,7 @@ pagerhist_push(HistoryBuf *self) { if (ph->bufsize - ph->end < 1024 && !pagerhist_extend(ph)) { ph->bufend = ph->end; ph->end = 0; } - ph->end += line_as_ansi(&l, ph->buffer + ph->end, 1023); + ph->end += line_as_ansi(&l, ph->buffer + ph->end, 1023, &truncated); ph->buffer[ph->end++] = '\r'; if (ph->bufend) ph->start = ph->end + 1 < ph->bufend ? ph->end + 1 : 0; @@ -234,12 +235,13 @@ as_ansi(HistoryBuf *self, PyObject *callback) { #define as_ansi_doc "as_ansi(callback) -> The contents of this buffer as ANSI escaped text. callback is called with each successive line." static Py_UCS4 t[5120]; Line l = {.xnum=self->xnum}; + bool truncated; for(unsigned int i = 0; i < self->count; i++) { init_line(self, i, &l); if (i < self->count - 1) { l.continued = *attrptr(self, index_of(self, i + 1)) & CONTINUED_MASK; } else l.continued = false; - index_type num = line_as_ansi(&l, t, 5120); + index_type num = line_as_ansi(&l, t, 5120, &truncated); if (!(l.continued) && num < 5119) t[num++] = 10; // 10 = \n PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, t, num); if (ans == NULL) return PyErr_NoMemory(); diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 0e8179639..87dff3ebe 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -397,16 +397,17 @@ as_ansi(LineBuf *self, PyObject *callback) { Line l = {.xnum=self->xnum}; // remove trailing empty lines index_type ylimit = self->ynum - 1; + bool truncated; do { init_line(self, (&l), self->line_map[ylimit]); - if (line_as_ansi(&l, t, 5120) != 0) break; + if (line_as_ansi(&l, t, 5120, &truncated) != 0) break; ylimit--; } while(ylimit > 0); for(index_type i = 0; i <= ylimit; i++) { l.continued = ((i < self->ynum - 1) ? self->line_attrs[i+1] : self->line_attrs[i]) & CONTINUED_MASK; init_line(self, (&l), self->line_map[i]); - index_type num = line_as_ansi(&l, t, 5120); + index_type num = line_as_ansi(&l, t, 5120, &truncated); if (!(l.continued) && num < 5119) t[num++] = 10; // 10 = \n PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, t, num); if (ans == NULL) return PyErr_NoMemory(); diff --git a/kitty/line.c b/kitty/line.c index d871d5281..c45b2764c 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -227,11 +227,12 @@ write_sgr(const char *val, Py_UCS4 *buf, index_type buflen, index_type *i) { } index_type -line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen) { -#define WRITE_SGR(val) { if (!write_sgr(val, buf, buflen, &i)) return i; } -#define WRITE_CH(val) if (i > buflen - 1) return i; buf[i++] = val; +line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen, bool *truncated) { +#define WRITE_SGR(val) { if (!write_sgr(val, buf, buflen, &i)) { *truncated = true; return i; } } +#define WRITE_CH(val) if (i > buflen - 1) { *truncated = true; return i; } buf[i++] = val; index_type limit = xlimit_for_line(self), i=0; + *truncated = false; if (limit == 0) return 0; char_type previous_width = 0; @@ -271,7 +272,8 @@ static PyObject* as_ansi(Line* self, PyObject *a UNUSED) { #define as_ansi_doc "Return the line's contents with ANSI (SGR) escape codes for formatting" static Py_UCS4 t[5120] = {0}; - index_type num = line_as_ansi(self, t, 5120); + bool truncated; + index_type num = line_as_ansi(self, t, 5120, &truncated); PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, t, num); return ans; } diff --git a/kitty/lineops.h b/kitty/lineops.h index 3347d78e6..e38d72e52 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -60,7 +60,7 @@ void line_right_shift(Line *, unsigned int , unsigned int ); 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); -index_type line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen); +index_type line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen, bool*); 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_utf8(CPUCell *cell, bool include_cc, char *buf, char_type); @@ -110,7 +110,8 @@ void historybuf_clear(HistoryBuf *self); Py_CLEAR(ret); \ } \ if (as_ansi) { \ - index_type num = line_as_ansi(line, buf, columns * 100 - 2); \ + bool truncated; \ + index_type num = line_as_ansi(line, buf, columns * 100 - 2, &truncated); \ t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, num); \ } else { \ t = line_as_unicode(line); \