Artifact ce036e5e716c149a43d6fa0b449b7c7d7f3c88589c1f30def0696c38b26c5d00:
- File
src/tui/theme.h
— part of check-in
[f99ea5c3ff]
at
2026-11-22 11:08:00
on branch ghostty-fixes
— tui: consume terminal replies, pick light or dark from OSC 11, signal when a reply is ready
The decoder consumes OSC, DCS, APC, PM and SOS replies while a probe is outstanding and every private CSI reply (DA1, DA2, DECRQM, kitty flags, colour scheme reports) always, so an answer never reaches the editor as an Escape and typed text. At start pic asks for the background colour and DA1, waits up to 150 ms for DA1, and keeps the reply window open for a second; the answer decides system and light/dark themes ahead of COLORFGBG. Focus reports (DECSET 1004) gate a BEL plus OSC 9 (OSC 777 in foot and VTE) when a turn ends; the notifyWhenDone setting turns it off. (user: dmestas, size: 5496) [annotate] [blame] [check-ins using] [more...]
/* Pi's theme model (theme.c): every colour token Pi defines, theme files in * Pi's JSON schema (vars, colours, okhsl/oklch/hex/256-index/"" values), the * built-in dark and light themes, and the SGR sequences the TUI draws with. * The current theme is process-global (the TUI is single-threaded): tui_fg * and tui_bg return strings that stay valid until the theme or the colour * mode changes. Until tui_theme_use is called the current theme follows the * environment: Pi's dark or light by COLORFGBG, in truecolor when COLORTERM * says so, else 256 colours. */ #ifndef PIC_TUI_THEME_H #define PIC_TUI_THEME_H #include <stddef.h> #include "highlight.h" /* Pi's THEME_TOKENS, in its order. S marks the foreground/background slot. */ #define TUI_TOKENS(X) \ X(ACCENT, accent) X(BORDER, border) X(BORDER_ACCENT, borderAccent) X(BORDER_MUTED, borderMuted) \ X(SUCCESS, success) X(ERROR, error) X(WARNING, warning) X(MUTED, muted) X(DIM, dim) X(TEXT, text) \ X(THINKING_TEXT, thinkingText) X(SCROLLBAR_TRACK, scrollbarTrack) X(SCROLLBAR_THUMB, scrollbarThumb) \ X(SELECTED_BG, selectedBg) X(SEARCH_MATCH_BG, searchMatchBg) X(SEARCH_MATCH_TEXT, searchMatchText) \ X(USER_MESSAGE_BG, userMessageBg) X(USER_MESSAGE_TEXT, userMessageText) \ X(CUSTOM_MESSAGE_BG, customMessageBg) X(CUSTOM_MESSAGE_TEXT, customMessageText) \ X(CUSTOM_MESSAGE_LABEL, customMessageLabel) X(TOOL_PENDING_BG, toolPendingBg) \ X(TOOL_SUCCESS_BG, toolSuccessBg) X(TOOL_ERROR_BG, toolErrorBg) X(TOOL_TITLE, toolTitle) \ X(TOOL_OUTPUT, toolOutput) X(MD_HEADING, mdHeading) X(MD_LINK, mdLink) X(MD_LINK_URL, mdLinkUrl) \ X(MD_CODE, mdCode) X(MD_CODE_BLOCK, mdCodeBlock) X(MD_CODE_BLOCK_BORDER, mdCodeBlockBorder) \ X(MD_QUOTE, mdQuote) X(MD_QUOTE_BORDER, mdQuoteBorder) X(MD_HR, mdHr) X(MD_LIST_BULLET, mdListBullet) \ X(TOOL_DIFF_ADDED, toolDiffAdded) X(TOOL_DIFF_REMOVED, toolDiffRemoved) \ X(TOOL_DIFF_CONTEXT, toolDiffContext) X(SYNTAX_COMMENT, syntaxComment) X(SYNTAX_KEYWORD, syntaxKeyword) \ X(SYNTAX_FUNCTION, syntaxFunction) X(SYNTAX_VARIABLE, syntaxVariable) X(SYNTAX_STRING, syntaxString) \ X(SYNTAX_NUMBER, syntaxNumber) X(SYNTAX_TYPE, syntaxType) X(SYNTAX_OPERATOR, syntaxOperator) \ X(SYNTAX_PUNCTUATION, syntaxPunctuation) X(THINKING_OFF, thinkingOff) X(THINKING_MINIMAL, thinkingMinimal) \ X(THINKING_LOW, thinkingLow) X(THINKING_MEDIUM, thinkingMedium) X(THINKING_HIGH, thinkingHigh) \ X(THINKING_XHIGH, thinkingXhigh) X(THINKING_MAX, thinkingMax) X(BASH_MODE, bashMode) typedef enum { #define X(id, name) TUI_T_##id, TUI_TOKENS(X) #undef X TUI_T_N } tui_token; /* A resolved colour: the terminal's default (""), an sRGB triple, or a 256-colour palette index. */ typedef struct { unsigned char kind; /* TUI_COLOR_* */ unsigned char r, g, b; int index; } tui_color; enum { TUI_COLOR_DEFAULT, TUI_COLOR_RGB, TUI_COLOR_INDEX }; typedef struct { char name[64]; char path[512]; /* the file it came from; "" for the built-in themes */ int appearance; /* -1 not declared, 0 dark, 1 light */ tui_color c[TUI_T_N]; } tui_theme; /* A token's name in theme files ("userMessageBg"), or NULL when out of range; and the reverse (-1). */ const char *tui_token_name(tui_token t); int tui_token_index(const char *name); /* sRGB (0-255) of OKHSL: hue in degrees, saturation and lightness 0-1, as Pi's okhslToRgb. */ void tui_okhsl_to_rgb(double h, double s, double l, int *r, int *g, int *b); /* Parses one colour value ("#0af", "oklch(62% 0.1 200)", "okhsl(250 60% 55%)"; "" is the default), as Pi's * parseColor. 0, or -1 with a message in err. */ int tui_color_parse(const char *s, tui_color *out, char *err, size_t errcap); /* A theme file's text (Pi's schema), label naming it in messages. 0, or -1 with a message in err that names * every problem as Pi does ("Invalid theme ...: Missing required color tokens: ..."). */ int tui_theme_parse(const char *json, size_t n, const char *label, tui_theme *out, char *err, size_t errcap); /* Pi's built-in "dark" or "light": 0, or -1 for another name. */ int tui_theme_builtin(const char *name, tui_theme *out); /* Makes T the current theme; truecolor 0 renders in 256 colours. */ void tui_theme_use(const tui_theme *t, int truecolor); /* Back to following the environment (COLORFGBG, COLORTERM). */ void tui_theme_follow_env(void); const tui_theme *tui_theme_current(void); /* Pi's dark/light decision from COLORFGBG ("fg;bg", "fg;xpm;bg"): 1 light, 0 dark; env NULL reads the environment. */ int tui_terminal_light(const char *colorfgbg); /* What the terminal answered about its background (OSC 11): 1 light, 0 dark, -1 no answer. While known it * decides tui_terminal_light(NULL) ahead of COLORFGBG. */ void tui_terminal_light_set(int light); int tui_terminal_truecolor(void); /* The SGR sequence setting token T as the foreground, or as the background (the other slot's tokens work * in either). "" tokens give the default-colour sequence (ESC[39m, ESC[49m). */ const char *tui_fg(tui_token t); const char *tui_bg(tui_token t); /* The sequence for a colour in 256 colours, as Pi's rgbToAnsi256 chooses (truecolor is "38;2;r;g;b"). */ int tui_rgb_to_256(int r, int g, int b); /* Pi's editor border token for a thinking level ("off", "minimal", ... "max"); other names are thinkingOff. */ tui_token tui_thinking_token(const char *level); /* The current theme's syntax colours for tui_highlight. */ void tui_hl_theme_current(tui_hl_theme *out); #endif