@diegopetrucci/pi-annotate-last-message 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/session.ts CHANGED
@@ -1,112 +1,117 @@
1
1
  import type { SessionEntry, SessionMessageEntry } from "@earendil-works/pi-coding-agent";
2
2
 
3
- import type { LastAssistantMessageData, LastAssistantMessageLookupResult, LastAssistantMessageSection } from "./types.js";
3
+ import type {
4
+ LastAssistantMessageData,
5
+ LastAssistantMessageLookupResult,
6
+ LastAssistantMessageSection,
7
+ } from "./types.js";
4
8
 
5
9
  const SECTION_PREVIEW_LIMIT = 96;
6
10
 
7
11
  function truncatePreview(value: string, limit = SECTION_PREVIEW_LIMIT): string {
8
- const normalized = value.replace(/\s+/g, " ").trim();
9
- if (normalized.length <= limit) return normalized;
10
- return `${normalized.slice(0, Math.max(0, limit - 1)).trimEnd()}…`;
12
+ const normalized = value.replace(/\s+/g, " ").trim();
13
+ if (normalized.length <= limit) return normalized;
14
+ return `${normalized.slice(0, Math.max(0, limit - 1)).trimEnd()}…`;
11
15
  }
12
16
 
13
17
  function assistantMessageText(
14
- messageEntry: SessionMessageEntry,
18
+ messageEntry: SessionMessageEntry,
15
19
  ): { kind: "assistant"; text: string } | { kind: "incomplete"; stopReason: string } | null {
16
- const { message } = messageEntry;
17
- if (!("role" in message) || message.role !== "assistant") {
18
- return null;
19
- }
20
- if (message.stopReason !== "stop") {
21
- return { kind: "incomplete", stopReason: String(message.stopReason) };
22
- }
23
- if (!Array.isArray(message.content)) {
24
- return { kind: "assistant", text: "" };
25
- }
26
- return {
27
- kind: "assistant",
28
- text: message.content
29
- .filter((part): part is { type: "text"; text: string } => part.type === "text" && typeof part.text === "string")
30
- .map((part) => part.text)
31
- .join("\n")
32
- .replace(/\r\n?/g, "\n"),
33
- };
20
+ const { message } = messageEntry;
21
+ if (!("role" in message) || message.role !== "assistant") {
22
+ return null;
23
+ }
24
+ if (message.stopReason !== "stop") {
25
+ return { kind: "incomplete", stopReason: String(message.stopReason) };
26
+ }
27
+ if (!Array.isArray(message.content)) {
28
+ return { kind: "assistant", text: "" };
29
+ }
30
+ return {
31
+ kind: "assistant",
32
+ text: message.content
33
+ .filter(
34
+ (part): part is { type: "text"; text: string } =>
35
+ part.type === "text" && typeof part.text === "string",
36
+ )
37
+ .map((part) => part.text)
38
+ .join("\n")
39
+ .replace(/\r\n?/g, "\n"),
40
+ };
34
41
  }
35
42
 
36
43
  function buildSections(lines: string[]): LastAssistantMessageSection[] {
37
- const sections: LastAssistantMessageSection[] = [];
38
- let startIndex: number | null = null;
44
+ const sections: LastAssistantMessageSection[] = [];
45
+ let startIndex: number | null = null;
39
46
 
40
- const flushSection = (endIndex: number): void => {
41
- if (startIndex == null) return;
42
- const sectionLines = lines.slice(startIndex, endIndex + 1);
43
- const previewSource = sectionLines.find((line) => line.trim().length > 0) ?? "";
44
- sections.push({
45
- id: `section-${sections.length + 1}`,
46
- index: sections.length + 1,
47
- startLine: startIndex + 1,
48
- endLine: endIndex + 1,
49
- preview: truncatePreview(previewSource),
50
- text: sectionLines.join("\n"),
51
- });
52
- startIndex = null;
53
- };
47
+ const flushSection = (endIndex: number): void => {
48
+ if (startIndex == null) return;
49
+ const sectionLines = lines.slice(startIndex, endIndex + 1);
50
+ const previewSource = sectionLines.find((line) => line.trim().length > 0) ?? "";
51
+ sections.push({
52
+ id: `section-${sections.length + 1}`,
53
+ index: sections.length + 1,
54
+ startLine: startIndex + 1,
55
+ endLine: endIndex + 1,
56
+ preview: truncatePreview(previewSource),
57
+ });
58
+ startIndex = null;
59
+ };
54
60
 
55
- for (let index = 0; index < lines.length; index += 1) {
56
- const line = lines[index];
57
- if (line.trim().length === 0) {
58
- flushSection(index - 1);
59
- continue;
60
- }
61
- if (startIndex == null) {
62
- startIndex = index;
63
- }
64
- }
61
+ for (let index = 0; index < lines.length; index += 1) {
62
+ const line = lines[index];
63
+ if (line.trim().length === 0) {
64
+ flushSection(index - 1);
65
+ continue;
66
+ }
67
+ if (startIndex == null) {
68
+ startIndex = index;
69
+ }
70
+ }
65
71
 
66
- flushSection(lines.length - 1);
67
- return sections;
72
+ flushSection(lines.length - 1);
73
+ return sections;
68
74
  }
