@aeondave/pi-theme-1337 0.0.0-stage → 0.5.1
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/CHANGELOG.md +253 -0
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/assets/banner.png +0 -0
- package/assets/palettes.gif +0 -0
- package/docs/architecture.md +167 -0
- package/docs/configuration.md +87 -0
- package/docs/palette-design.md +51 -0
- package/docs/theme-motion.md +88 -0
- package/package.json +68 -4
- package/src/avatar.ts +174 -0
- package/src/background.ts +82 -0
- package/src/config.ts +357 -0
- package/src/diff-spans.ts +77 -0
- package/src/editor.ts +443 -0
- package/src/format.ts +464 -0
- package/src/frame.ts +380 -0
- package/src/index.ts +1195 -0
- package/src/quota.ts +581 -0
- package/src/redraw-guard.ts +79 -0
- package/src/run-summary.ts +623 -0
- package/src/spinner.ts +72 -0
- package/src/terminal-title.ts +67 -0
- package/src/tool-activity.ts +216 -0
- package/src/tool-adapter.ts +491 -0
- package/src/tool-groups.ts +192 -0
- package/src/tool-lexicon.ts +200 -0
- package/src/tool-view.ts +788 -0
- package/src/width.ts +246 -0
- package/themes/1337-amber.json +94 -0
- package/themes/1337-amber.pi1337 +3 -0
- package/themes/1337-cosmos.json +94 -0
- package/themes/1337-cosmos.pi1337 +3 -0
- package/themes/1337-cyber.json +94 -0
- package/themes/1337-cyber.pi1337 +3 -0
- package/themes/1337-cyberpunk-2077-edgerunner.json +94 -0
- package/themes/1337-cyberpunk-2077-edgerunner.pi1337 +7 -0
- package/themes/1337-cyberpunk-2077.json +94 -0
- package/themes/1337-cyberpunk-2077.pi1337 +7 -0
- package/themes/1337-fallout.json +94 -0
- package/themes/1337-fallout.pi1337 +4 -0
- package/themes/1337-ice.json +94 -0
- package/themes/1337-ice.pi1337 +3 -0
- package/themes/1337-magma.json +94 -0
- package/themes/1337-magma.pi1337 +1 -0
- package/themes/1337-neon.json +94 -0
- package/themes/1337-neon.pi1337 +3 -0
- package/themes/1337-poison.json +94 -0
- package/themes/1337-poison.pi1337 +3 -0
- package/themes/1337-skynet-dark.json +94 -0
- package/themes/1337-skynet-dark.pi1337 +4 -0
- package/themes/1337-skynet.json +94 -0
- package/themes/1337-skynet.pi1337 +4 -0
- package/themes/1337.json +95 -0
- package/themes/1337.pi1337 +3 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment-driven config for pi-1337.
|
|
3
|
+
*
|
|
4
|
+
* | Env var | Default | Purpose |
|
|
5
|
+
* |---------|---------|---------|
|
|
6
|
+
* | `PI_1337_POLISH` | `on` | Compact tool rows and diff previews; `off` restores host renderers. |
|
|
7
|
+
* | `PI_1337_DISABLE` | _(off)_ | Any non-empty value disables the extension entirely. |
|
|
8
|
+
* | `PI_1337_THEME` | `on` | `off` keeps your current theme; `on` preserves a bundled Pi choice or uses green; `green` explicitly pins green. Palette aliases include `poison`, `magma`, `cyberpunk-2077`, `cyberpunk-2077-edgerunner`, and `skynet-dark`. |
|
|
9
|
+
* | `PI_1337_THINKING_LABEL` | `∴ thinking` | Label for the collapsed thinking row. |
|
|
10
|
+
* | `PI_1337_COPY_KEY` | `alt+c` | Shortcut that copies the input buffer as clean text. Empty disables it. |
|
|
11
|
+
* | `PI_1337_BG` | _(theme)_ | `on`/`auto` tints the terminal window background to the active palette's `void`; a `#rrggbb`/`#rgb` value forces that color; `off` leaves the terminal untouched. Unset defers to the theme's own `pi1337.background`; a set value overrides it. |
|
|
12
|
+
* | `PI_1337_ACTIVITY` | `on` | Names a long-running tool in the status row (`Running npm test…`); `off` hides it. |
|
|
13
|
+
* | `PI_1337_GROUPS` | `on` | Folds runs of three or more settled read/search/list rows into one tally row; `off` keeps one row per call. Needs `PI_1337_POLISH`. |
|
|
14
|
+
* | `PI_1337_SUMMARY` | `on` | Appends one summary row when a run that used tools, was interrupted or failed settles; `off` writes none and hides existing ones. |
|
|
15
|
+
* | `PI_1337_TITLE` | `on` | Shows the run state in the terminal title (`● Running npm test… — repo`); `off` never touches the title. |
|
|
16
|
+
* | `PI_1337_QUOTA` | `on` | Shows the subscription quota a provider reports in the status row (`5h 43% · wk 12%`); `off` reads nothing and registers no handler. |
|
|
17
|
+
* | `PI_1337_MAX_WIDTH` | `100` | Widest framed editor box, in columns; `0`/`full` removes the cap. Values below 40 are raised to 40. |
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { readFileSync } from "node:fs";
|
|
21
|
+
import { homedir } from "node:os";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* This extension's state directory — `<agent-dir>/1337-theme/`, namespaced the
|
|
26
|
+
* same way every other Pi extension namespaces its state (`claude-native/`,
|
|
27
|
+
* `persona/`, `skill-optimizer/`). Override with `PI_1337_STATE_DIR`.
|
|
28
|
+
*/
|
|
29
|
+
export function stateDir(): string {
|
|
30
|
+
return process.env.PI_1337_STATE_DIR?.trim() || join(agentDir(), "1337-theme");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Pi's own agent directory: `PI_CODING_AGENT_DIR` env, else `~/.pi/agent`.
|
|
35
|
+
*
|
|
36
|
+
* REPLICATED rather than imported (same rationale as `pi-claude`): this module
|
|
37
|
+
* deliberately imports nothing from Pi at runtime, and the helper is not part
|
|
38
|
+
* of the extension API surface we can rely on across Pi versions.
|
|
39
|
+
*/
|
|
40
|
+
function agentDir(): string {
|
|
41
|
+
return process.env.PI_CODING_AGENT_DIR?.trim() || join(homedir(), ".pi", "agent");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Settings persisted in `<stateDir>/config.json`. Keys map 1:1 to the env vars. */
|
|
45
|
+
export interface ConfigFileSettings {
|
|
46
|
+
disable?: boolean;
|
|
47
|
+
polish?: boolean;
|
|
48
|
+
activity?: boolean;
|
|
49
|
+
groups?: boolean;
|
|
50
|
+
summary?: boolean;
|
|
51
|
+
title?: boolean;
|
|
52
|
+
quota?: boolean;
|
|
53
|
+
theme?: string;
|
|
54
|
+
thinkingLabel?: string;
|
|
55
|
+
copyKey?: string;
|
|
56
|
+
background?: string;
|
|
57
|
+
/** Columns, or `"full"`; validated by `maxBoxWidth`, not here. */
|
|
58
|
+
maxWidth?: number | string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let cachedDir: string | null = null;
|
|
62
|
+
let cachedSettings: ConfigFileSettings | null = null;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Read `<stateDir>/config.json`. Returns `{}` for a missing, unreadable,
|
|
66
|
+
* malformed, or wrongly-typed file: a broken config must never stop Pi from
|
|
67
|
+
* starting, so every failure degrades to the built-in defaults.
|
|
68
|
+
*
|
|
69
|
+
* Only known keys of the expected primitive type are kept; anything else is
|
|
70
|
+
* dropped rather than surfaced as a wrong-typed value.
|
|
71
|
+
*/
|
|
72
|
+
export function configFileSettings(): ConfigFileSettings {
|
|
73
|
+
const dir = stateDir();
|
|
74
|
+
// Keyed on the resolved dir so a changed PI_1337_STATE_DIR is never served
|
|
75
|
+
// from a previous dir's memoized file.
|
|
76
|
+
if (cachedSettings && cachedDir === dir) return cachedSettings;
|
|
77
|
+
cachedDir = dir;
|
|
78
|
+
const empty: ConfigFileSettings = {};
|
|
79
|
+
let raw: string;
|
|
80
|
+
try {
|
|
81
|
+
raw = readFileSync(join(dir, "config.json"), "utf8");
|
|
82
|
+
} catch {
|
|
83
|
+
cachedSettings = empty;
|
|
84
|
+
return cachedSettings;
|
|
85
|
+
}
|
|
86
|
+
let parsed: unknown;
|
|
87
|
+
try {
|
|
88
|
+
parsed = JSON.parse(raw);
|
|
89
|
+
} catch {
|
|
90
|
+
cachedSettings = empty;
|
|
91
|
+
return cachedSettings;
|
|
92
|
+
}
|
|
93
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
94
|
+
cachedSettings = empty;
|
|
95
|
+
return cachedSettings;
|
|
96
|
+
}
|
|
97
|
+
const out: ConfigFileSettings = {};
|
|
98
|
+
const src = parsed as Record<string, unknown>;
|
|
99
|
+
if (typeof src.disable === "boolean") out.disable = src.disable;
|
|
100
|
+
if (typeof src.polish === "boolean") out.polish = src.polish;
|
|
101
|
+
if (typeof src.activity === "boolean") out.activity = src.activity;
|
|
102
|
+
if (typeof src.groups === "boolean") out.groups = src.groups;
|
|
103
|
+
if (typeof src.summary === "boolean") out.summary = src.summary;
|
|
104
|
+
if (typeof src.title === "boolean") out.title = src.title;
|
|
105
|
+
if (typeof src.quota === "boolean") out.quota = src.quota;
|
|
106
|
+
if (typeof src.theme === "string") out.theme = src.theme;
|
|
107
|
+
if (typeof src.thinkingLabel === "string") out.thinkingLabel = src.thinkingLabel;
|
|
108
|
+
if (typeof src.copyKey === "string") out.copyKey = src.copyKey;
|
|
109
|
+
if (typeof src.background === "string") out.background = src.background;
|
|
110
|
+
if (typeof src.maxWidth === "number" || typeof src.maxWidth === "string") out.maxWidth = src.maxWidth;
|
|
111
|
+
cachedSettings = out;
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Drop the memoized `config.json` read (tests and `/reload`). */
|
|
116
|
+
export function resetConfigCache(): void {
|
|
117
|
+
cachedSettings = null;
|
|
118
|
+
cachedDir = null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The default bundled theme's name (registered via `pi.themes`). */
|
|
122
|
+
export const THEME_NAME = "1337";
|
|
123
|
+
|
|
124
|
+
/** All bundled palettes — the set `/settings` can persist that we must respect. */
|
|
125
|
+
export const BUNDLED_THEMES = [
|
|
126
|
+
"1337",
|
|
127
|
+
"1337-amber",
|
|
128
|
+
"1337-ice",
|
|
129
|
+
"1337-poison",
|
|
130
|
+
"1337-magma",
|
|
131
|
+
"1337-cyber",
|
|
132
|
+
"1337-cyberpunk-2077",
|
|
133
|
+
"1337-cyberpunk-2077-edgerunner",
|
|
134
|
+
"1337-neon",
|
|
135
|
+
"1337-cosmos",
|
|
136
|
+
"1337-fallout",
|
|
137
|
+
"1337-skynet",
|
|
138
|
+
"1337-skynet-dark",
|
|
139
|
+
] as const;
|
|
140
|
+
|
|
141
|
+
/** Short aliases → registered theme ids. All are also selectable via `/settings`. */
|
|
142
|
+
const THEME_ALIASES: Record<string, string> = {
|
|
143
|
+
green: "1337",
|
|
144
|
+
amber: "1337-amber",
|
|
145
|
+
ice: "1337-ice",
|
|
146
|
+
poison: "1337-poison",
|
|
147
|
+
magma: "1337-magma",
|
|
148
|
+
cyber: "1337-cyber",
|
|
149
|
+
blue: "1337-cyber",
|
|
150
|
+
"cyberpunk-2077": "1337-cyberpunk-2077",
|
|
151
|
+
"cyberpunk-2077-edgerunner": "1337-cyberpunk-2077-edgerunner",
|
|
152
|
+
neon: "1337-neon",
|
|
153
|
+
cosmos: "1337-cosmos",
|
|
154
|
+
fallout: "1337-fallout",
|
|
155
|
+
skynet: "1337-skynet",
|
|
156
|
+
"skynet-dark": "1337-skynet-dark",
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
function paletteFrom(value: string | undefined): string | undefined {
|
|
160
|
+
const raw = value?.trim().toLowerCase();
|
|
161
|
+
if (!raw || raw === "on" || raw === "off") return undefined;
|
|
162
|
+
if (raw in THEME_ALIASES) return THEME_ALIASES[raw];
|
|
163
|
+
if (isBundledTheme(raw)) return raw;
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function isDisabled(): boolean {
|
|
168
|
+
return !!process.env.PI_1337_DISABLE?.trim() || configFileSettings().disable === true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Whether to switch to a bundled phosphor theme on activation. `off` in either
|
|
173
|
+
* the env var or the config file keeps the current theme (redesign only).
|
|
174
|
+
*/
|
|
175
|
+
export function autoThemeEnabled(): boolean {
|
|
176
|
+
const raw = process.env.PI_1337_THEME ?? configFileSettings().theme;
|
|
177
|
+
return raw?.trim().toLowerCase() !== "off";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** True when `name` is one of the palettes this package ships. */
|
|
181
|
+
export function isBundledTheme(name: string | undefined): name is (typeof BUNDLED_THEMES)[number] {
|
|
182
|
+
return !!name && (BUNDLED_THEMES as readonly string[]).includes(name);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The palette explicitly pinned via `PI_1337_THEME`, or `undefined` when the
|
|
187
|
+
* variable is unset, a plain `on`/`off`, or unrecognized. Config-file theme
|
|
188
|
+
* values are startup fallbacks, not explicit pins.
|
|
189
|
+
*/
|
|
190
|
+
export function envPalette(): string | undefined {
|
|
191
|
+
return paletteFrom(process.env.PI_1337_THEME);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function configPalette(): string | undefined {
|
|
195
|
+
return paletteFrom(configFileSettings().theme);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The palette to apply at session start: an explicit environment pin wins;
|
|
200
|
+
* otherwise Pi's current bundled palette is respected; otherwise a configured
|
|
201
|
+
* config-file palette seeds startup; otherwise a light theme is kept as is;
|
|
202
|
+
* otherwise default green. `appearance` is the current theme's (Pi's
|
|
203
|
+
* `Theme.appearance`).
|
|
204
|
+
*/
|
|
205
|
+
export function chooseStartupTheme(current: string | undefined, appearance?: "light" | "dark"): string {
|
|
206
|
+
const pinned = envPalette();
|
|
207
|
+
if (pinned) return pinned;
|
|
208
|
+
// Pi's current bundled theme is its persisted /settings choice. The config
|
|
209
|
+
// file is a first-start fallback, so it must not override a later UI choice.
|
|
210
|
+
if (isBundledTheme(current)) return current;
|
|
211
|
+
// Any environment setting takes precedence over the config file. An explicit
|
|
212
|
+
// `on` asks for the default palette, on a light theme too. With no env
|
|
213
|
+
// setting, the config-file palette may seed startup when Pi has no bundled choice.
|
|
214
|
+
const env = process.env.PI_1337_THEME;
|
|
215
|
+
if (env?.trim().toLowerCase() === "on") return THEME_NAME;
|
|
216
|
+
const configured = env === undefined ? configPalette() : undefined;
|
|
217
|
+
if (configured) return configured;
|
|
218
|
+
// Every bundled palette is dark and the tint is opt-in, so on a light theme
|
|
219
|
+
// (Pi picks `light` for a light terminal) switching would leave text
|
|
220
|
+
// unreadable. The chrome uses semantic tokens, so keep the user's theme.
|
|
221
|
+
if (appearance === "light" && current) return current;
|
|
222
|
+
return THEME_NAME;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function hiddenThinkingLabel(): string {
|
|
226
|
+
return (process.env.PI_1337_THINKING_LABEL ?? configFileSettings().thinkingLabel)?.trim() || "∴ thinking";
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Key chord for the "copy input buffer" shortcut (a `KeyId` such as `alt+c` or
|
|
231
|
+
* `ctrl+alt+y`). Set `PI_1337_COPY_KEY` (or `copyKey` in `config.json`) to an
|
|
232
|
+
* empty/whitespace value to disable.
|
|
233
|
+
*/
|
|
234
|
+
export function copyShortcut(): string {
|
|
235
|
+
// `!== undefined` rather than `??`: an env var set to "" must still win and
|
|
236
|
+
// disable the shortcut, not fall through to the config file's value.
|
|
237
|
+
const raw = process.env.PI_1337_COPY_KEY !== undefined ? process.env.PI_1337_COPY_KEY : configFileSettings().copyKey;
|
|
238
|
+
if (raw === undefined) return "alt+c";
|
|
239
|
+
return raw.trim();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The raw `PI_1337_BG` value (undefined when unset), forwarded to the pure
|
|
244
|
+
* `resolveBackground` resolver so an env setting overrides the theme's own
|
|
245
|
+
* `pi1337.background`. Off by default: tinting the terminal is invasive, so it
|
|
246
|
+
* only happens when the env var OR the active palette explicitly opts in.
|
|
247
|
+
*/
|
|
248
|
+
export function backgroundEnv(): string | undefined {
|
|
249
|
+
return process.env.PI_1337_BG ?? configFileSettings().background;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Shared env-over-file resolution for the `on`/`off` presentation toggles: an
|
|
254
|
+
* env value of exactly `on`/`off` wins, otherwise the config-file boolean,
|
|
255
|
+
* otherwise the built-in default.
|
|
256
|
+
*/
|
|
257
|
+
function onOffSetting(env: string | undefined, file: boolean | undefined, fallback: boolean): boolean {
|
|
258
|
+
const raw = env?.trim().toLowerCase();
|
|
259
|
+
if (raw === "off") return false;
|
|
260
|
+
if (raw === "on") return true;
|
|
261
|
+
return file ?? fallback;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Compact transcript rendering follows the redesign unless explicitly disabled. */
|
|
265
|
+
export function polishEnabled(): boolean {
|
|
266
|
+
return onOffSetting(process.env.PI_1337_POLISH, configFileSettings().polish, true);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Transient status-row activity label follows the redesign unless explicitly disabled. */
|
|
270
|
+
export function activityEnabled(): boolean {
|
|
271
|
+
return onOffSetting(process.env.PI_1337_ACTIVITY, configFileSettings().activity, true);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Folding settled exploration rows rides the polished tool rows, so it is off
|
|
276
|
+
* whenever polish is, whatever its own setting says.
|
|
277
|
+
*/
|
|
278
|
+
export function groupsEnabled(): boolean {
|
|
279
|
+
return polishEnabled() && onOffSetting(process.env.PI_1337_GROUPS, configFileSettings().groups, true);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The end-of-run summary row follows the redesign unless explicitly disabled. */
|
|
283
|
+
export function summaryEnabled(): boolean {
|
|
284
|
+
return onOffSetting(process.env.PI_1337_SUMMARY, configFileSettings().summary, true);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Run state in the terminal title follows the redesign unless explicitly disabled. */
|
|
288
|
+
export function titleEnabled(): boolean {
|
|
289
|
+
return onOffSetting(process.env.PI_1337_TITLE, configFileSettings().title, true);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** The subscription quota item of the status row follows the redesign unless explicitly disabled. */
|
|
293
|
+
export function quotaEnabled(): boolean {
|
|
294
|
+
return onOffSetting(process.env.PI_1337_QUOTA, configFileSettings().quota, true);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Box cap when nothing valid is configured: the redesign's original width. */
|
|
298
|
+
export const DEFAULT_MAX_WIDTH = 100;
|
|
299
|
+
/** A cap below this leaves too few columns for the input and its status row to read. */
|
|
300
|
+
export const MIN_MAX_WIDTH = 40;
|
|
301
|
+
|
|
302
|
+
/** `full`/`0` → no cap; a whole number of columns → itself (raised to the floor); anything else → undefined. */
|
|
303
|
+
function parseMaxWidth(raw: number | string | undefined): number | undefined {
|
|
304
|
+
if (raw === undefined) return undefined;
|
|
305
|
+
const text = String(raw).trim().toLowerCase();
|
|
306
|
+
if (text === "full") return Number.POSITIVE_INFINITY;
|
|
307
|
+
if (!/^\d+$/.test(text)) return undefined;
|
|
308
|
+
const columns = Number(text);
|
|
309
|
+
if (!Number.isSafeInteger(columns)) return undefined;
|
|
310
|
+
return columns === 0 ? Number.POSITIVE_INFINITY : Math.max(MIN_MAX_WIDTH, columns);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Widest framed box in columns (`Infinity` = no cap). A valid env value wins,
|
|
315
|
+
* then a valid config-file value, then the default; invalid values are ignored
|
|
316
|
+
* so a typo can never collapse the editor.
|
|
317
|
+
*/
|
|
318
|
+
export function maxBoxWidth(): number {
|
|
319
|
+
return parseMaxWidth(process.env.PI_1337_MAX_WIDTH) ?? parseMaxWidth(configFileSettings().maxWidth) ?? DEFAULT_MAX_WIDTH;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Every setting after merging env over the config file over the built-in defaults. */
|
|
323
|
+
export interface ResolvedSettings {
|
|
324
|
+
disable: boolean;
|
|
325
|
+
polish: boolean;
|
|
326
|
+
activity: boolean;
|
|
327
|
+
groups: boolean;
|
|
328
|
+
summary: boolean;
|
|
329
|
+
title: boolean;
|
|
330
|
+
quota: boolean;
|
|
331
|
+
autoTheme: boolean;
|
|
332
|
+
theme?: string;
|
|
333
|
+
thinkingLabel: string;
|
|
334
|
+
copyKey: string;
|
|
335
|
+
background?: string;
|
|
336
|
+
/** Columns; `Infinity` when uncapped. */
|
|
337
|
+
maxWidth: number;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Merge env → config file → defaults into one resolved view. */
|
|
341
|
+
export function resolveSettings(): ResolvedSettings {
|
|
342
|
+
return {
|
|
343
|
+
disable: isDisabled(),
|
|
344
|
+
polish: polishEnabled(),
|
|
345
|
+
activity: activityEnabled(),
|
|
346
|
+
groups: groupsEnabled(),
|
|
347
|
+
summary: summaryEnabled(),
|
|
348
|
+
title: titleEnabled(),
|
|
349
|
+
quota: quotaEnabled(),
|
|
350
|
+
autoTheme: autoThemeEnabled(),
|
|
351
|
+
theme: process.env.PI_1337_THEME === undefined ? configPalette() : envPalette(),
|
|
352
|
+
thinkingLabel: hiddenThinkingLabel(),
|
|
353
|
+
copyKey: copyShortcut(),
|
|
354
|
+
background: backgroundEnv(),
|
|
355
|
+
maxWidth: maxBoxWidth(),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** Bounded, presentation-only comparison of two sanitized replacement bodies. */
|
|
2
|
+
export interface DiffSpan { start: number; end: number }
|
|
3
|
+
export interface ChangedSpans { before: DiffSpan[]; after: DiffSpan[] }
|
|
4
|
+
const WORDS = new Intl.Segmenter(undefined, { granularity: "word" });
|
|
5
|
+
const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
6
|
+
const MAX_CHARS = 8192;
|
|
7
|
+
const MAX_TOKENS = 256;
|
|
8
|
+
const MAX_CELLS = 65_536;
|
|
9
|
+
type Token = { text: string; start: number };
|
|
10
|
+
|
|
11
|
+
function tokens(text: string): Token[] | undefined {
|
|
12
|
+
const result: Token[] = [];
|
|
13
|
+
for (const part of WORDS.segment(text)) {
|
|
14
|
+
if (result.length === MAX_TOKENS) return undefined;
|
|
15
|
+
result.push({ text: part.segment, start: part.index });
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Refine the edges of a changed word without splitting emoji or combining marks. */
|
|
21
|
+
function refine(before: string, after: string, oldStart: number, newStart: number, result: ChangedSpans): void {
|
|
22
|
+
const old = Array.from(GRAPHEMES.segment(before), part => part.segment);
|
|
23
|
+
const next = Array.from(GRAPHEMES.segment(after), part => part.segment);
|
|
24
|
+
let left = 0;
|
|
25
|
+
let right = 0;
|
|
26
|
+
let prefix = 0;
|
|
27
|
+
let suffix = 0;
|
|
28
|
+
while (left < Math.min(old.length, next.length) && old[left] === next[left]) prefix += old[left++]!.length;
|
|
29
|
+
while (right < Math.min(old.length, next.length) - left && old[old.length - right - 1] === next[next.length - right - 1]) {
|
|
30
|
+
suffix += old[old.length - ++right]!.length;
|
|
31
|
+
}
|
|
32
|
+
if (before.length > prefix + suffix) result.before.push({ start: oldStart + prefix, end: oldStart + before.length - suffix });
|
|
33
|
+
if (after.length > prefix + suffix) result.after.push({ start: newStart + prefix, end: newStart + after.length - suffix });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Retain common words/punctuation between edits, then refine each changed span.
|
|
38
|
+
* Huge lines/token streams get no intra-line emphasis; their full-line colors remain.
|
|
39
|
+
* Limits bound segmentation input, quadratic work and matrix memory (~128 KiB).
|
|
40
|
+
*/
|
|
41
|
+
export function changedSpans(before: string, after: string): ChangedSpans {
|
|
42
|
+
const result: ChangedSpans = { before: [], after: [] };
|
|
43
|
+
if (before === after || before.length > MAX_CHARS || after.length > MAX_CHARS) return result;
|
|
44
|
+
const old = tokens(before);
|
|
45
|
+
const next = tokens(after);
|
|
46
|
+
if (!old || !next || (old.length + 1) * (next.length + 1) > MAX_CELLS) return result;
|
|
47
|
+
const columns = next.length + 1;
|
|
48
|
+
const common = new Uint16Array((old.length + 1) * columns);
|
|
49
|
+
for (let i = old.length - 1; i >= 0; i--) {
|
|
50
|
+
for (let j = next.length - 1; j >= 0; j--) {
|
|
51
|
+
common[i * columns + j] = old[i]!.text === next[j]!.text
|
|
52
|
+
? 1 + common[(i + 1) * columns + j + 1]!
|
|
53
|
+
: Math.max(common[(i + 1) * columns + j]!, common[i * columns + j + 1]!);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let i = 0;
|
|
57
|
+
let j = 0;
|
|
58
|
+
let oldStart = 0;
|
|
59
|
+
let newStart = 0;
|
|
60
|
+
const flush = () => {
|
|
61
|
+
const oldEnd = old[i]?.start ?? before.length;
|
|
62
|
+
const newEnd = next[j]?.start ?? after.length;
|
|
63
|
+
refine(before.slice(oldStart, oldEnd), after.slice(newStart, newEnd), oldStart, newStart, result);
|
|
64
|
+
};
|
|
65
|
+
while (i < old.length || j < next.length) {
|
|
66
|
+
if (i < old.length && j < next.length && old[i]!.text === next[j]!.text) {
|
|
67
|
+
flush();
|
|
68
|
+
oldStart = old[i]!.start + old[i]!.text.length;
|
|
69
|
+
newStart = next[j]!.start + next[j]!.text.length;
|
|
70
|
+
i++;
|
|
71
|
+
j++;
|
|
72
|
+
} else if (j === next.length || (i < old.length && common[(i + 1) * columns + j]! >= common[i * columns + j + 1]!)) i++;
|
|
73
|
+
else j++;
|
|
74
|
+
}
|
|
75
|
+
flush();
|
|
76
|
+
return result;
|
|
77
|
+
}
|