commit dc078d3

uint  ·  2026-05-14 19:16:43 +0000 UTC
parent c733a52
add reverse term colours attribute support (SGR 7, 27)
3 files changed,  +17, -3
+3, -2
 1@@ -350,8 +350,9 @@ static void run(void)
 2 				Rune* r = &RUNE(&term, x, y);
 3 				bool cursor = term.cursor_visible &&
 4 				              x == car.x && y == car.y;
 5-				uint32_t fg = cursor ? r->bg : r->fg;
 6-				uint32_t bg = cursor ? r->fg : r->bg;
 7+				bool reverse = (r->attr & TERM_ATTR_REVERSE) != 0;
 8+				uint32_t fg = (cursor != reverse) ? r->bg : r->fg;
 9+				uint32_t bg = (cursor != reverse) ? r->fg : r->bg;
10 
11 				if (!r->dmg)
12 					continue;
+3, -0
 1@@ -10,6 +10,7 @@
 2 #define CSI_PARAMS_MAX (16)
 3 #define TERM_DEFAULT_FG 0xffffffffu
 4 #define TERM_DEFAULT_BG 0xff222222u
 5+#define TERM_ATTR_REVERSE (1 << 0)
 6 
 7 enum TState {
 8 	TSTATE_NORMAL,
 9@@ -21,6 +22,7 @@ typedef struct {
10 	uint32_t       cp;  /* codepoint */
11 	uint32_t       fg;
12 	uint32_t       bg;
13+	uint8_t        attr;
14 	bool           dmg; /* rune damaged */
15 } Rune;
16 
17@@ -35,6 +37,7 @@ typedef struct {
18 	Caret          saved;
19 	uint32_t       fg;
20 	uint32_t       bg;
21+	uint8_t        attr;
22 
23 	int            cols;
24 	int            rows;
+11, -1
 1@@ -351,6 +351,7 @@ static void reset_rune(Term* t, int x, int y)
 2 	r->cp = ' ';
 3 	r->fg = t->fg;
 4 	r->bg = t->bg;
 5+	r->attr = t->attr;
 6 	r->dmg = true;
 7 }
 8 
 9@@ -404,7 +405,12 @@ static void sgr(Term* t)
10 		if (p == 0) {
11 			t->fg = TERM_DEFAULT_FG;
12 			t->bg = TERM_DEFAULT_BG;
13+			t->attr = 0;
14 		}
15+		else if (p == 7)
16+			t->attr |= TERM_ATTR_REVERSE;
17+		else if (p == 27)
18+			t->attr &= ~TERM_ATTR_REVERSE; /* turn off reverse attribute bit */
19 		else if (p >= 30 && p <= 37)
20 			t->fg = ansi_colours[p - 30];
21 		else if (p >= 40 && p <= 47)
22@@ -485,6 +491,7 @@ void term_clear(Term* t, Caret* c)
23 			rune->cp = ' ';
24 			rune->fg = t->fg;
25 			rune->bg = t->bg;
26+			rune->attr = t->attr;
27 			rune->dmg = true;
28 		}
29 	}
30@@ -547,10 +554,12 @@ void term_putc(Term* t, Caret* c, uint32_t cp)
31 		}
32 
33 		Rune* r = &RUNE(t, c->x, c->y);
34-		if (r->cp != cp || r->fg != t->fg || r->bg != t->bg) {
35+		if (r->cp != cp || r->fg != t->fg || r->bg != t->bg ||
36+		    r->attr != t->attr) {
37 			r->cp = cp;
38 			r->fg = t->fg;
39 			r->bg = t->bg;
40+			r->attr = t->attr;
41 			r->dmg = true;
42 		}
43 		if (c->x == t->cols - 1)
44@@ -592,6 +601,7 @@ int term_resize(Term* t, Caret* c, int cols, int rows)
45 			r->cp = ' ';
46 			r->fg = t->fg;
47 			r->bg = t->bg;
48+			r->attr = t->attr;
49 			r->dmg = true;
50 		}
51 	}