69
75
 
70
76
  export function findLastAssistantMessage(branch: SessionEntry[]): LastAssistantMessageLookupResult {
71
- for (let index = branch.length - 1; index >= 0; index -= 1) {
72
- const entry = branch[index];
73
- if (entry.type !== "message") {
74
- continue;
75
- }
76
- const extracted = assistantMessageText(entry);
77
- if (extracted == null) {
78
- continue;
79
- }
80
- if (extracted.kind === "incomplete") {
81
- return {
82
- ok: false,
83
- code: "incomplete",
84
- message: `Latest assistant message is incomplete (${extracted.stopReason}). Wait for it to finish, then try again.`,
85
- };
86
- }
77
+ for (let index = branch.length - 1; index >= 0; index -= 1) {
78
+ const entry = branch[index];
79
+ if (entry.type !== "message") {
80
+ continue;
81
+ }
82
+ const extracted = assistantMessageText(entry);
83
+ if (extracted == null) {
84
+ continue;
85
+ }
86
+ if (extracted.kind === "incomplete") {
87
+ return {
88
+ ok: false,
89
+ code: "incomplete",
90
+ message: `Latest assistant message is incomplete (${extracted.stopReason}). Wait for it to finish, then try again.`,
91
+ };
92
+ }
87
93
 
88
- const text = extracted.text;
89
- const trimmed = text.trim();
90
- if (trimmed.length === 0) {
91
- return {
92
- ok: false,
93
- code: "empty",
94
- message: "Latest assistant message has no text to annotate.",
95
- };
96
- }
94
+ const text = extracted.text;
95
+ const trimmed = text.trim();
96
+ if (trimmed.length === 0) {
97
+ return {
98
+ ok: false,
99
+ code: "empty",
100
+ message: "Latest assistant message has no text to annotate.",
101
+ };
102
+ }
97
103
 
98
- const lines = text.split("\n");
99
- const data: LastAssistantMessageData = {
100
- text,
101
- lines: lines.map((line, lineIndex) => ({ number: lineIndex + 1, text: line })),
102
- sections: buildSections(lines),
103
- };
104
- return { ok: true, data };
105
- }
104
+ const lines = text.split("\n");
105
+ const data: LastAssistantMessageData = {
106
+ lines: lines.map((line, lineIndex) => ({ number: lineIndex + 1, text: line })),
107
+ sections: buildSections(lines),
108
+ };
109
+ return { ok: true, data };
110
+ }
106
111
 
107
- return {
108
- ok: false,
109
- code: "missing",
110
- message: "No assistant messages found on the current session branch.",
111
- };
112
+ return {
113
+ ok: false,
114
+ code: "missing",
115
+ message: "No assistant messages found on the current session branch.",
116
+ };
112
117
  }
package/theme.ts CHANGED
@@ -1,8 +1,4 @@
1
- import type {
2
- Theme,
3
- ThemeBg,
4
- ThemeColor,
5
- } from "@earendil-works/pi-coding-agent";
1
+ import type { Theme, ThemeBg, ThemeColor } from "@earendil-works/pi-coding-agent";
6
2
 
