xonecas.io
64875
Mc

mini-coder / files

mini-coder / src / tui / theme.ts

4.0 KB
1/**
2 * The one palette: the TokyoNight `night` slots the TUI uses, from
3 * `folke/tokyonight.nvim` (`lua/tokyonight/colors/night.lua` and its generated
4 * highlight groups). Every colour the TUI writes comes from here, foreground and
5 * background alike, so no row can fall back to the terminal's own colours.
6 * Truecolor only.
7 */
8export const PALETTE = {
9  bg: "#1a1b26",
10  fg: "#c0caf5",
11  fg_dark: "#a9b1d6",
12  comment: "#565f89",
13  terminal_black: "#414868",
14  blue: "#7aa2f7",
15  blue1: "#2ac3de",
16  blue5: "#89ddff",
17  green: "#9ece6a",
18  green1: "#73daca",
19  magenta: "#bb9af7",
20  orange: "#ff9e64",
21  purple: "#9d7cd8",
22  red: "#f7768e",
23  teal: "#1abc9c",
24  yellow: "#e0af68",
25} as const;
26
27/** `Normal`: the pair every row is painted with, and restored to. */
28export const NORMAL_FG = PALETTE.fg;
29export const NORMAL_BG = PALETTE.bg;
30
31/** Diff row backgrounds, from the `DiffAdd`/`DiffDelete` groups. */
32export const DIFF_ADD = "#243e4a";
33export const DIFF_DELETE = "#4a272f";
34
35/** `r;g;b` for one `#rrggbb`. */
36export function channels(hex: string): string {
37  return [1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join(";");
38}
39
40/**
41 * Explicit colour, never a reset: a `39`/`49` or a `0` would hand the rest of
42 * the row back to the host terminal, which is the leak this palette closes.
43 */
44export function sgrFg(hex: string): string {
45  return `\x1b[38;2;${channels(hex)}m`;
46}
47
48export function sgrBg(hex: string): string {
49  return `\x1b[48;2;${channels(hex)}m`;
50}
51
52/**
53 * Attributes off, then `Normal`. The state a row starts and ends in: bold, dim
54 * and the like must not bleed past the span that set them, and a reset (`0`,
55 * or a bare colour off) would drop the row back onto the host terminal.
56 */
57export function sgrPlain(): string {
58  return `\x1b[22;23;24;38;2;${channels(NORMAL_FG)};48;2;${channels(NORMAL_BG)}m`;
59}
60
61export interface Style {
62  fg?: string;
63  bg?: string;
64  bold?: boolean;
65  italic?: boolean;
66  underline?: boolean;
67}
68
69/**
70 * Capture name to colour, mapped the way `folke/tokyonight.nvim` maps capture
71 * names to highlight groups. A dotted name falls back to its parent.
72 */
73export const STYLES: Record<string, Style | undefined> = {
74  comment: { fg: PALETTE.comment },
75  constant: { fg: PALETTE.orange },
76  "constant.builtin": { fg: PALETTE.blue1 },
77  constructor: { fg: PALETTE.magenta },
78  escape: { fg: PALETTE.magenta },
79  function: { fg: PALETTE.blue },
80  "function.builtin": { fg: PALETTE.blue1 },
81  keyword: { fg: PALETTE.purple },
82  number: { fg: PALETTE.orange },
83  operator: { fg: PALETTE.blue5 },
84  property: { fg: PALETTE.green1 },
85  "punctuation.bracket": { fg: PALETTE.fg_dark },
86  "punctuation.delimiter": { fg: PALETTE.blue5 },
87  "punctuation.special": { fg: PALETTE.blue5 },
88  string: { fg: PALETTE.green },
89  "string.special": { fg: PALETTE.blue1 },
90  type: { fg: PALETTE.blue1 },
91  "type.builtin": { fg: "#27a1b9" }, // a per-variant literal, not a palette slot
92  variable: { fg: PALETTE.fg },
93  "variable.builtin": { fg: PALETTE.red },
94  "variable.parameter": { fg: PALETTE.yellow },
95  // Markdown's queries predate the `@markup` rename; these are the old names.
96  "text.emphasis": { italic: true },
97  "text.strong": { bold: true },
98  "text.literal": { fg: PALETTE.green },
99  "text.uri": { underline: true },
100  "text.reference": { fg: PALETTE.blue1 },
101};
102
103/**
104 * Headings take their level's colour from TokyoNight's rainbow, over a 10% tint
105 * of it, as `@markup.heading.N.markdown` does. The grammar's own query names one
106 * heading colour for all levels, so the level comes from the marker.
107 */
108export const HEADINGS: Style[] = [
109  { fg: PALETTE.blue, bg: "#24293b" },
110  { fg: PALETTE.yellow, bg: "#2e2a2d" },
111  { fg: PALETTE.green, bg: "#272d2d" },
112  { fg: PALETTE.teal, bg: "#1a2b32" },
113  { fg: PALETTE.magenta, bg: "#2a283b" },
114  { fg: PALETTE.purple, bg: "#272538" },
115  { fg: PALETTE.orange, bg: "#31282c" },
116  { fg: PALETTE.red, bg: "#302430" },
117].map((style) => ({ ...style, bold: true }));
118
119/** Inline code, `@markup.raw.markdown_inline`. */
120export const INLINE_CODE: Style = { fg: PALETTE.blue, bg: PALETTE.terminal_black };