From ee3ce3c7d8be8a22faf52bc7fd99a44cbccd2791 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Sep 2018 08:58:33 +0530 Subject: [PATCH] Move cell_as_sgr to line.c --- kitty/cursor.c | 70 ++++++---------------------------------------- kitty/data-types.h | 1 + kitty/line.c | 67 ++++++++++++++++++++++++++++++++++++++++++++ kitty/screen.c | 8 +----- 4 files changed, 77 insertions(+), 69 deletions(-) diff --git a/kitty/cursor.c b/kitty/cursor.c index 53f8328a9..1f0ba2a4a 100644 --- a/kitty/cursor.c +++ b/kitty/cursor.c @@ -211,69 +211,15 @@ END_ALLOW_CASE_RANGE #undef RANGE } -static inline int -color_as_sgr(char *buf, size_t sz, unsigned long val, unsigned simple_code, unsigned aix_code, unsigned complex_code) { - switch(val & 0xff) { - case 1: - val >>= 8; - if (val < 16 && simple_code) { - return snprintf(buf, sz, "%lu;", (val < 8) ? simple_code + val : aix_code + (val - 8)); - } - return snprintf(buf, sz, "%u:5:%lu;", complex_code, val); - case 2: - return snprintf(buf, sz, "%u:2:%lu:%lu:%lu;", complex_code, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff); - default: - return snprintf(buf, sz, "%u;", complex_code + 1); // reset - } -} - -static inline const char* -decoration_as_sgr(uint8_t decoration) { - switch(decoration) { - case 1: return "4;"; - case 2: return "4:2;"; - case 3: return "4:3;"; - default: return "24;"; - } -} - const char* -cell_as_sgr(GPUCell *cell, GPUCell *prev) { - static char buf[128]; -#define SZ sizeof(buf) - (p - buf) - 2 -#define P(s) { size_t len = strlen(s); if (SZ > len) { memcpy(p, s, len); p += len; } } - char *p = buf; -#define CMP(attr) (attr(cell) != attr(prev)) -#define BOLD(cell) (cell->attrs & (1 << BOLD_SHIFT)) -#define DIM(cell) (cell->attrs & (1 << DIM_SHIFT)) -#define ITALIC(cell) (cell->attrs & (1 << ITALIC_SHIFT)) -#define REVERSE(cell) (cell->attrs & (1 << REVERSE_SHIFT)) -#define STRIKETHROUGH(cell) (cell->attrs & (1 << STRIKE_SHIFT)) -#define DECORATION(cell) (cell->attrs & (DECORATION_MASK << DECORATION_SHIFT)) - bool intensity_differs = CMP(BOLD) || CMP(DIM); - if (intensity_differs) { - if (!BOLD(cell) && !DIM(cell)) { P("22;"); } - else { if (BOLD(cell)) P("1;"); if (DIM(cell)) P("2;"); } - } - if (CMP(ITALIC)) P(ITALIC(cell) ? "3;" : "23;"); - if (CMP(REVERSE)) P(REVERSE(cell) ? "7;" : "27;"); - if (CMP(STRIKETHROUGH)) P(STRIKETHROUGH(cell) ? "9;" : "29;"); - if (cell->fg != prev->fg) p += color_as_sgr(p, SZ, cell->fg, 30, 90, 38); - if (cell->bg != prev->bg) p += color_as_sgr(p, SZ, cell->bg, 40, 100, 48); - if (cell->decoration_fg != prev->decoration_fg) p += color_as_sgr(p, SZ, cell->decoration_fg, 0, 0, DECORATION_FG_CODE); - if (CMP(DECORATION)) P(decoration_as_sgr((cell->attrs >> DECORATION_SHIFT) & DECORATION_MASK)); -#undef CMP -#undef BOLD -#undef DIM -#undef ITALIC -#undef REVERSE -#undef STRIKETHROUGH -#undef DECORATION -#undef P -#undef SZ - if (p > buf) *(p - 1) = 0; // remove trailing semi-colon - *p = 0; // ensure string is null-terminated - return buf; +cursor_as_sgr(const Cursor *self) { + GPUCell blank_cell = { 0 }, cursor_cell = { + .attrs = CURSOR_TO_ATTRS(self, 1), + .fg = self->fg & COL_MASK, + .bg = self->bg & COL_MASK, + .decoration_fg = self->decoration_fg & COL_MASK, + }; + return cell_as_sgr(&cursor_cell, &blank_cell); } static PyObject * diff --git a/kitty/data-types.h b/kitty/data-types.h index c3d5b2c76..f3295629c 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -277,6 +277,7 @@ void cursor_reset_display_attrs(Cursor*); void cursor_from_sgr(Cursor *self, unsigned int *params, unsigned int count); void apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, unsigned int *params, unsigned int count); const char* cell_as_sgr(GPUCell *, GPUCell *); +const char* cursor_as_sgr(const Cursor *); double monotonic(); PyObject* cm_thread_write(PyObject *self, PyObject *args); diff --git a/kitty/line.c b/kitty/line.c index 74330535b..d871d5281 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -528,6 +528,73 @@ set_attribute(Line *self, PyObject *args) { Py_RETURN_NONE; } +static inline int +color_as_sgr(char *buf, size_t sz, unsigned long val, unsigned simple_code, unsigned aix_code, unsigned complex_code) { + switch(val & 0xff) { + case 1: + val >>= 8; + if (val < 16 && simple_code) { + return snprintf(buf, sz, "%lu;", (val < 8) ? simple_code + val : aix_code + (val - 8)); + } + return snprintf(buf, sz, "%u:5:%lu;", complex_code, val); + case 2: + return snprintf(buf, sz, "%u:2:%lu:%lu:%lu;", complex_code, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff); + default: + return snprintf(buf, sz, "%u;", complex_code + 1); // reset + } +} + +static inline const char* +decoration_as_sgr(uint8_t decoration) { + switch(decoration) { + case 1: return "4;"; + case 2: return "4:2;"; + case 3: return "4:3;"; + default: return "24;"; + } +} + + +const char* +cell_as_sgr(GPUCell *cell, GPUCell *prev) { + static char buf[128]; +#define SZ sizeof(buf) - (p - buf) - 2 +#define P(s) { size_t len = strlen(s); if (SZ > len) { memcpy(p, s, len); p += len; } } + char *p = buf; +#define CMP(attr) (attr(cell) != attr(prev)) +#define BOLD(cell) (cell->attrs & (1 << BOLD_SHIFT)) +#define DIM(cell) (cell->attrs & (1 << DIM_SHIFT)) +#define ITALIC(cell) (cell->attrs & (1 << ITALIC_SHIFT)) +#define REVERSE(cell) (cell->attrs & (1 << REVERSE_SHIFT)) +#define STRIKETHROUGH(cell) (cell->attrs & (1 << STRIKE_SHIFT)) +#define DECORATION(cell) (cell->attrs & (DECORATION_MASK << DECORATION_SHIFT)) + bool intensity_differs = CMP(BOLD) || CMP(DIM); + if (intensity_differs) { + if (!BOLD(cell) && !DIM(cell)) { P("22;"); } + else { if (BOLD(cell)) P("1;"); if (DIM(cell)) P("2;"); } + } + if (CMP(ITALIC)) P(ITALIC(cell) ? "3;" : "23;"); + if (CMP(REVERSE)) P(REVERSE(cell) ? "7;" : "27;"); + if (CMP(STRIKETHROUGH)) P(STRIKETHROUGH(cell) ? "9;" : "29;"); + if (cell->fg != prev->fg) p += color_as_sgr(p, SZ, cell->fg, 30, 90, 38); + if (cell->bg != prev->bg) p += color_as_sgr(p, SZ, cell->bg, 40, 100, 48); + if (cell->decoration_fg != prev->decoration_fg) p += color_as_sgr(p, SZ, cell->decoration_fg, 0, 0, DECORATION_FG_CODE); + if (CMP(DECORATION)) P(decoration_as_sgr((cell->attrs >> DECORATION_SHIFT) & DECORATION_MASK)); +#undef CMP +#undef BOLD +#undef DIM +#undef ITALIC +#undef REVERSE +#undef STRIKETHROUGH +#undef DECORATION +#undef P +#undef SZ + if (p > buf) *(p - 1) = 0; // remove trailing semi-colon + *p = 0; // ensure string is null-terminated + return buf; +} + + static Py_ssize_t __len__(PyObject *self) { return (Py_ssize_t)(((Line*)self)->xnum); diff --git a/kitty/screen.c b/kitty/screen.c index 65d312115..e24c60dcd 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1429,13 +1429,7 @@ screen_request_capabilities(Screen *self, char c, PyObject *q) { shape = snprintf(buf, sizeof(buf), "1$r%d q", shape); } else if (strcmp("m", query) == 0) { // SGR - GPUCell blank_cell = { 0 }, cursor_cell = { - .attrs = CURSOR_TO_ATTRS(self->cursor, 1), - .fg = self->cursor->fg & COL_MASK, - .bg = self->cursor->bg & COL_MASK, - .decoration_fg = self->cursor->decoration_fg & COL_MASK, - }; - shape = snprintf(buf, sizeof(buf), "1$r%sm", cell_as_sgr(&cursor_cell, &blank_cell)); + shape = snprintf(buf, sizeof(buf), "1$r%sm", cursor_as_sgr(self->cursor)); } else if (strcmp("r", query) == 0) { shape = snprintf(buf, sizeof(buf), "1$r%u;%ur", self->margin_top + 1, self->margin_bottom + 1); } else {