7
3
  /**
8
4
  * The small public part of Pi's active theme used by the native annotation UI.
@@ -11,209 +7,219 @@ import type {
11
7
  export type AnnotateTheme = Pick<Theme, "appearance" | "colors" | "fg" | "bg">;
12
8
 
13
9
  const DARK_FALLBACK: Readonly<Record<string, string>> = {
14
- "--color-scheme": "dark",
15
- "--bg": "#0d1117",
16
- "--panel": "#151b23",
17
- "--panel-hover": "#1f242c",
18
- "--inset": "#0b1118",
19
- "--border": "#30363d",
20
- "--border-muted": "#21262d",
21
- "--text": "#f0f6fc",
22
- "--muted": "#8a8a8a",
23
- "--dim": "#585858",
24
- "--accent": "#f4c95d",
25
- "--success": "#7ee787",
26
- "--error": "#ffb3b3",
27
- "--warning": "#f4c95d",
28
- "--mdHeading": "#f4c95d",
29
- "--mdLink": "#7dd3fc",
30
- "--mdLinkUrl": "#8a8a8a",
31
- "--mdCode": "#9b7bff",
32
- "--mdCodeBlock": "inherit",
33
- "--mdCodeBlockBorder": "#6f42c1",
34
- "--mdQuote": "#8a8a8a",
35
- "--mdQuoteBorder": "#6f42c1",
36
- "--mdHr": "#585858",
37
- "--mdListBullet": "#f4c95d",
38
- "--mdBold": "inherit",
39
- "--mdItalic": "inherit",
10
+ "--color-scheme": "dark",
11
+ "--bg": "#0d1117",
12
+ "--panel": "#151b23",
13
+ "--panel-hover": "#1f242c",
14
+ "--inset": "#0b1118",
15
+ "--border": "#30363d",
16
+ "--border-muted": "#21262d",
17
+ "--text": "#f0f6fc",
18
+ "--muted": "#8a8a8a",
19
+ "--dim": "#585858",
20
+ "--accent": "#f4c95d",
21
+ "--success": "#7ee787",
22
+ "--error": "#ffb3b3",
23
+ "--warning": "#f4c95d",
24
+ "--mdHeading": "#f4c95d",
25
+ "--mdLink": "#7dd3fc",
26
+ "--mdLinkUrl": "#8a8a8a",
27
+ "--mdCode": "#9b7bff",
28
+ "--mdCodeBlock": "inherit",
29
+ "--mdCodeBlockBorder": "#6f42c1",
30
+ "--mdQuote": "#8a8a8a",
31
+ "--mdQuoteBorder": "#6f42c1",
32
+ "--mdHr": "#585858",
33
+ "--mdListBullet": "#f4c95d",
34
+ "--mdBold": "inherit",
35
+ "--mdItalic": "inherit",
40
36
  };
41
37
 
42
38
  const LIGHT_FALLBACK: Readonly<Record<string, string>> = {
43
- "--color-scheme": "light",
44
- "--bg": "#f6f8fa",
45
- "--panel": "#ffffff",
46
- "--panel-hover": "#f3f4f6",
47
- "--inset": "#f6f8fa",
48
- "--border": "#d0d7de",
49
- "--border-muted": "#afb8c1",
50
- "--text": "#1f2328",
51
- "--muted": "#57606a",
52
- "--dim": "#6e7781",
53
- "--accent": "#8250df",
54
- "--success": "#1a7f37",
55
- "--error": "#cf222e",
56
- "--warning": "#9a6700",
57
- "--mdHeading": "#9a6700",
58
- "--mdLink": "#0969da",
59
- "--mdLinkUrl": "#57606a",
60
- "--mdCode": "#8250df",
61
- "--mdCodeBlock": "#1a7f37",
62
- "--mdCodeBlockBorder": "#6e7781",
63
- "--mdQuote": "#57606a",
64
- "--mdQuoteBorder": "#6e7781",
65
- "--mdHr": "#8c959f",
66
- "--mdListBullet": "#8250df",
67
- "--mdBold": "inherit",
68
- "--mdItalic": "inherit",
39
+ "--color-scheme": "light",
40
+ "--bg": "#f6f8fa",
41
+ "--panel": "#ffffff",
42
+ "--panel-hover": "#f3f4f6",
43
+ "--inset": "#f6f8fa",
44
+ "--border": "#d0d7de",
45
+ "--border-muted": "#afb8c1",
46
+ "--text": "#1f2328",
47
+ "--muted": "#57606a",
48
+ "--dim": "#6e7781",
49
+ "--accent": "#8250df",
50
+ "--success": "#1a7f37",
51
+ "--error": "#cf222e",
52
+ "--warning": "#9a6700",
53
+ "--mdHeading": "#9a6700",
54
+ "--mdLink": "#0969da",
55
+ "--mdLinkUrl": "#57606a",
56
+ "--mdCode": "#8250df",
57
+ "--mdCodeBlock": "#1a7f37",
58
+ "--mdCodeBlockBorder": "#6e7781",
59
+ "--mdQuote": "#57606a",
60
+ "--mdQuoteBorder": "#6e7781",
61
+ "--mdHr": "#8c959f",
62
+ "--mdListBullet": "#8250df",
63
+ "--mdBold": "inherit",
64
+ "--mdItalic": "inherit",
69
65
  };
70
66
 
71
67
  const BASIC_ANSI_COLORS: Readonly<Record<number, string>> = {
72
- 0: "#000000",
73
- 1: "#800000",
74
- 2: "#008000",
75
- 3: "#808000",
76
- 4: "#000080",
77
- 5: "#800080",
78
- 6: "#008080",
79
- 7: "#c0c0c0",
80
- 8: "#808080",
81
- 9: "#ff0000",
82
- 10: "#00ff00",
83
- 11: "#ffff00",
84
- 12: "#0000ff",
85
- 13: "#ff00ff",
86
- 14: "#00ffff",
87
- 15: "#ffffff",
68
+ 0: "#000000",
69
+ 1: "#800000",
70
+ 2: "#008000",
71
+ 3: "#808000",
72
+ 4: "#000080",
73
+ 5: "#800080",
74
+ 6: "#008080",
75
+ 7: "#c0c0c0",
76
+ 8: "#808080",
77
+ 9: "#ff0000",
78
+ 10: "#00ff00",
79
+ 11: "#ffff00",
80
+ 12: "#0000ff",
81
+ 13: "#ff00ff",
82
+ 14: "#00ffff",
83
+ 15: "#ffffff",
88
84
  };
89
85
 
90
86
  const CUBE_LEVELS = [0, 95, 135, 175, 215, 255] as const;
91
87
  const SENTINEL = "X";
92
88
 
93
89
  const FOREGROUND_THEME_VARS: ReadonlyArray<{ token: ThemeColor; cssVar: string }> = [
94
- { token: "text", cssVar: "--text" },
95
- { token: "muted", cssVar: "--muted" },
96
- { token: "dim", cssVar: "--dim" },
97
- { token: "accent", cssVar: "--accent" },
98
- { token: "success", cssVar: "--success" },
99
- { token: "error", cssVar: "--error" },
100
- { token: "warning", cssVar: "--warning" },
101
- { token: "mdHeading", cssVar: "--mdHeading" },
102
- { token: "mdLink", cssVar: "--mdLink" },
103
- { token: "mdLinkUrl", cssVar: "--mdLinkUrl" },
104
- { token: "mdCode", cssVar: "--mdCode" },
105
- { token: "mdCodeBlock", cssVar: "--mdCodeBlock" },
106
- { token: "mdCodeBlockBorder", cssVar: "--mdCodeBlockBorder" },
107
- { token: "mdQuote", cssVar: "--mdQuote" },
108
- { token: "mdQuoteBorder", cssVar: "--mdQuoteBorder" },
109
- { token: "mdHr", cssVar: "--mdHr" },
110
- { token: "mdListBullet", cssVar: "--mdListBullet" },
111
- { token: "border", cssVar: "--border" },
112
- { token: "borderMuted", cssVar: "--border-muted" },
90
+ { token: "text", cssVar: "--text" },
91
+ { token: "muted", cssVar: "--muted" },
92
+ { token: "dim", cssVar: "--dim" },
93
+ { token: "accent", cssVar: "--accent" },
94
+ { token: "success", cssVar: "--success" },
95
+ { token: "error", cssVar: "--error" },
96
+ { token: "warning", cssVar: "--warning" },
97
+ { token: "mdHeading", cssVar: "--mdHeading" },
98
+ { token: "mdLink", cssVar: "--mdLink" },
99
+ { token: "mdLinkUrl", cssVar: "--mdLinkUrl" },
100
+ { token: "mdCode", cssVar: "--mdCode" },
101
+ { token: "mdCodeBlock", cssVar: "--mdCodeBlock" },
102
+ { token: "mdCodeBlockBorder", cssVar: "--mdCodeBlockBorder" },
103
+ { token: "mdQuote", cssVar: "--mdQuote" },
104
+ { token: "mdQuoteBorder", cssVar: "--mdQuoteBorder" },
105
+ { token: "mdHr", cssVar: "--mdHr" },
106
+ { token: "mdListBullet", cssVar: "--mdListBullet" },
107
+ { token: "border", cssVar: "--border" },
108
+ { token: "borderMuted", cssVar: "--border-muted" },
113
109
  ];
114
110
 
115
111
  const BACKGROUND_THEME_VARS: ReadonlyArray<{ token: ThemeBg; cssVar: string }> = [
116
- { token: "customMessageBg", cssVar: "--panel" },
117
- { token: "selectedBg", cssVar: "--panel-hover" },
118
- { token: "toolPendingBg", cssVar: "--inset" },
112
+ { token: "customMessageBg", cssVar: "--panel" },
113
+ { token: "selectedBg", cssVar: "--panel-hover" },
114
+ { token: "toolPendingBg", cssVar: "--inset" },
119
115
  ];
120
116
 
121
117
  function toHex(value: number): string {
122
- const clamped = Math.min(255, Math.max(0, Math.round(value)));
123
- return clamped.toString(16).padStart(2, "0");
118
+ const clamped = Math.min(255, Math.max(0, Math.round(value)));
119
+ return clamped.toString(16).padStart(2, "0");
124
120
  }
125
121
 
126
122
  function xterm256ToHex(index: number): string | null {
127
- if (!Number.isInteger(index) || index < 0 || index > 255) return null;
128
- if (index < 16) return BASIC_ANSI_COLORS[index] ?? null;
129
- if (index >= 232) {
130
- const value = 8 + (index - 232) * 10;
131
- const hex = toHex(value);
132
- return `#${hex}${hex}${hex}`;
133
- }
134
- const cubeIndex = index - 16;
135
- const blue = cubeIndex % 6;
136
- const green = Math.floor(cubeIndex / 6) % 6;
137
- const red = Math.floor(cubeIndex / 36) % 6;
138
- return `#${toHex(CUBE_LEVELS[red])}${toHex(CUBE_LEVELS[green])}${toHex(CUBE_LEVELS[blue])}`;
123
+ if (!Number.isInteger(index) || index < 0 || index > 255) return null;
124
+ if (index < 16) return BASIC_ANSI_COLORS[index] ?? null;
125
+ if (index >= 232) {
126
+ const value = 8 + (index - 232) * 10;
127
+ const hex = toHex(value);
128
+ return `#${hex}${hex}${hex}`;
129
+ }
130
+ const cubeIndex = index - 16;
131
+ const blue = cubeIndex % 6;
132
+ const green = Math.floor(cubeIndex / 6) % 6;
133
+ const red = Math.floor(cubeIndex / 36) % 6;
134
+ return `#${toHex(CUBE_LEVELS[red])}${toHex(CUBE_LEVELS[green])}${toHex(CUBE_LEVELS[blue])}`;
139
135
  }
140
136
 
141
137
  function colorValueToHex(value: unknown): string | null {
142
- if (typeof value !== "object" || value == null) return null;
143
- const color = value as { kind?: unknown; r?: unknown; g?: unknown; b?: unknown; index?: unknown };
144
- if (color.kind === "rgb") {
145
- const channels = [color.r, color.g, color.b];
146
- if (!channels.every((channel) => typeof channel === "number" && Number.isFinite(channel) && channel >= 0 && channel <= 255)) {
147
- return null;
148
- }
149
- return `#${toHex(color.r as number)}${toHex(color.g as number)}${toHex(color.b as number)}`;
150
- }
151
- if (color.kind === "indexed" && typeof color.index === "number") {
152
- return xterm256ToHex(color.index);
153
- }
154
- // OKLCH values are intentionally handled through theme.fg/theme.bg below.
155
- // Those public helpers account for Pi's configured terminal color mode.
156
- return null;
138
+ if (typeof value !== "object" || value == null) return null;
139
+ const color = value as { kind?: unknown; r?: unknown; g?: unknown; b?: unknown; index?: unknown };
140
+ if (color.kind === "rgb") {
141
+ const channels = [color.r, color.g, color.b];
142
+ if (
143
+ !channels.every(
144
+ (channel) =>
145
+ typeof channel === "number" && Number.isFinite(channel) && channel >= 0 && channel <= 255,
146
+ )
147
+ ) {
148
+ return null;
149
+ }
150
+ return `#${toHex(color.r as number)}${toHex(color.g as number)}${toHex(color.b as number)}`;
151
+ }
152
+ if (color.kind === "indexed" && typeof color.index === "number") {
153
+ return xterm256ToHex(color.index);
154
+ }
155
+ // OKLCH values are intentionally handled through theme.fg/theme.bg below.
156
+ // Those public helpers account for Pi's configured terminal color mode.
157
+ return null;
157
158
  }
158
159
 
159
160
  function ansiColorToHex(value: unknown, background: boolean): string | null {
160
- if (typeof value !== "string") return null;
161
- const match = new RegExp(`^\\u001b\\[([0-9;]+)m`).exec(value);
162
- if (!match) return null;
163
- const params = match[1].split(";").map(Number);
164
- const [mode, kind, first, second, third] = params;
165
- const expectedMode = background ? 48 : 38;
166
- if (mode !== expectedMode) {
167
- if (!background && ((mode >= 30 && mode <= 37) || (mode >= 90 && mode <= 97))) {
168
- return xterm256ToHex(mode >= 90 ? mode - 82 : mode - 30);
169
- }
170
- if (background && ((mode >= 40 && mode <= 47) || (mode >= 100 && mode <= 107))) {
171
- return xterm256ToHex(mode >= 100 ? mode - 92 : mode - 40);
172
- }
173
- return null;
174
- }
175
- if (kind === 2 && params.length >= 5) {
176
- const channels = [first, second, third];
177
- if (!channels.every((channel) => Number.isInteger(channel) && channel >= 0 && channel <= 255)) return null;
178
- return `#${toHex(first)}${toHex(second)}${toHex(third)}`;
179
- }
180
- if (kind === 5 && params.length >= 3) return xterm256ToHex(first);
181
- return null;
161
+ if (typeof value !== "string") return null;
162
+ const match = new RegExp(`^\\u001b\\[([0-9;]+)m`).exec(value);
163
+ if (!match) return null;
164
+ const params = match[1].split(";").map(Number);
165
+ const [mode, kind, first, second, third] = params;
166
+ const expectedMode = background ? 48 : 38;
167
+ if (mode !== expectedMode) {
168
+ if (!background && ((mode >= 30 && mode <= 37) || (mode >= 90 && mode <= 97))) {
169
+ return xterm256ToHex(mode >= 90 ? mode - 82 : mode - 30);
170
+ }
171
+ if (background && ((mode >= 40 && mode <= 47) || (mode >= 100 && mode <= 107))) {
172
+ return xterm256ToHex(mode >= 100 ? mode - 92 : mode - 40);
173
+ }
174
+ return null;
175
+ }
176
+ if (kind === 2 && params.length >= 5) {
177
+ const channels = [first, second, third];
178
+ if (!channels.every((channel) => Number.isInteger(channel) && channel >= 0 && channel <= 255))
179
+ return null;
180
+ return `#${toHex(first)}${toHex(second)}${toHex(third)}`;
181
+ }
182
+ if (kind === 5 && params.length >= 3) return xterm256ToHex(first);
183
+ return null;
182
184
  }
183
185
 
184
- function readThemeColor(theme: AnnotateTheme, token: ThemeColor | ThemeBg, background: boolean): string | null {
185
- let colors: Readonly<Record<string, unknown>> | undefined;
186
- try {
187
- colors = theme.colors as Readonly<Record<string, unknown>>;
188
- } catch {
189
- colors = undefined;
190
- }
191
-
192
- let colorValue: unknown;
193
- try {
194
- colorValue = colors?.[token];
195
- } catch {
196
- colorValue = undefined;
197
- }
198
- const fromColors = colorValueToHex(colorValue);
199
- if (fromColors != null) return fromColors;
200
-
201
- try {
202
- const styled = background
203
- ? theme.bg(token as ThemeBg, SENTINEL)
204
- : theme.fg(token as ThemeColor, SENTINEL);
205
- return ansiColorToHex(styled, background);
206
- } catch {
207
- return null;
208
- }
186
+ function readThemeColor(
187
+ theme: AnnotateTheme,
188
+ token: ThemeColor | ThemeBg,
189
+ background: boolean,
190
+ ): string | null {
191
+ let colors: Readonly<Record<string, unknown>> | undefined;
192
+ try {
193
+ colors = theme.colors as Readonly<Record<string, unknown>>;
194
+ } catch {
195
+ colors = undefined;
196
+ }
197
+
198
+ let colorValue: unknown;
199
+ try {
200
+ colorValue = colors?.[token];
201
+ } catch {
202
+ colorValue = undefined;
203
+ }
204
+ const fromColors = colorValueToHex(colorValue);
205
+ if (fromColors != null) return fromColors;
206
+
207
+ try {
208
+ const styled = background
209
+ ? theme.bg(token as ThemeBg, SENTINEL)
210
+ : theme.fg(token as ThemeColor, SENTINEL);
211
+ return ansiColorToHex(styled, background);
212
+ } catch {
213
+ return null;
214
+ }
209
215
  }
210
216
 
211
217
  function readAppearance(theme: AnnotateTheme | null | undefined): "dark" | "light" {
212
- try {
213
- return theme?.appearance === "light" ? "light" : "dark";
214
- } catch {
215
- return "dark";
216
- }
218
+ try {
219
+ return theme?.appearance === "light" ? "light" : "dark";
220
+ } catch {
221
+ return "dark";
222
+ }
217
223
  }
218
224
 
219
225
  /**
@@ -224,24 +230,24 @@ function readAppearance(theme: AnnotateTheme | null | undefined): "dark" | "ligh
224
230
  * native annotation window unreadable or prevent it from opening.
225
231
  */
