@@ -1,35 +1,228 @@
1-//! The one palette: the TokyoNight `night` slots the TUI uses. Every colour the
2-//! TUI writes comes from here, foreground and background alike, so no row can
3-//! fall back to the terminal's own colours. Truecolor only.
1+//! The palettes: one per theme id. Every colour the TUI writes comes from the
2+//! active theme, foreground and background alike, so no row can fall back to
3+//! the terminal's own colours. Truecolor only.
4+//!
5+//! A theme is one `Theme` value: the slots the TUI itself paints with, then its
6+//! own capture table and its own heading ramp. The capture tables are not
7+//! shared, so a theme maps a capture the way its own upstream theme does.
8+//!
9+//! The theme is chosen once at startup and never changes: `init` runs before the
10+//! first row is painted, and nothing writes `current` again.
411
512 const std = @import("std");
613
7-pub const PALETTE = struct {
8- pub const bg = "#1a1b26";
9- pub const fg = "#c0caf5";
10- pub const fg_dark = "#a9b1d6";
11- pub const comment = "#565f89";
12- pub const terminal_black = "#414868";
13- pub const blue = "#7aa2f7";
14- pub const blue1 = "#2ac3de";
15- pub const blue5 = "#89ddff";
16- pub const green = "#9ece6a";
17- pub const green1 = "#73daca";
18- pub const magenta = "#bb9af7";
19- pub const orange = "#ff9e64";
20- pub const purple = "#9d7cd8";
21- pub const red = "#f7768e";
22- pub const teal = "#1abc9c";
23- pub const yellow = "#e0af68";
14+pub const Style = struct {
15+ fg: ?[]const u8 = null,
16+ bg: ?[]const u8 = null,
17+ bold: bool = false,
18+ italic: bool = false,
19+ underline: bool = false,
20+};
21+
22+/// One capture table entry. A dotted name falls back to its parent.
23+const Capture = struct { name: []const u8, style: Style };
24+
25+pub const Theme = struct {
26+ /// `Normal`: the pair every row is painted with, and restored to.
27+ fg: []const u8,
28+ bg: []const u8,
29+ /// `Comment`: diff context, the status row, the elision notes.
30+ comment: []const u8,
31+ /// The user's own text, a diff path and hunk header, command output.
32+ prompt: []const u8,
33+ /// The tool accent: call-line heads.
34+ accent: []const u8,
35+ /// Diff `+` lines.
36+ add: []const u8,
37+ /// Diff `-` lines, and every error.
38+ error_: []const u8,
39+ /// A near-full context window.
40+ warn: []const u8,
41+ /// Diff row backgrounds.
42+ diff_add: []const u8,
43+ diff_delete: []const u8,
44+ /// Inline code, `@markup.raw.markdown_inline`.
45+ inline_code: Style,
46+ /// One style per Markdown heading level; the last covers every deeper level.
47+ headings: []const Style,
48+ captures: []const Capture,
49+};
50+
51+// ---- TokyoNight `night` ---------------------------------------------------
52+
53+const TOKYONIGHT = Theme{
54+ .fg = "#c0caf5",
55+ .bg = "#1a1b26",
56+ .comment = "#565f89",
57+ .prompt = "#7aa2f7",
58+ .accent = "#1abc9c",
59+ .add = "#9ece6a",
60+ .error_ = "#f7768e",
61+ .warn = "#e0af68",
62+ .diff_add = "#243e4a",
63+ .diff_delete = "#4a272f",
64+ .inline_code = .{ .fg = "#7aa2f7", .bg = "#414868" },
65+ .headings = &TOKYONIGHT_HEADINGS,
66+ .captures = &TOKYONIGHT_CAPTURES,
2467 };
2568
26-/// `Normal`: the pair every row is painted with, and restored to.
27-pub const NORMAL_FG = PALETTE.fg;
28-pub const NORMAL_BG = PALETTE.bg;
69+/// Headings take their level's colour from TokyoNight's rainbow over a tint.
70+const TOKYONIGHT_HEADINGS = [_]Style{
71+ .{ .fg = "#7aa2f7", .bg = "#24293b", .bold = true },
72+ .{ .fg = "#e0af68", .bg = "#2e2a2d", .bold = true },
73+ .{ .fg = "#9ece6a", .bg = "#272d2d", .bold = true },
74+ .{ .fg = "#1abc9c", .bg = "#1a2b32", .bold = true },
75+ .{ .fg = "#bb9af7", .bg = "#2a283b", .bold = true },
76+ .{ .fg = "#9d7cd8", .bg = "#272538", .bold = true },
77+ .{ .fg = "#ff9e64", .bg = "#31282c", .bold = true },
78+ .{ .fg = "#f7768e", .bg = "#302430", .bold = true },
79+};
80+
81+const TOKYONIGHT_CAPTURES = [_]Capture{
82+ .{ .name = "boolean", .style = .{ .fg = "#ff9e64" } },
83+ .{ .name = "character", .style = .{ .fg = "#9ece6a" } },
84+ .{ .name = "comment", .style = .{ .fg = "#565f89" } },
85+ .{ .name = "constant", .style = .{ .fg = "#ff9e64" } },
86+ .{ .name = "constant.builtin", .style = .{ .fg = "#2ac3de" } },
87+ .{ .name = "constructor", .style = .{ .fg = "#bb9af7" } },
88+ .{ .name = "escape", .style = .{ .fg = "#bb9af7" } },
89+ .{ .name = "function", .style = .{ .fg = "#7aa2f7" } },
90+ .{ .name = "function.builtin", .style = .{ .fg = "#2ac3de" } },
91+ .{ .name = "keyword", .style = .{ .fg = "#9d7cd8" } },
92+ .{ .name = "label", .style = .{ .fg = "#7aa2f7" } },
93+ // `@module` is TokyoNight's `Include`, which is undefined there and falls
94+ // through to `PreProc`, a cyan the palette does not carry.
95+ .{ .name = "module", .style = .{ .fg = "#2ac3de" } },
96+ .{ .name = "number", .style = .{ .fg = "#ff9e64" } },
97+ .{ .name = "operator", .style = .{ .fg = "#89ddff" } },
98+ .{ .name = "property", .style = .{ .fg = "#73daca" } },
99+ .{ .name = "punctuation.bracket", .style = .{ .fg = "#a9b1d6" } },
100+ .{ .name = "punctuation.delimiter", .style = .{ .fg = "#89ddff" } },
101+ .{ .name = "punctuation.special", .style = .{ .fg = "#89ddff" } },
102+ .{ .name = "string", .style = .{ .fg = "#9ece6a" } },
103+ .{ .name = "string.special", .style = .{ .fg = "#2ac3de" } },
104+ .{ .name = "type", .style = .{ .fg = "#2ac3de" } },
105+ .{ .name = "type.builtin", .style = .{ .fg = "#27a1b9" } },
106+ .{ .name = "variable", .style = .{ .fg = "#c0caf5" } },
107+ .{ .name = "variable.builtin", .style = .{ .fg = "#f7768e" } },
108+ .{ .name = "variable.parameter", .style = .{ .fg = "#e0af68" } },
109+ // Markdown's queries predate the `@markup` rename; these are the old names.
110+ .{ .name = "text.emphasis", .style = .{ .italic = true } },
111+ .{ .name = "text.strong", .style = .{ .bold = true } },
112+ .{ .name = "text.literal", .style = .{ .fg = "#9ece6a" } },
113+ .{ .name = "text.title", .style = .{ .fg = "#7aa2f7", .bold = true } },
114+ .{ .name = "text.uri", .style = .{ .underline = true } },
115+ .{ .name = "text.reference", .style = .{ .fg = "#2ac3de" } },
116+};
29117
30-/// Diff row backgrounds, from the `DiffAdd`/`DiffDelete` groups.
31-pub const DIFF_ADD = "#243e4a";
32-pub const DIFF_DELETE = "#4a272f";
118+// ---- Oxocarbon ------------------------------------------------------------
119+
120+/// The greys are the upstream `blend_hex(base00, base06, n)` ramp resolved to
121+/// hex. The two diff tints are the upstream `DiffAdd` and `DiffDelete`.
122+const OXOCARBON = Theme{
123+ .fg = "#d0d0d0",
124+ .bg = "#161616",
125+ .comment = "#525252",
126+ .prompt = "#78a9ff",
127+ .accent = "#3ddbd9",
128+ .add = "#08bdba",
129+ .error_ = "#ee5396",
130+ .warn = "#be95ff",
131+ .diff_add = "#122f2f",
132+ .diff_delete = "#361c28",
133+ .inline_code = .{ .fg = "#be95ff", .bg = "#262626" },
134+ .headings = &OXOCARBON_HEADINGS,
135+ .captures = &OXOCARBON_CAPTURES,
136+};
137+
138+/// Oxocarbon paints every heading level with one colour, `markdownH1` = base10,
139+/// so the ramp is a single entry and the clamp sends every level to it.
140+const OXOCARBON_HEADINGS = [_]Style{
141+ .{ .fg = "#ee5396", .bg = "#2c1c23", .bold = true },
142+};
143+
144+const OXOCARBON_CAPTURES = [_]Capture{
145+ .{ .name = "boolean", .style = .{ .fg = "#78a9ff" } },
146+ .{ .name = "character", .style = .{ .fg = "#be95ff" } },
147+ .{ .name = "comment", .style = .{ .fg = "#525252", .italic = true } },
148+ .{ .name = "constant", .style = .{ .fg = "#be95ff" } },
149+ .{ .name = "constant.builtin", .style = .{ .fg = "#08bdba" } },
150+ .{ .name = "constructor", .style = .{ .fg = "#78a9ff" } },
151+ .{ .name = "function", .style = .{ .fg = "#ff7eb6", .bold = true } },
152+ .{ .name = "function.builtin", .style = .{ .fg = "#ff7eb6" } },
153+ .{ .name = "keyword", .style = .{ .fg = "#78a9ff" } },
154+ .{ .name = "label", .style = .{ .fg = "#82cfff" } },
155+ // `@module` is `Include` upstream, base09.
156+ .{ .name = "module", .style = .{ .fg = "#78a9ff" } },
157+ .{ .name = "number", .style = .{ .fg = "#82cfff" } },
158+ .{ .name = "operator", .style = .{ .fg = "#78a9ff" } },
159+ .{ .name = "property", .style = .{ .fg = "#ee5396" } },
160+ .{ .name = "punctuation.bracket", .style = .{ .fg = "#3ddbd9" } },
161+ .{ .name = "punctuation.delimiter", .style = .{ .fg = "#3ddbd9" } },
162+ .{ .name = "punctuation.special", .style = .{ .fg = "#3ddbd9" } },
163+ .{ .name = "string", .style = .{ .fg = "#be95ff" } },
164+ .{ .name = "string.special", .style = .{ .fg = "#be95ff" } },
165+ .{ .name = "type", .style = .{ .fg = "#78a9ff" } },
166+ .{ .name = "type.builtin", .style = .{ .fg = "#78a9ff" } },
167+ .{ .name = "variable", .style = .{ .fg = "#d0d0d0" } },
168+ .{ .name = "variable.builtin", .style = .{ .fg = "#d0d0d0" } },
169+ .{ .name = "variable.parameter", .style = .{ .fg = "#d0d0d0" } },
170+ .{ .name = "text.emphasis", .style = .{ .fg = "#ee5396", .bold = true } },
171+ .{ .name = "text.strong", .style = .{ .bold = true } },
172+ .{ .name = "text.literal", .style = .{ .fg = "#d0d0d0" } },
173+ .{ .name = "text.title", .style = .{ .fg = "#ee5396" } },
174+ .{ .name = "text.uri", .style = .{ .fg = "#be95ff", .underline = true } },
175+ .{ .name = "text.reference", .style = .{ .fg = "#d0d0d0" } },
176+};
177+
178+// ---- the active theme -----------------------------------------------------
179+
180+/// The id a config with no `theme` key gets.
181+pub const default_id = "tokyonight";
182+
183+/// The theme `id` names, or null when no theme does.
184+pub fn find(id: []const u8) ?*const Theme {
185+ if (std.mem.eql(u8, id, "tokyonight")) return &TOKYONIGHT;
186+ if (std.mem.eql(u8, id, "oxocarbon")) return &OXOCARBON;
187+ return null;
188+}
189+
190+/// The active theme. Set once by `init`; every call site reads it.
191+pub var current: *const Theme = &TOKYONIGHT;
192+
193+/// The three sequences a row starts and ends in, built from the active theme.
194+/// Comptime while the palette was fixed; runtime now, because the palette is
195+/// not known until the config is read.
196+pub var SGR_PLAIN: []const u8 = "";
197+pub var SGR_NORMAL_FG: []const u8 = "";
198+pub var SGR_NORMAL_BG: []const u8 = "";
199+
200+/// Selects the theme and bakes its sequences. Runs once, before the first row
201+/// is painted. An unknown id falls back to the default.
202+pub fn init(a: std.mem.Allocator, id: []const u8) void {
203+ current = find(id) orelse &TOKYONIGHT;
204+ const f = rgb(current.fg);
205+ const b = rgb(current.bg);
206+ SGR_PLAIN = std.fmt.allocPrint(a, "\x1b[22;23;24;38;2;{d};{d};{d};48;2;{d};{d};{d}m", .{
207+ f[0], f[1], f[2], b[0], b[1], b[2],
208+ }) catch "";
209+ SGR_NORMAL_FG = sgrFg(a, current.fg);
210+ SGR_NORMAL_BG = sgrBg(a, current.bg);
211+}
212+
213+/// A capture's style, falling back to its dotted parent; null when unmapped.
214+pub fn styleFor(capture: []const u8) ?Style {
215+ var name = capture;
216+ while (true) {
217+ for (current.captures) |entry| {
218+ if (std.mem.eql(u8, entry.name, name)) return entry.style;
219+ }
220+ const dot = std.mem.lastIndexOfScalar(u8, name, '.') orelse return null;
221+ name = name[0..dot];
222+ }
223+}
224+
225+// ---- colour helpers -------------------------------------------------------
33226
34227 /// The three channels of one `#rrggbb`.
35228 fn rgb(hex: []const u8) [3]u8 {
@@ -40,12 +233,6 @@ fn rgb(hex: []const u8) [3]u8 {
40233 };
41234 }
42235
43-/// A compile-time SGR sequence for one fixed `#rrggbb`.
44-fn sgrConst(comptime code: []const u8, comptime hex: []const u8) []const u8 {
45- const c = comptime rgb(hex);
46- return std.fmt.comptimePrint("\x1b[{s}{d};{d};{d}m", .{ code, c[0], c[1], c[2] });
47-}
48-
49236 /// Explicit colour, never a reset: a `39`/`49` or a `0` would hand the rest of
50237 /// the row back to the host terminal, which is the leak this palette closes.
51238 pub fn sgrFg(a: std.mem.Allocator, hex: []const u8) []const u8 {
@@ -58,33 +245,12 @@ pub fn sgrBg(a: std.mem.Allocator, hex: []const u8) []const u8 {
58245 return std.fmt.allocPrint(a, "\x1b[48;2;{d};{d};{d}m", .{ c[0], c[1], c[2] }) catch "";
59246 }
60247
61-/// Attributes off, then `Normal`. The state a row starts and ends in.
62-pub const SGR_PLAIN = blk: {
63- const f = rgb(NORMAL_FG);
64- const b = rgb(NORMAL_BG);
65- break :blk std.fmt.comptimePrint("\x1b[22;23;24;38;2;{d};{d};{d};48;2;{d};{d};{d}m", .{ f[0], f[1], f[2], b[0], b[1], b[2] });
66-};
67-
68-/// `Normal`'s foreground, restored after a span so no cell inherits a colour.
69-pub const SGR_NORMAL_FG = sgrConst("38;2;", NORMAL_FG);
70-
71-/// `Normal`'s row background, the tint every plain row is painted with.
72-pub const SGR_NORMAL_BG = sgrConst("48;2;", NORMAL_BG);
73-
74-pub const Style = struct {
75- fg: ?[]const u8 = null,
76- bg: ?[]const u8 = null,
77- bold: bool = false,
78- italic: bool = false,
79- underline: bool = false,
80-};
81-
82248 /// A style's own attributes, with the defaults spelled out: a span that names
83249 /// no colour must not inherit the colour of the span or token before it, and
84250 /// the fallback is the palette's `Normal`, never the terminal's own pair.
85251 pub fn sgr(a: std.mem.Allocator, style: Style) []const u8 {
86- const f = rgb(style.fg orelse NORMAL_FG);
87- const b = rgb(style.bg orelse NORMAL_BG);
252+ const f = rgb(style.fg orelse current.fg);
253+ const b = rgb(style.bg orelse current.bg);
88254 return std.fmt.allocPrint(a, "\x1b[{s}{s}{s}38;2;{d};{d};{d};48;2;{d};{d};{d}m", .{
89255 if (style.bold) "1;" else "",
90256 if (style.italic) "3;" else "",
@@ -97,69 +263,3 @@ pub fn sgr(a: std.mem.Allocator, style: Style) []const u8 {
97263 b[2],
98264 }) catch "";
99265 }
100-
101-/// Capture name to colour, mapped the way `folke/tokyonight.nvim` maps capture
102-/// names to highlight groups. A dotted name falls back to its parent.
103-const STYLES = [_]struct { name: []const u8, style: Style }{
104- .{ .name = "boolean", .style = .{ .fg = PALETTE.orange } },
105- .{ .name = "character", .style = .{ .fg = PALETTE.green } },
106- .{ .name = "comment", .style = .{ .fg = PALETTE.comment } },
107- .{ .name = "constant", .style = .{ .fg = PALETTE.orange } },
108- .{ .name = "constant.builtin", .style = .{ .fg = PALETTE.blue1 } },
109- .{ .name = "constructor", .style = .{ .fg = PALETTE.magenta } },
110- .{ .name = "escape", .style = .{ .fg = PALETTE.magenta } },
111- .{ .name = "function", .style = .{ .fg = PALETTE.blue } },
112- .{ .name = "function.builtin", .style = .{ .fg = PALETTE.blue1 } },
113- .{ .name = "keyword", .style = .{ .fg = PALETTE.purple } },
114- .{ .name = "label", .style = .{ .fg = PALETTE.blue } },
115- // `@module` is TokyoNight's `Include`, which is undefined there and falls
116- // through to `PreProc`, a cyan the palette does not carry.
117- .{ .name = "module", .style = .{ .fg = PALETTE.blue1 } },
118- .{ .name = "number", .style = .{ .fg = PALETTE.orange } },
119- .{ .name = "operator", .style = .{ .fg = PALETTE.blue5 } },
120- .{ .name = "property", .style = .{ .fg = PALETTE.green1 } },
121- .{ .name = "punctuation.bracket", .style = .{ .fg = PALETTE.fg_dark } },
122- .{ .name = "punctuation.delimiter", .style = .{ .fg = PALETTE.blue5 } },
123- .{ .name = "punctuation.special", .style = .{ .fg = PALETTE.blue5 } },
124- .{ .name = "string", .style = .{ .fg = PALETTE.green } },
125- .{ .name = "string.special", .style = .{ .fg = PALETTE.blue1 } },
126- .{ .name = "type", .style = .{ .fg = PALETTE.blue1 } },
127- .{ .name = "type.builtin", .style = .{ .fg = "#27a1b9" } },
128- .{ .name = "variable", .style = .{ .fg = PALETTE.fg } },
129- .{ .name = "variable.builtin", .style = .{ .fg = PALETTE.red } },
130- .{ .name = "variable.parameter", .style = .{ .fg = PALETTE.yellow } },
131- // Markdown's queries predate the `@markup` rename; these are the old names.
132- .{ .name = "text.emphasis", .style = .{ .italic = true } },
133- .{ .name = "text.strong", .style = .{ .bold = true } },
134- .{ .name = "text.literal", .style = .{ .fg = PALETTE.green } },
135- .{ .name = "text.title", .style = .{ .fg = PALETTE.blue, .bold = true } },
136- .{ .name = "text.uri", .style = .{ .underline = true } },
137- .{ .name = "text.reference", .style = .{ .fg = PALETTE.blue1 } },
138-};
139-
140-/// A capture's style, falling back to its dotted parent; null when unmapped.
141-pub fn styleFor(capture: []const u8) ?Style {
142- var name = capture;
143- while (true) {
144- for (STYLES) |entry| {
145- if (std.mem.eql(u8, entry.name, name)) return entry.style;
146- }
147- const dot = std.mem.lastIndexOfScalar(u8, name, '.') orelse return null;
148- name = name[0..dot];
149- }
150-}
151-
152-/// Headings take their level's colour from TokyoNight's rainbow over a tint.
153-pub const HEADINGS = [_]Style{
154- .{ .fg = PALETTE.blue, .bg = "#24293b", .bold = true },
155- .{ .fg = PALETTE.yellow, .bg = "#2e2a2d", .bold = true },
156- .{ .fg = PALETTE.green, .bg = "#272d2d", .bold = true },
157- .{ .fg = PALETTE.teal, .bg = "#1a2b32", .bold = true },
158- .{ .fg = PALETTE.magenta, .bg = "#2a283b", .bold = true },
159- .{ .fg = PALETTE.purple, .bg = "#272538", .bold = true },
160- .{ .fg = PALETTE.orange, .bg = "#31282c", .bold = true },
161- .{ .fg = PALETTE.red, .bg = "#302430", .bold = true },
162-};
163-
164-/// Inline code, `@markup.raw.markdown_inline`.
165-pub const INLINE_CODE = Style{ .fg = PALETTE.blue, .bg = PALETTE.terminal_black };