xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / tui / theme.c

6.7 KB
1#include "theme.h"
2
3#include <stdio.h>
4#include <string.h>
5
6// The TokyoNight `night` slots the TUI uses, from folke/tokyonight.nvim.
7static const tiny_palette TOKYO_NIGHT = {
8    .bg = "#1a1b26",
9    .fg = "#c0caf5",
10    .fg_dark = "#a9b1d6",
11    .comment = "#565f89",
12    .terminal_black = "#414868",
13    .blue = "#7aa2f7",
14    .blue1 = "#2ac3de",
15    .blue5 = "#89ddff",
16    .green = "#9ece6a",
17    .green1 = "#73daca",
18    .magenta = "#bb9af7",
19    .orange = "#ff9e64",
20    .purple = "#9d7cd8",
21    .red = "#f7768e",
22    .teal = "#1abc9c",
23    .yellow = "#e0af68",
24};
25
26static const char DIFF_ADD[] = "#243e4a";
27static const char DIFF_DELETE[] = "#4a272f";
28
29static const tiny_palette *g_pal = &TOKYO_NIGHT;
30
31void tiny_theme_use(tiny_theme theme) {
32    // `auto` will derive the palette from the host terminal; until then it
33    // shares the TokyoNight palette so no call site branches on the theme.
34    (void)theme;
35    g_pal = &TOKYO_NIGHT;
36}
37
38const char *tiny_normal_fg(void) { return g_pal->fg; }
39const char *tiny_normal_bg(void) { return g_pal->bg; }
40const char *tiny_diff_add(void) { return DIFF_ADD; }
41const char *tiny_diff_delete(void) { return DIFF_DELETE; }
42
43// `r;g;b` for one `#rrggbb`.
44static void channels(tui_buf *b, const char *hex) {
45    unsigned r = 0, g = 0, bl = 0;
46    if (hex && hex[0] == '#' && strlen(hex) >= 7) {
47        sscanf(hex + 1, "%2x%2x%2x", &r, &g, &bl);
48    }
49    buf_addf(b, "%u;%u;%u", r, g, bl);
50}
51
52void sgr_fg(tui_buf *b, const char *hex) {
53    buf_adds(b, "\x1b[38;2;");
54    channels(b, hex);
55    buf_addc(b, 'm');
56}
57
58void sgr_bg(tui_buf *b, const char *hex) {
59    buf_adds(b, "\x1b[48;2;");
60    channels(b, hex);
61    buf_addc(b, 'm');
62}
63
64void sgr_plain(tui_buf *b) {
65    buf_adds(b, "\x1b[22;23;24;38;2;");
66    channels(b, tiny_normal_fg());
67    buf_adds(b, ";48;2;");
68    channels(b, tiny_normal_bg());
69    buf_addc(b, 'm');
70}
71
72void sgr_style(tui_buf *b, const tiny_style *s) {
73    buf_adds(b, "\x1b[");
74    if (s->bold) buf_adds(b, "1;");
75    if (s->italic) buf_adds(b, "3;");
76    if (s->underline) buf_adds(b, "4;");
77    buf_adds(b, "38;2;");
78    channels(b, s->fg ? s->fg : tiny_normal_fg());
79    buf_adds(b, ";48;2;");
80    channels(b, s->bg ? s->bg : tiny_normal_bg());
81    buf_addc(b, 'm');
82}
83
84static void fg(tui_buf *b, const char *hex, const char *text) {
85    sgr_fg(b, hex);
86    buf_adds(b, text);
87    sgr_fg(b, tiny_normal_fg());
88}
89
90void fg_dim(tui_buf *b, const char *text) { fg(b, g_pal->comment, text); }
91void fg_red(tui_buf *b, const char *text) { fg(b, g_pal->red, text); }
92void fg_green(tui_buf *b, const char *text) { fg(b, g_pal->green, text); }
93void fg_yellow(tui_buf *b, const char *text) { fg(b, g_pal->yellow, text); }
94void fg_cyan(tui_buf *b, const char *text) { fg(b, g_pal->blue, text); }
95void fg_blue(tui_buf *b, const char *text) { fg(b, g_pal->blue, text); }
96void fg_teal(tui_buf *b, const char *text) { fg(b, g_pal->teal, text); }
97
98// One entry per capture name; `fg`/`bg` reference the palette by tag so the
99// table survives a palette swap at `tiny_theme_use` time.
100typedef struct {
101    const char *name;
102    tiny_style style;
103} style_entry;
104
105#define FG(tag) tag
106static const style_entry STYLES[] = {
107    {"comment", {FG("#565f89"), NULL, false, false, false}},
108    {"constant", {FG("#ff9e64"), NULL, false, false, false}},
109    {"constant.builtin", {FG("#2ac3de"), NULL, false, false, false}},
110    {"constructor", {FG("#bb9af7"), NULL, false, false, false}},
111    {"escape", {FG("#bb9af7"), NULL, false, false, false}},
112    {"function", {FG("#7aa2f7"), NULL, false, false, false}},
113    {"function.builtin", {FG("#2ac3de"), NULL, false, false, false}},
114    {"keyword", {FG("#9d7cd8"), NULL, false, false, false}},
115    {"number", {FG("#ff9e64"), NULL, false, false, false}},
116    {"operator", {FG("#89ddff"), NULL, false, false, false}},
117    {"property", {FG("#73daca"), NULL, false, false, false}},
118    {"punctuation.bracket", {FG("#a9b1d6"), NULL, false, false, false}},
119    {"punctuation.delimiter", {FG("#89ddff"), NULL, false, false, false}},
120    {"punctuation.special", {FG("#89ddff"), NULL, false, false, false}},
121    {"string", {FG("#9ece6a"), NULL, false, false, false}},
122    {"string.special", {FG("#2ac3de"), NULL, false, false, false}},
123    {"type", {FG("#2ac3de"), NULL, false, false, false}},
124    {"type.builtin", {FG("#27a1b9"), NULL, false, false, false}},
125    {"variable", {FG("#c0caf5"), NULL, false, false, false}},
126    {"variable.builtin", {FG("#f7768e"), NULL, false, false, false}},
127    {"variable.parameter", {FG("#e0af68"), NULL, false, false, false}},
128    {"text.emphasis", {NULL, NULL, false, true, false}},
129    {"text.strong", {NULL, NULL, true, false, false}},
130    {"text.literal", {FG("#9ece6a"), NULL, false, false, false}},
131    {"text.uri", {NULL, NULL, false, false, true}},
132    {"text.reference", {FG("#2ac3de"), NULL, false, false, false}},
133    // The `@markup` names used by nvim's current markdown queries.
134    {"markup.heading", {NULL, NULL, true, false, false}},
135    {"markup.strong", {NULL, NULL, true, false, false}},
136    {"markup.emphasis", {NULL, NULL, false, true, false}},
137    {"markup.raw", {FG("#9ece6a"), NULL, false, false, false}},
138    {"markup.link", {FG("#2ac3de"), NULL, false, false, false}},
139    {"markup.link.url", {NULL, NULL, false, false, true}},
140    {"markup.list", {FG("#e0af68"), NULL, false, false, false}},
141    {"markup.quote", {FG("#565f89"), NULL, false, false, false}},
142};
143#undef FG
144
145const tiny_style *tiny_capture_style(const char *capture) {
146    for (const char *name = capture;;) {
147        for (size_t i = 0; i < sizeof STYLES / sizeof STYLES[0]; i++)
148            if (strcmp(STYLES[i].name, name) == 0) return &STYLES[i].style;
149        const char *dot = strrchr(name, '.');
150        if (!dot) return NULL;
151        static _Thread_local char buf[128];
152        size_t n = (size_t)(dot - name);
153        if (n >= sizeof buf) n = sizeof buf - 1;
154        memcpy(buf, name, n);
155        buf[n] = '\0';
156        name = buf;
157    }
158}
159
160// Headings take their level's colour from TokyoNight's rainbow, over a 10% tint.
161static const tiny_style HEADINGS[] = {
162    {"#7aa2f7", "#24293b", true, false, false},
163    {"#e0af68", "#2e2a2d", true, false, false},
164    {"#9ece6a", "#272d2d", true, false, false},
165    {"#1abc9c", "#1a2b32", true, false, false},
166    {"#bb9af7", "#2a283b", true, false, false},
167    {"#9d7cd8", "#272538", true, false, false},
168    {"#ff9e64", "#31282c", true, false, false},
169    {"#f7768e", "#302430", true, false, false},
170};
171
172const tiny_style *tiny_heading_style(int level) {
173    if (level < 1) level = 1;
174    if (level > (int)(sizeof HEADINGS / sizeof HEADINGS[0]))
175        level = (int)(sizeof HEADINGS / sizeof HEADINGS[0]);
176    return &HEADINGS[level - 1];
177}
178
179static const tiny_style INLINE_CODE = {"#7aa2f7", "#414868", false, false, false};
180const tiny_style *tiny_inline_code_style(void) { return &INLINE_CODE; }