226
232
  export function getThemeCssVars(theme?: AnnotateTheme | null): Record<string, string> {
227
- const vars: Record<string, string> = {
228
- ...(readAppearance(theme) === "light" ? LIGHT_FALLBACK : DARK_FALLBACK),
229
- };
230
- if (theme == null) return vars;
231
-
232
- for (const { token, cssVar } of FOREGROUND_THEME_VARS) {
233
- const color = readThemeColor(theme, token, false);
234
- if (color != null) vars[cssVar] = color;
235
- }
236
- for (const { token, cssVar } of BACKGROUND_THEME_VARS) {
237
- const color = readThemeColor(theme, token, true);
238
- if (color != null) vars[cssVar] = color;
239
- }
240
- return vars;
233
+ const vars: Record<string, string> = {
234
+ ...(readAppearance(theme) === "light" ? LIGHT_FALLBACK : DARK_FALLBACK),
235
+ };
236
+ if (theme == null) return vars;
237
+
238
+ for (const { token, cssVar } of FOREGROUND_THEME_VARS) {
239
+ const color = readThemeColor(theme, token, false);
240
+ if (color != null) vars[cssVar] = color;
241
+ }
242
+ for (const { token, cssVar } of BACKGROUND_THEME_VARS) {
243
+ const color = readThemeColor(theme, token, true);
244
+ if (color != null) vars[cssVar] = color;
245
+ }
246
+ return vars;
241
247
  }
242
248
 
243
249
  export const __testing = {
244
- ansiColorToHex,
245
- colorValueToHex,
246
- xterm256ToHex,
250
+ ansiColorToHex,
251
+ colorValueToHex,
252
+ xterm256ToHex,
247
253
  };