commit dd33bbb
uint
·
2026-05-14 16:03:56 +0000 UTC
parent 8955d95
parse sgr colors
3 files changed,
+53,
-3
+2,
-2
1@@ -48,8 +48,8 @@ static Term term = {
2 .rows = 0,
3 .ptyfd = -1,
4 .ptypid = -1,
5- .fg = 0xffffffff,
6- .bg = 0xff222222,
7+ .fg = TERM_DEFAULT_FG,
8+ .bg = TERM_DEFAULT_BG,
9 };
10
11 /**
+2,
-0
1@@ -8,6 +8,8 @@
2 /* resolve 1D rune from 2D input */
3 #define RUNE(t, x, y) ((t)->runes[(y) * (t)->cols + (x)])
4 #define CSI_PARAMS_MAX (16)
5+#define TERM_DEFAULT_FG 0xffffffffu
6+#define TERM_DEFAULT_BG 0xff222222u
7
8 enum TState {
9 TSTATE_NORMAL,
+49,
-1
1@@ -5,6 +5,7 @@
2 #include <grapheme.h>
3
4 #include "term.h"
5+#include "utils.h"
6
7 static int csi_arg(Term* t, int i, int fallback);
8 static void csi_dispatch(Term* t, Caret* c, unsigned char ch);
9@@ -12,6 +13,14 @@ static void csi_reset(Term* t);
10 static void erase_display(Term* t, Caret* c);
11 static void erase_line(Term* t, Caret* c);
12 static void reset_rune(Term* t, int x, int y);
13+static void sgr(Term* t);
14+
15+static const uint32_t ansi_colours[16] = { /* TODO */
16+ 0xff000000, 0xff0000cd, 0xff00cd00, 0xff00cdcd,
17+ 0xffee0000, 0xffcd00cd, 0xffcdcd00, 0xffe5e5e5,
18+ 0xff7f7f7f, 0xff0000ff, 0xff00ff00, 0xff00ffff,
19+ 0xffff5c5c, 0xffff00ff, 0xffffff00, 0xffffffff,
20+};
21
22 /** TODO
23 */
24@@ -58,7 +67,7 @@ static void csi_dispatch(Term* t, Caret* c, unsigned char ch)
25 break;
26
27 case 'm': /* SGR - Select Graphics Rendition - ESC[nm, colours */
28- /* TODO: colours */
29+ sgr(t);
30 break;
31
32 case 'A': { /* CUU - cursor up */
33@@ -196,6 +205,45 @@ static void reset_rune(Term* t, int x, int y)
34 r->dmg = true;
35 }
36
37+/** TODO
38+ */
39+static void sgr(Term* t)
40+{
41+ for (int i = 0; i <= t->csi_param; i++) {
42+ int p = t->csi_params[i];
43+
44+ if (p == 0) {
45+ t->fg = TERM_DEFAULT_FG;
46+ t->bg = TERM_DEFAULT_BG;
47+ }
48+ else if (p >= 30 && p <= 37)
49+ t->fg = ansi_colours[p - 30];
50+ else if (p >= 40 && p <= 47)
51+ t->bg = ansi_colours[p - 40];
52+ else if (p >= 90 && p <= 97)
53+ t->fg = ansi_colours[p - 90 + 8];
54+ else if (p >= 100 && p <= 107)
55+ t->bg = ansi_colours[p - 100 + 8];
56+ else if (p == 39)
57+ t->fg = TERM_DEFAULT_FG;
58+ else if (p == 49)
59+ t->bg = TERM_DEFAULT_BG;
60+ else if ((p == 38 || p == 48) &&
61+ i + 4 <= t->csi_param && t->csi_params[i + 1] == 2) {
62+ uint32_t col = rgba(
63+ t->csi_params[i + 2],
64+ t->csi_params[i + 3],
65+ t->csi_params[i + 4], 255
66+ );
67+ if (p == 38)
68+ t->fg = col;
69+ else
70+ t->bg = col;
71+ i += 4;
72+ }
73+ }
74+}
75+
76 void term_clear(Term* t, Caret* c)
77 {
78 for (int y = 0; y < t->rows; y++) {