From 3a97fce6d06dea35211191237a6ad3c2d41e5d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 26 Jun 2019 19:44:31 +0200 Subject: [PATCH] grid: attributes now track whether we've set a foreground/background color This means we don't have to explicitly set the foreground/background to the grid's default colors whenever we reset/clear a cell, and we can instead simply memset() the entire cell to 0. This also means the renderer has to get the default color when rendering a cell without a foreground/background color set. --- csi.c | 26 +++++++++++++++++--------- grid.c | 8 -------- main.c | 6 ++++-- terminal.h | 6 ++++-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/csi.c b/csi.c index 49431bca..ba026101 100644 --- a/csi.c +++ b/csi.c @@ -75,14 +75,8 @@ param_get(const struct terminal *term, size_t idx, int default_value) static void sgr_reset(struct terminal *term) { - term->vt.attrs.bold = false; + memset(&term->vt.attrs, 0, sizeof(term->vt.attrs)); term->vt.dim = false; - term->vt.attrs.italic = false; - term->vt.attrs.underline = false; - term->vt.attrs.strikethrough = false; - term->vt.attrs.blink = false; - term->vt.attrs.conceal = false; - term->vt.attrs.reverse = false; term->vt.attrs.foreground = term->grid.foreground; term->vt.attrs.background = term->grid.background; } @@ -130,6 +124,7 @@ csi_sgr(struct terminal *term) case 35: case 36: case 37: + term->vt.attrs.have_foreground = true; term->vt.attrs.foreground = colors_regular[param - 30]; break; @@ -139,6 +134,7 @@ csi_sgr(struct terminal *term) { size_t idx = term->vt.params.v[i + 2].value; term->vt.attrs.foreground = colors256[idx]; + term->vt.attrs.have_foreground = true; i += 2; } else if (term->vt.params.idx - i - 1 == 4 && term->vt.params.v[i + 1].value == 2) @@ -147,6 +143,7 @@ csi_sgr(struct terminal *term) uint32_t g = term->vt.params.v[i + 3].value; uint32_t b = term->vt.params.v[i + 4].value; term->vt.attrs.foreground = r << 24 | g << 16 | b << 8 | 0xff; + term->vt.attrs.have_foreground = true; i += 4; } else { LOG_ERR("invalid CSI SGR sequence"); @@ -154,7 +151,10 @@ csi_sgr(struct terminal *term) } break; } - case 39: term->vt.attrs.foreground = term->grid.foreground; break; + case 39: + term->vt.attrs.foreground = term->grid.foreground; + term->vt.attrs.have_foreground = false; + break; /* Regular background colors */ case 40: @@ -166,6 +166,7 @@ csi_sgr(struct terminal *term) case 46: case 47: term->vt.attrs.background = colors_regular[param - 40]; + term->vt.attrs.have_background = true; break; case 48: { @@ -174,6 +175,7 @@ csi_sgr(struct terminal *term) { size_t idx = term->vt.params.v[i + 2].value; term->vt.attrs.background = colors256[idx]; + term->vt.attrs.have_background = true; i += 2; } else if (term->vt.params.idx - i - 1 == 4 && term->vt.params.v[i + 1].value == 2) @@ -182,6 +184,7 @@ csi_sgr(struct terminal *term) uint32_t g = term->vt.params.v[i + 3].value; uint32_t b = term->vt.params.v[i + 4].value; term->vt.attrs.background = r << 24 | g << 16 | b << 8 | 0xff; + term->vt.attrs.have_background = true; i += 4; } else { LOG_ERR("invalid CSI SGR sequence"); @@ -189,7 +192,10 @@ csi_sgr(struct terminal *term) } break; } - case 49: term->vt.attrs.background = term->grid.background; break; + case 49: + term->vt.attrs.background = term->grid.background; + term->vt.attrs.have_background = false; + break; /* Bright foreground colors */ case 90: @@ -201,6 +207,7 @@ csi_sgr(struct terminal *term) case 96: case 97: term->vt.attrs.foreground = colors_bright[param - 90]; + term->vt.attrs.have_foreground = true; break; /* Regular background colors */ @@ -213,6 +220,7 @@ csi_sgr(struct terminal *term) case 106: case 107: term->vt.attrs.background = colors_bright[param - 100]; + term->vt.attrs.have_background = true; break; default: diff --git a/grid.c b/grid.c index 2663952d..a6ed618b 100644 --- a/grid.c +++ b/grid.c @@ -187,14 +187,6 @@ grid_erase(struct grid *grid, int start, int end) { assert(end >= start); memset(&grid->cells[start], 0, (end - start) * sizeof(grid->cells[0])); - - for (int i = start; i < end; i++) { - struct cell *cell = &grid->cells[i]; - - cell->attrs.foreground = grid->foreground; - cell->attrs.background = grid->background; - } - grid_damage_erase(grid, start, end - start); } diff --git a/main.c b/main.c index 781db37b..ec951f9b 100644 --- a/main.c +++ b/main.c @@ -102,8 +102,10 @@ grid_render_update(struct context *c, struct buffer *buf, const struct damage *d int width = c->term.grid.cell_width; int height = c->term.grid.cell_height; - uint32_t foreground = cell->attrs.foreground; - uint32_t background = cell->attrs.background; + uint32_t foreground = cell->attrs.have_foreground + ? cell->attrs.foreground : c->term.grid.foreground; + uint32_t background = cell->attrs.have_background + ? cell->attrs.background : c->term.grid.background; if (has_cursor) { uint32_t swap = foreground; diff --git a/terminal.h b/terminal.h index 08a0e538..3eb8abce 100644 --- a/terminal.h +++ b/terminal.h @@ -19,8 +19,10 @@ struct attributes { bool blink; bool conceal; bool reverse; - uint32_t foreground; - uint32_t background; + bool have_foreground; + bool have_background; + uint32_t foreground; /* Only valid when have_foreground == true */ + uint32_t background; /* Only valid when have_background == true */ }; struct cell {