@@ -5,72 +5,16 @@ import Parser from "tree-sitter";
55 import JavaScript from "tree-sitter-javascript";
66 import TypeScript from "tree-sitter-typescript";
77 import Markdown from "@tree-sitter-grammars/tree-sitter-markdown";
8+import { HEADINGS, INLINE_CODE, NORMAL_BG, NORMAL_FG, STYLES, channels, sgrPlain, type Style } from "./theme.ts";
89
910 /**
1011 * Syntax highlighting for committed scrollback, once, over the whole block.
1112 * Grammar queries ship with the grammar packages and are consumed as data, so
12- * a capture name is the only thing that reaches the palette. Colors are the
13- * TokyoNight `moon` palette, mapped the way `folke/tokyonight.nvim` maps
14- * capture names to highlight groups. Truecolor only.
13+ * a capture name is the only thing that reaches the palette. The colours come
14+ * from the TokyoNight `night` palette in `theme.ts`, mapped the way
15+ * `folke/tokyonight.nvim` maps capture names to highlight groups. Truecolor only.
1516 */
1617
17-interface Style {
18- fg?: string;
19- bg?: string;
20- bold?: boolean;
21- italic?: boolean;
22- underline?: boolean;
23-}
24-
25-/**
26- * Headings take their level's color from TokyoNight's rainbow, over a 10%
27- * tint of it, as `@markup.heading.N.markdown` does. The grammar's own query
28- * names one heading color for all levels, so the level comes from the marker.
29- */
30-const HEADINGS: Style[] = [
31- { fg: "#82aaff", bg: "#2c314a" },
32- { fg: "#ffc777", bg: "#38343c" },
33- { fg: "#c3e88d", bg: "#32383f" },
34- { fg: "#4fd6be", bg: "#263644" },
35- { fg: "#c099ff", bg: "#32304a" },
36- { fg: "#fca7ea", bg: "#383148" },
37-].map((style) => ({ ...style, bold: true }));
38-
39-/** Inline code: Helix's `@markup.raw.markdown_inline`, not Neovim's plain one. */
40-const INLINE_CODE: Style = { fg: "#82aaff", bg: "#444a73" };
41-
42-/** Capture name to color. A dotted name falls back to its parent. */
43-const STYLES: Record<string, Style | undefined> = {
44- comment: { fg: "#636da6" },
45- constant: { fg: "#ff966c" },
46- "constant.builtin": { fg: "#65bcff" },
47- constructor: { fg: "#c099ff" },
48- escape: { fg: "#c099ff" },
49- function: { fg: "#82aaff" },
50- "function.builtin": { fg: "#65bcff" },
51- "function.method": { fg: "#82aaff" },
52- keyword: { fg: "#fca7ea" },
53- number: { fg: "#ff966c" },
54- operator: { fg: "#89ddff" },
55- property: { fg: "#4fd6be" },
56- "punctuation.bracket": { fg: "#828bb8" },
57- "punctuation.delimiter": { fg: "#89ddff" },
58- "punctuation.special": { fg: "#89ddff" },
59- string: { fg: "#c3e88d" },
60- "string.special": { fg: "#65bcff" },
61- type: { fg: "#65bcff" },
62- "type.builtin": { fg: "#589ed7" },
63- variable: { fg: "#c8d3f5" },
64- "variable.builtin": { fg: "#ff757f" },
65- "variable.parameter": { fg: "#ffc777" },
66- // Markdown's queries predate the `@markup` rename; these are the old names.
67- "text.emphasis": { italic: true },
68- "text.strong": { bold: true },
69- "text.literal": { fg: "#c3e88d" },
70- "text.uri": { underline: true },
71- "text.reference": { fg: "#65bcff" },
72-};
73-
7418 function styleFor(capture: string): Style | undefined {
7519 for (let name = capture; ; name = name.slice(0, name.lastIndexOf("."))) {
7620 const style = STYLES[name];
@@ -152,24 +96,24 @@ function parse(syntax: Syntax, text: string): Parser.SyntaxNode {
15296
15397 /**
15498 * A style's own attributes, with the defaults spelled out: a span that names no
155- * color must not inherit the color of the span or token before it.
99+ * color must not inherit the color of the span or token before it, and the
100+ * fallback is the palette's `Normal`, never the terminal's own pair.
156101 */
157102 function sgr(style: Style): string {
158- const channel = (code: number, hex: string): string =>
159- `${code};2;${[1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join(";")};`;
160103 let out = "\x1b[";
161104 if (style.bold) out += "1;";
162105 if (style.italic) out += "3;";
163106 if (style.underline) out += "4;";
164- out += style.fg === undefined ? "39;" : channel(38, style.fg);
165- out += style.bg === undefined ? "49;" : channel(48, style.bg);
166- return `${out.slice(0, -1)}m`;
107+ out += `38;2;${channels(style.fg ?? NORMAL_FG)};48;2;${channels(style.bg ?? NORMAL_BG)}m`;
108+ return out;
167109 }
168110
169111 /**
170112 * Wraps every span in its color. Spans nest, so the innermost one wins; text
171- * outside any span is reset to plain. The trailing reset closes the whole text,
172- * so a span may cross a line break and the last line still ends plain.
113+ * outside any span falls back to `Normal`. The trailing close restores the same
114+ * pair, so a span may cross a line break and the last line still ends plain —
115+ * and no cell is ever left to the terminal. Because each row is painted on its
116+ * own, a span that outlives a line break re-asserts itself on the next line.
173117 */
174118 function paint(text: string, spans: Span[]): string {
175119 const events: { at: number; open: boolean; span: Span }[] = [];
@@ -187,13 +131,14 @@ function paint(text: string, spans: Span[]): string {
187131 if (end <= at) return;
188132 const style = active.length === 0 ? undefined : active[active.length - 1].style;
189133 if (style === undefined) {
190- if (!plain) out += "\x1b[0m";
134+ if (!plain) out += sgrPlain();
191135 plain = true;
192136 } else {
193137 out += sgr(style);
194138 plain = false;
195139 }
196- out += text.slice(at, end);
140+ const chunk = text.slice(at, end);
141+ out += style === undefined ? chunk : chunk.replace(/\n/g, `\n${sgr(style)}`);
197142 at = end;
198143 };
199144 for (const event of events) {
@@ -202,7 +147,7 @@ function paint(text: string, spans: Span[]): string {
202147 else active.splice(active.indexOf(event.span), 1);
203148 }
204149 emit(text.length);
205- return plain ? out : `${out}\x1b[0m`;
150+ return plain ? out : `${out}${sgrPlain()}`;
206151 }
207152
208153 /**