Allow using line_as_ansi with ranges

This commit is contained in:
Kovid Goyal 2022-02-09 21:40:33 +05:30
parent 696f371aa4
commit ce8b0cf748
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 11 additions and 11 deletions

View File

@ -242,7 +242,7 @@ pagerhist_push(HistoryBuf *self, ANSIBuf *as_ansi_buf) {
const GPUCell *prev_cell = NULL; const GPUCell *prev_cell = NULL;
Line l = {.xnum=self->xnum}; Line l = {.xnum=self->xnum};
init_line(self, self->start_of_data, &l); 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); 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); 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); 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) { if (i < self->count - 1) {
l.attrs.continued = attrptr(self, index_of(self, i + 1))->continued; l.attrs.continued = attrptr(self, index_of(self, i + 1))->continued;
} else l.attrs.continued = false; } 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) { if (!l.attrs.continued) {
ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false); ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false);
output.buf[output.len++] = 10; // 10 = \n output.buf[output.len++] = 10; // 10 = \n

View File

@ -413,7 +413,7 @@ as_ansi(LineBuf *self, PyObject *callback) {
ANSIBuf output = {0}; ANSIBuf output = {0};
do { do {
init_line(self, (&l), self->line_map[ylimit]); 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; if (output.len) break;
ylimit--; ylimit--;
} while(ylimit > 0); } while(ylimit > 0);
@ -421,7 +421,7 @@ as_ansi(LineBuf *self, PyObject *callback) {
for(index_type i = 0; i <= ylimit; i++) { for(index_type i = 0; i <= ylimit; i++) {
l.attrs.continued = self->line_attrs[(i + 1 < self->ynum) ? i+1 : i].continued; l.attrs.continued = self->line_attrs[(i + 1 < self->ynum) ? i+1 : i].continued;
init_line(self, (&l), self->line_map[i]); 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) { if (!l.attrs.continued) {
ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false); ensure_space_for(&output, buf, Py_UCS4, output.len + 1, capacity, 2048, false);
output.buf[output.len++] = 10; // 10 = \n output.buf[output.len++] = 10; // 10 = \n

View File

@ -302,14 +302,14 @@ write_hyperlink(hyperlink_id_type hid, ANSIBuf *output) {
void 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 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_SGR(val) { ENSURE_SPACE(128); write_sgr(val, output); }
#define WRITE_CH(val) { ENSURE_SPACE(1); output->buf[output->len++] = val; } #define WRITE_CH(val) { ENSURE_SPACE(1); output->buf[output->len++] = val; }
#define WRITE_HYPERLINK(val) { ENSURE_SPACE(2256); write_hyperlink(val, output); } #define WRITE_HYPERLINK(val) { ENSURE_SPACE(2256); write_hyperlink(val, output); }
output->len = 0; output->len = 0;
index_type limit = xlimit_for_line(self); index_type limit = MIN(stop_before, xlimit_for_line(self));
if (limit == 0) return; if (limit <= start_at) return;
char_type previous_width = 0; char_type previous_width = 0;
static const GPUCell blank_cell = { 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; if (*prev_cell == NULL) *prev_cell = &blank_cell;
const CellAttrs mask_for_sgr = {.val=SGR_MASK}; 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; char_type ch = self->cpu_cells[pos].ch;
if (ch == 0) { if (ch == 0) {
if (previous_width == 2) { previous_width = 0; continue; } 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" #define as_ansi_doc "Return the line's contents with ANSI (SGR) escape codes for formatting"
const GPUCell *prev_cell = NULL; const GPUCell *prev_cell = NULL;
ANSIBuf output = {0}; 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); PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output.buf, output.len);
free(output.buf); free(output.buf);
return ans; 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. // get around to writing a nice pager kitten.
// see https://github.com/kovidgoyal/kitty/issues/2381 // see https://github.com/kovidgoyal/kitty/issues/2381
prev_cell = NULL; 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); t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, ansibuf->buf, ansibuf->len);
if (t && ansibuf->len > 0) APPEND(sgr_reset); if (t && ansibuf->len > 0) APPEND(sgr_reset);
} else { } else {

View File

@ -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_start_at(Line *self, index_type x);
index_type line_url_end_at(Line *self, index_type x, bool, char_type, bool); index_type line_url_end_at(Line *self, index_type x, bool, char_type, bool);
bool line_startswith_url_chars(Line*); 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); 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(CPUCell *cell, bool include_cc, Py_UCS4 *buf, char_type);
size_t cell_as_unicode_for_fallback(CPUCell *cell, Py_UCS4 *buf); size_t cell_as_unicode_for_fallback(CPUCell *cell, Py_UCS4 *buf);