Have line_as_ansi indicate when the output is truncated

This commit is contained in:
Kovid Goyal 2018-11-20 11:13:49 +05:30
parent 33b62dcabc
commit ab97646576
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 16 additions and 10 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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;
}

View File

@ -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); \