Skip to content
pi-c Artifact [a892c13eeb]
Login

Artifact [a892c13eeb]

Artifact a892c13eeb78ec685cb210130cc6f77fb295dfec226eea9b0c9e37a645bd25c1:


/* 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);
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