pi-stereo-view 0.1.22 → 0.1.24
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 +25 -0
- package/README.md +34 -2
- package/package.json +1 -1
- package/src/compact-tools.ts +34 -10
- package/src/content.ts +66 -0
- package/src/hex-input.ts +51 -0
- package/src/index.ts +114 -11
- package/src/layout.ts +173 -17
- package/src/native.ts +184 -19
- package/src/preferences.ts +67 -4
- package/src/report.ts +183 -0
- package/src/steady-settings.ts +77 -0
- package/src/surface.ts +31 -1
- package/src/timestamps.ts +66 -23
- package/src/turns.ts +182 -0
package/src/report.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { VERSION, type Theme } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { truncateToWidth, visibleWidth, wrapTextWithAnsi, type Component, type TUI, type TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
5
|
+
|
|
6
|
+
export const ISSUES_URL = "https://github.com/alexshpunt/pi-stereo-view/issues";
|
|
7
|
+
/** GitHub rejects longer new-issue links; long stacks lose their last frames first. */
|
|
8
|
+
const MAX_URL_LENGTH = 7000;
|
|
9
|
+
const MAX_STACK_LINES = 15;
|
|
10
|
+
const WIDGET = "pi-stereo-view-problem";
|
|
11
|
+
|
|
12
|
+
/** Something that stopped the split: an unexpected error, or a Pi layout the extension cannot use. */
|
|
13
|
+
export interface Problem {
|
|
14
|
+
/** What happened, in a few words, for the user. */
|
|
15
|
+
summary: string;
|
|
16
|
+
error?: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ProblemReport {
|
|
20
|
+
title: string;
|
|
21
|
+
/** Issue body with the error, its stack and versions. */
|
|
22
|
+
body: string;
|
|
23
|
+
/** New-issue link with the title and body filled in. */
|
|
24
|
+
url: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function extensionVersion(): string {
|
|
28
|
+
try {
|
|
29
|
+
const value: unknown = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
30
|
+
const version = (value as { version?: unknown }).version;
|
|
31
|
+
return typeof version === "string" ? version : "unknown";
|
|
32
|
+
} catch {
|
|
33
|
+
return "unknown";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Pi's version, whether it is the Pi-Bolt build, the runtime and the platform. */
|
|
38
|
+
function hostDescription(): string {
|
|
39
|
+
const bolt = /pi-bolt/i.test(process.execPath) ? " (Pi-Bolt)" : "";
|
|
40
|
+
const runtime = process.versions.bun ? `Bun ${process.versions.bun}` : `Node.js ${process.versions.node}`;
|
|
41
|
+
return `Pi ${VERSION}${bolt}, ${runtime}, ${process.platform}-${process.arch}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const errorMessage = (error: unknown): string => error instanceof Error ? error.message : error === undefined ? "" : String(error);
|
|
45
|
+
|
|
46
|
+
/** Stack frames without the user's home folder, which appears in every installed extension path. */
|
|
47
|
+
function stackFrames(error: unknown): string[] {
|
|
48
|
+
if (!(error instanceof Error) || !error.stack) return [];
|
|
49
|
+
const home = homedir();
|
|
50
|
+
return error.stack.split("\n").slice(1).map(line => line.trim()).filter(Boolean)
|
|
51
|
+
.map(line => home ? line.replaceAll(home, "~") : line).slice(0, MAX_STACK_LINES);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Build the issue a user can review and submit; nothing is sent automatically. */
|
|
55
|
+
export function problemReport(problem: Problem): ProblemReport {
|
|
56
|
+
const message = errorMessage(problem.error);
|
|
57
|
+
const title = truncateToWidth(message || problem.summary, 100, "…");
|
|
58
|
+
const frames = stackFrames(problem.error);
|
|
59
|
+
const body = (lines: string[]): string => [
|
|
60
|
+
"### What happened",
|
|
61
|
+
problem.summary,
|
|
62
|
+
"",
|
|
63
|
+
...(message ? ["### Error", "```text", message, ...lines, "```", ""] : []),
|
|
64
|
+
"### Environment",
|
|
65
|
+
`- pi-stereo-view ${extensionVersion()}`,
|
|
66
|
+
`- ${hostDescription()}`,
|
|
67
|
+
"",
|
|
68
|
+
"### What I was doing",
|
|
69
|
+
"<!-- For example: streaming a reply, switching to Combined, resizing the terminal. -->",
|
|
70
|
+
"",
|
|
71
|
+
].join("\n");
|
|
72
|
+
const link = (text: string): string =>
|
|
73
|
+
`${ISSUES_URL}/new?title=${encodeURIComponent(title)}&body=${encodeURIComponent(text)}`;
|
|
74
|
+
let shown = frames;
|
|
75
|
+
while (shown.length && link(body(shown)).length > MAX_URL_LENGTH) shown = shown.slice(0, -1);
|
|
76
|
+
return { title, body: body(frames), url: link(body(shown)) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hyperlink = (url: string, text: string): string => `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`;
|
|
80
|
+
|
|
81
|
+
type Action = "open" | "copy" | "close";
|
|
82
|
+
const ACTIONS: { action: Action; label: string }[] = [
|
|
83
|
+
{ action: "open", label: "[Open issue]" },
|
|
84
|
+
{ action: "copy", label: "[Copy report]" },
|
|
85
|
+
{ action: "close", label: "[Close]" },
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Widget above the editor that names the problem and offers a prefilled GitHub issue. It stays until closed,
|
|
90
|
+
* because a short notification is easy to miss while the split silently falls back to combined history.
|
|
91
|
+
*/
|
|
92
|
+
export class ProblemPanel implements Component {
|
|
93
|
+
private actionsRow = -1;
|
|
94
|
+
private readonly spans: { action: Action; start: number; end: number }[] = [];
|
|
95
|
+
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly problem: Problem,
|
|
98
|
+
private readonly report: ProblemReport,
|
|
99
|
+
private readonly theme: Theme,
|
|
100
|
+
private readonly act: (action: Action) => void,
|
|
101
|
+
) {}
|
|
102
|
+
|
|
103
|
+
render(width: number): string[] {
|
|
104
|
+
const theme = this.theme;
|
|
105
|
+
const message = errorMessage(this.problem.error);
|
|
106
|
+
const inner = Math.max(1, width - 3);
|
|
107
|
+
const wrap = (text: string): string[] => wrapTextWithAnsi(text, inner).map(line => " " + line);
|
|
108
|
+
const lines = [
|
|
109
|
+
truncateToWidth(" " + theme.fg("warning", "⚠ ") + theme.bold("pi-stereo-view " + this.problem.summary), width),
|
|
110
|
+
...(message ? wrap(theme.fg("muted", message)) : []),
|
|
111
|
+
...wrap(theme.fg("dim", "Pi keeps working with its own history view. Please report this so it can be fixed:")),
|
|
112
|
+
truncateToWidth(" " + hyperlink(ISSUES_URL, theme.fg("accent", ISSUES_URL)), width),
|
|
113
|
+
];
|
|
114
|
+
this.spans.length = 0;
|
|
115
|
+
let column = 3;
|
|
116
|
+
let row = " ";
|
|
117
|
+
for (const { action, label } of ACTIONS) {
|
|
118
|
+
const text = action === "open" ? hyperlink(this.report.url, theme.fg("accent", label)) : theme.fg("accent", label);
|
|
119
|
+
this.spans.push({ action, start: column, end: column + visibleWidth(label) });
|
|
120
|
+
row += text + " ";
|
|
121
|
+
column += visibleWidth(label) + 1;
|
|
122
|
+
}
|
|
123
|
+
this.actionsRow = lines.length;
|
|
124
|
+
lines.push(truncateToWidth(row, width));
|
|
125
|
+
return lines;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
handleMouse(event: TuiMouseEvent): { handled: true; render?: boolean } | { capture: true } | undefined {
|
|
129
|
+
if (event.button !== "left" || event.y !== this.actionsRow) return undefined;
|
|
130
|
+
const span = this.spans.find(candidate => event.x >= candidate.start && event.x < candidate.end);
|
|
131
|
+
if (!span) return undefined;
|
|
132
|
+
// Like Pi's own buttons: capture the press, act on the click that follows its release.
|
|
133
|
+
if (event.type === "press") return { capture: true };
|
|
134
|
+
if (event.type === "click") {
|
|
135
|
+
this.act(span.action);
|
|
136
|
+
return { handled: true, render: true };
|
|
137
|
+
}
|
|
138
|
+
return { handled: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
invalidate(): void {}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Show one problem at a time and never reopen a problem the user closed in this session. */
|
|
145
|
+
export class ProblemReporter {
|
|
146
|
+
private readonly closed = new Set<string>();
|
|
147
|
+
private current?: string;
|
|
148
|
+
|
|
149
|
+
constructor(private readonly ui: {
|
|
150
|
+
setWidget(key: string, content: ((tui: TUI, theme: Theme) => Component) | undefined): void;
|
|
151
|
+
notify(message: string, type?: "info" | "warning" | "error"): void;
|
|
152
|
+
}, private readonly copy: (text: string) => Promise<void>) {}
|
|
153
|
+
|
|
154
|
+
show(problem: Problem): void {
|
|
155
|
+
const key = problem.summary + "\n" + errorMessage(problem.error);
|
|
156
|
+
if (this.closed.has(key) || this.current === key) return;
|
|
157
|
+
this.current = key;
|
|
158
|
+
const report = problemReport(problem);
|
|
159
|
+
this.ui.setWidget(WIDGET, (tui, theme) => new ProblemPanel(problem, report, theme, action => {
|
|
160
|
+
if (action === "close") {
|
|
161
|
+
this.closed.add(key);
|
|
162
|
+
this.clear();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (action === "open") {
|
|
166
|
+
const open = Reflect.get(tui, "openUrl") as ((url: string) => void) | undefined;
|
|
167
|
+
if (typeof open === "function") {
|
|
168
|
+
try { open.call(tui, report.url); return; } catch { /* Fall back to copying the link. */ }
|
|
169
|
+
}
|
|
170
|
+
void this.copy(report.url).then(() => this.ui.notify("Issue link copied to the clipboard.", "info"),
|
|
171
|
+
error => this.ui.notify("Could not copy the issue link: " + String(error), "error"));
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
void this.copy(report.body).then(() => this.ui.notify("Problem report copied to the clipboard.", "info"),
|
|
175
|
+
error => this.ui.notify("Could not copy the problem report: " + String(error), "error"));
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
clear(): void {
|
|
180
|
+
this.current = undefined;
|
|
181
|
+
this.ui.setWidget(WIDGET, undefined);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { matchesKey, wrapTextWithAnsi, type Component, type SettingItem, type SettingsList, type TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
/** Rows of Pi's settings list under its items: a blank row and the key hint. */
|
|
4
|
+
const HINT_ROWS = 2;
|
|
5
|
+
/** Pi's key hint, extended with the arrow keys this list adds. */
|
|
6
|
+
const PI_HINT = "Enter/Space to change";
|
|
7
|
+
const ARROW_HINT = "←/→ or Enter/Space to change";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Keep a settings list at one height while the selection moves, and step values with the arrow keys.
|
|
11
|
+
*
|
|
12
|
+
* Pi's list shows the selected item's description between the items and the key hint, so its height follows the
|
|
13
|
+
* description's length; a centered overlay then jumps. Blank rows before the hint make every selection as tall as
|
|
14
|
+
* the one with the longest description. Left and Right pick the previous or next value, wrapping around at either
|
|
15
|
+
* end like Enter and Space, so a strength can be tuned in both directions.
|
|
16
|
+
*/
|
|
17
|
+
export class SteadySettings implements Component {
|
|
18
|
+
private measured?: { width: number; rows: number };
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly list: SettingsList,
|
|
22
|
+
private readonly items: readonly SettingItem[],
|
|
23
|
+
private readonly visible: number,
|
|
24
|
+
private readonly change: (id: string, value: string) => void,
|
|
25
|
+
) {}
|
|
26
|
+
|
|
27
|
+
render(width: number): string[] {
|
|
28
|
+
const rows = this.rows(width);
|
|
29
|
+
// A submenu, such as a color input, keeps the list's height too: blank rows follow it.
|
|
30
|
+
if (this.submenuOpen()) {
|
|
31
|
+
const lines = this.list.render(width);
|
|
32
|
+
return lines.length >= rows ? lines : [...lines, ...Array<string>(rows - lines.length).fill("")];
|
|
33
|
+
}
|
|
34
|
+
const lines = this.list.render(width).map(line => line.replace(PI_HINT, ARROW_HINT));
|
|
35
|
+
// A list taller than measured renders as it is.
|
|
36
|
+
if (lines.length >= rows) return lines;
|
|
37
|
+
const cut = Math.max(0, lines.length - HINT_ROWS);
|
|
38
|
+
return [...lines.slice(0, cut), ...Array<string>(rows - lines.length).fill(""), ...lines.slice(cut)];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The list's height with the longest description, as Pi's list lays it out at this width. */
|
|
42
|
+
private rows(width: number): number {
|
|
43
|
+
if (this.measured?.width === width) return this.measured.rows;
|
|
44
|
+
const shown = Math.min(this.visible, this.items.length);
|
|
45
|
+
const scroll = this.items.length > this.visible ? 1 : 0;
|
|
46
|
+
const description = Math.max(0, ...this.items.map(item => item.description ? 1 + wrapTextWithAnsi(item.description, width - 4).length : 0));
|
|
47
|
+
const rows = shown + scroll + description + HINT_ROWS;
|
|
48
|
+
this.measured = { width, rows };
|
|
49
|
+
return rows;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Pi's list keeps an open submenu in a plain field. */
|
|
53
|
+
private submenuOpen(): boolean {
|
|
54
|
+
return Reflect.get(this.list, "submenuComponent") != null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
handleInput(data: string): void {
|
|
58
|
+
const step = matchesKey(data, "left") ? -1 : matchesKey(data, "right") ? 1 : 0;
|
|
59
|
+
// In a submenu, such as a text input, the arrows move its cursor.
|
|
60
|
+
if (!step || this.submenuOpen()) {
|
|
61
|
+
this.list.handleInput(data);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// Pi's list does not expose its selection; it keeps it in a plain field.
|
|
65
|
+
const index: unknown = Reflect.get(this.list, "selectedIndex");
|
|
66
|
+
const item = typeof index === "number" ? this.items[index] : undefined;
|
|
67
|
+
const values = item?.values;
|
|
68
|
+
if (!item || !values?.length) return;
|
|
69
|
+
const next = values[(values.indexOf(item.currentValue) + step + values.length) % values.length];
|
|
70
|
+
if (next === item.currentValue) return;
|
|
71
|
+
this.list.updateValue(item.id, next);
|
|
72
|
+
this.change(item.id, next);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
handleMouse(event: TuiMouseEvent): ReturnType<NonNullable<Component["handleMouse"]>> { return this.list.handleMouse(event); }
|
|
76
|
+
invalidate(): void { this.measured = undefined; this.list.invalidate(); }
|
|
77
|
+
}
|
package/src/surface.ts
CHANGED
|
@@ -277,6 +277,10 @@ export interface SplitSurfaceOptions {
|
|
|
277
277
|
height(): number;
|
|
278
278
|
/** Panel receiving keyboard scrolling and the follow-end indicator. */
|
|
279
279
|
active(): ScrollView | undefined;
|
|
280
|
+
/** An error while painting or dispatching the panels; it must not reach Pi's render loop, which would end Pi. */
|
|
281
|
+
failed?(error: unknown): void;
|
|
282
|
+
/** A pointer move starts and ends its dispatch; documents report what lies under it in between. */
|
|
283
|
+
pointer?: { start(): void; end(): void; leave(): void };
|
|
280
284
|
}
|
|
281
285
|
|
|
282
286
|
type SelectionApi = Record<"hasActiveSelection" | "copyActiveSelectionToClipboard" | "resetTextSelection", (...args: never[]) => unknown>;
|
|
@@ -322,6 +326,17 @@ export class SplitSurface extends Container {
|
|
|
322
326
|
}
|
|
323
327
|
|
|
324
328
|
override render(width: number): string[] {
|
|
329
|
+
try {
|
|
330
|
+
return this.layoutFrame(width);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
if (!this.options.failed) throw error;
|
|
333
|
+
this.frame = undefined;
|
|
334
|
+
this.options.failed(error);
|
|
335
|
+
return [];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private layoutFrame(width: number): string[] {
|
|
325
340
|
const model = this.options.model();
|
|
326
341
|
this.syncHostSelection();
|
|
327
342
|
if (!model) {
|
|
@@ -513,6 +528,7 @@ export class SplitSurface extends Container {
|
|
|
513
528
|
const seen = this.lastEvent;
|
|
514
529
|
if (seen && seen.type === event.type && seen.screenX === event.screenX && seen.screenY === event.screenY) return;
|
|
515
530
|
if (!this.scrollbarDrag && event.type !== "wheel") this.setHover(undefined);
|
|
531
|
+
if (event.type === "move") this.options.pointer?.leave();
|
|
516
532
|
}
|
|
517
533
|
|
|
518
534
|
private scrollToPointer(view: ScrollView, geometry: Geometry, y: number, grab: number): void {
|
|
@@ -523,7 +539,21 @@ export class SplitSurface extends Container {
|
|
|
523
539
|
|
|
524
540
|
override handleMouse(event: TuiMouseEvent): Dispatched | undefined {
|
|
525
541
|
this.lastEvent = event;
|
|
526
|
-
|
|
542
|
+
let result: (TuiMouseEventResult & Partial<Dispatched>) | undefined;
|
|
543
|
+
const move = event.type === "move";
|
|
544
|
+
try {
|
|
545
|
+
if (move) this.options.pointer?.start();
|
|
546
|
+
try {
|
|
547
|
+
result = this.mouse(event) as typeof result;
|
|
548
|
+
} finally {
|
|
549
|
+
if (move) this.options.pointer?.end();
|
|
550
|
+
}
|
|
551
|
+
} catch (error) {
|
|
552
|
+
if (!this.options.failed) throw error;
|
|
553
|
+
this.frame = undefined;
|
|
554
|
+
this.options.failed(error);
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
527
557
|
if (!result || result.target) return result as Dispatched | undefined;
|
|
528
558
|
if (!result.handled && !result.capture && !result.focus) return undefined;
|
|
529
559
|
return { ...result, handled: true, target: { component: this, originX: event.screenX - event.x, originY: event.screenY - event.y,
|
package/src/timestamps.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parseSkillBlock, sessionEntryToContextMessages, type ExtensionContext, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import {
|
|
3
3
|
Container, stripTerminalSequences, visibleWidth,
|
|
4
|
-
type Component, type TuiMouseEvent,
|
|
4
|
+
type Color, type Component, type TuiMouseEvent,
|
|
5
5
|
} from "@earendil-works/pi-tui";
|
|
6
6
|
|
|
7
7
|
import { NativeRenderCache, sameValues } from "./render-cache.ts";
|
|
@@ -279,6 +279,15 @@ export class TranscriptTimestamps {
|
|
|
279
279
|
const TEXT_COPY_CONTROL = "[Copy]";
|
|
280
280
|
// CGJ avoids xterm's single-codepoint rescaling; the space reserves glyph overhang.
|
|
281
281
|
const ICON_COPY_CONTROL = "[ ⧉\u034f ]";
|
|
282
|
+
const TEXT_GO_TO_CONTROL = "[Go to]";
|
|
283
|
+
const ICON_GO_TO_CONTROL = "[ ⇄ ]";
|
|
284
|
+
|
|
285
|
+
/** Opening the same point of history in the other panel from a block's clock or its button. */
|
|
286
|
+
export interface BlockGoTo {
|
|
287
|
+
clock(): boolean;
|
|
288
|
+
button(): boolean;
|
|
289
|
+
go(): void;
|
|
290
|
+
}
|
|
282
291
|
/** Keep message text full-width, with a clock above and copy action below. */
|
|
283
292
|
export class CompactMessage extends Container {
|
|
284
293
|
private top = 0;
|
|
@@ -290,17 +299,23 @@ export class CompactMessage extends Container {
|
|
|
290
299
|
private copyRow?: number;
|
|
291
300
|
private copyColumn?: number;
|
|
292
301
|
private copyWidth = 0;
|
|
293
|
-
private
|
|
302
|
+
private goToColumn?: number;
|
|
303
|
+
private goToWidth = 0;
|
|
304
|
+
private clockWidth = 0;
|
|
305
|
+
private pressed?: "copy" | "goTo" | "clock";
|
|
294
306
|
private leadingRows = 0;
|
|
295
307
|
private prepared?: { width: number; content: string[]; lines: string[] };
|
|
296
308
|
private readonly nativeCache: NativeRenderCache;
|
|
297
|
-
private formatted?: {
|
|
309
|
+
private formatted?: {
|
|
310
|
+
width: number; time: string; background?: Color; copy: string; goTo: string; gutter: number; theme: Theme; version: number; top: number; lines: string[];
|
|
311
|
+
};
|
|
298
312
|
|
|
299
313
|
constructor(private readonly component: Component, private readonly label: () => string,
|
|
300
314
|
private readonly theme: () => Theme, private readonly gutter: () => number,
|
|
301
315
|
private readonly show: () => boolean, private readonly copy?: () => void,
|
|
302
316
|
private readonly showCopy: () => boolean = () => true,
|
|
303
|
-
private readonly buttonStyle: () => "icons" | "text" = () => "text", changed?: () => void
|
|
317
|
+
private readonly buttonStyle: () => "icons" | "text" = () => "text", changed?: () => void,
|
|
318
|
+
private readonly clockBackground: () => Color | undefined = () => undefined, private readonly goTo?: BlockGoTo) {
|
|
304
319
|
super();
|
|
305
320
|
this.addChild(component);
|
|
306
321
|
this.nativeCache = new NativeRenderCache(component, changed);
|
|
@@ -340,14 +355,21 @@ export class CompactMessage extends Container {
|
|
|
340
355
|
const theme = this.theme();
|
|
341
356
|
// Only the scrollbar needs a side gutter; controls have their own rows.
|
|
342
357
|
this.contentWidth = Math.max(1, width - gutter);
|
|
343
|
-
const
|
|
344
|
-
const
|
|
358
|
+
const shownTime = label && this.show() && visibleWidth(label) + 1 <= this.contentWidth;
|
|
359
|
+
const background = shownTime ? this.clockBackground() : undefined;
|
|
360
|
+
const time = shownTime ? " " + label : "";
|
|
361
|
+
const icons = this.buttonStyle() === "icons";
|
|
362
|
+
const control = icons ? ICON_COPY_CONTROL : TEXT_COPY_CONTROL;
|
|
345
363
|
const controlWidth = visibleWidth(control);
|
|
346
364
|
const copy = label && this.copy && this.showCopy() && this.contentWidth >= controlWidth ? control : "";
|
|
365
|
+
const goToControl = icons ? ICON_GO_TO_CONTROL : TEXT_GO_TO_CONTROL;
|
|
366
|
+
const goToWidth = visibleWidth(goToControl);
|
|
367
|
+
const goTo = label && this.goTo?.button() && this.contentWidth >= goToWidth + (copy ? controlWidth + 1 : 0) ? goToControl : "";
|
|
347
368
|
const content = this.nativeCache.render(this.contentWidth, theme);
|
|
348
369
|
this.contentHeight = content.length;
|
|
349
370
|
const cached = this.formatted;
|
|
350
|
-
if (cached?.width === width && cached.time === time && cached.copy === copy && cached.
|
|
371
|
+
if (cached?.width === width && cached.time === time && cached.copy === copy && cached.goTo === goTo && cached.background === background &&
|
|
372
|
+
cached.gutter === gutter && cached.theme === theme &&
|
|
351
373
|
cached.version === this.nativeCache.version) {
|
|
352
374
|
this.top = cached.top;
|
|
353
375
|
return cached.lines;
|
|
@@ -359,6 +381,8 @@ export class CompactMessage extends Container {
|
|
|
359
381
|
this.bodyRows = 0;
|
|
360
382
|
this.copyRow = undefined;
|
|
361
383
|
this.copyColumn = undefined;
|
|
384
|
+
this.goToColumn = undefined;
|
|
385
|
+
this.clockWidth = 0;
|
|
362
386
|
let end = content.length;
|
|
363
387
|
// Image protocols need every placement row. Keep their native block intact.
|
|
364
388
|
if (!this.nativeCache.hasImagePlacement) {
|
|
@@ -374,38 +398,57 @@ export class CompactMessage extends Container {
|
|
|
374
398
|
body[body.length - 1] += zones(content.slice(end));
|
|
375
399
|
}
|
|
376
400
|
this.hasClock = Boolean(time);
|
|
401
|
+
this.clockWidth = time ? visibleWidth(time) : 0;
|
|
377
402
|
this.headerRows = time ? 1 : 0;
|
|
378
403
|
this.bodyRows = body.length;
|
|
379
|
-
const
|
|
380
|
-
|
|
404
|
+
const clock = !time ? "" : background ? " " + theme.style(label, { fg: "text", bg: background }) : theme.fg("dim", time);
|
|
405
|
+
const lines = time ? [clock, ...body] : body;
|
|
406
|
+
if (copy || goTo) {
|
|
381
407
|
this.copyRow = lines.length;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
408
|
+
const style = { fg: "dim" as const, dim: icons };
|
|
409
|
+
let row = "";
|
|
410
|
+
if (goTo) {
|
|
411
|
+
this.goToWidth = goToWidth;
|
|
412
|
+
this.goToColumn = this.contentWidth - goToWidth - (copy ? controlWidth + 1 : 0);
|
|
413
|
+
row = " ".repeat(this.goToColumn) + theme.style(goTo, style) + (copy ? " " : "");
|
|
414
|
+
}
|
|
415
|
+
if (copy) {
|
|
416
|
+
this.copyWidth = controlWidth;
|
|
417
|
+
this.copyColumn = this.contentWidth - controlWidth;
|
|
418
|
+
row = (row || " ".repeat(this.copyColumn)) + theme.style(copy, style);
|
|
419
|
+
}
|
|
420
|
+
lines.push(row);
|
|
385
421
|
}
|
|
386
|
-
this.formatted = { width, time, copy, gutter, theme, version: this.nativeCache.version, top: this.top, lines };
|
|
422
|
+
this.formatted = { width, time, background, copy, goTo, gutter, theme, version: this.nativeCache.version, top: this.top, lines };
|
|
387
423
|
return lines;
|
|
388
424
|
}
|
|
389
425
|
|
|
390
426
|
override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
|
|
391
427
|
const target = { component: this, originX: event.screenX - event.x, originY: event.screenY - event.y,
|
|
392
428
|
width: event.width, height: event.height };
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
event.x
|
|
396
|
-
|
|
397
|
-
this.
|
|
429
|
+
const footer = this.copyRow !== undefined && event.y === this.leadingRows + this.copyRow;
|
|
430
|
+
const within = (column: number | undefined, width: number): boolean =>
|
|
431
|
+
column !== undefined && event.x >= column && event.x < column + width;
|
|
432
|
+
const control = event.button !== "left" ? undefined :
|
|
433
|
+
footer && within(this.copyColumn, this.copyWidth) ? "copy" as const :
|
|
434
|
+
footer && within(this.goToColumn, this.goToWidth) ? "goTo" as const :
|
|
435
|
+
this.hasClock && this.goTo?.clock() && event.y === this.leadingRows && event.x < this.clockWidth ? "clock" as const : undefined;
|
|
436
|
+
if (control && event.type === "press") {
|
|
437
|
+
this.pressed = control;
|
|
398
438
|
return { handled: true, capture: true, target };
|
|
399
439
|
}
|
|
400
|
-
if (
|
|
401
|
-
this.
|
|
402
|
-
this.copy?.();
|
|
440
|
+
if (control && event.type === "click" && (this.pressed === undefined || this.pressed === control)) {
|
|
441
|
+
this.pressed = undefined;
|
|
442
|
+
if (control === "copy") this.copy?.();
|
|
443
|
+
else this.goTo?.go();
|
|
403
444
|
return { handled: true, target };
|
|
404
445
|
}
|
|
405
|
-
if (this.
|
|
406
|
-
|
|
446
|
+
if (this.pressed && (event.type === "drag" || event.type === "release" || event.type === "click")) {
|
|
447
|
+
// Pi sends the release, then a click when the pointer did not move; a click or the next gesture ends this one.
|
|
448
|
+
if (event.type === "click") this.pressed = undefined;
|
|
407
449
|
return { handled: true, target };
|
|
408
450
|
}
|
|
451
|
+
if (event.type === "press") this.pressed = undefined;
|
|
409
452
|
const bodyRow = event.y - this.leadingRows - this.headerRows;
|
|
410
453
|
if (bodyRow < 0 || bodyRow >= this.bodyRows || event.x >= this.contentWidth) return undefined;
|
|
411
454
|
return super.handleMouse({
|
package/src/turns.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { backgroundAnsi, colorToOkhsl, colorToRgb, foregroundAnsi, okhslColor, rgbColor, type Color, type RgbColor, type TUI } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/** Hues of the turn palette; consecutive turns step by a stride coprime with it, so neighbours differ strongly. */
|
|
5
|
+
const HUES = 16;
|
|
6
|
+
const STRIDE = 7;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Colors of one turn for each mark, layered over what lies beneath it; `undefined` for blocks before the first turn.
|
|
10
|
+
* Layers stack from the terminal background up: the turn background, then hover, then stripes, lines and clocks.
|
|
11
|
+
* Each layer adds its share of the turn's hue to the layers below, so a clock stays darker than a hovered block.
|
|
12
|
+
*/
|
|
13
|
+
export interface TurnColor {
|
|
14
|
+
/** Background of the block's clock, on top of the block's layers. */
|
|
15
|
+
clock: Color;
|
|
16
|
+
/** Background of default cells: the turn background and hover layers; none when neither applies. */
|
|
17
|
+
fill?: string;
|
|
18
|
+
/** Background of the one-cell block stripe on the left, on top of the block's layers. */
|
|
19
|
+
stripe: string;
|
|
20
|
+
/** Foreground of the line stripe and the separator rule, on top of the block's layers. */
|
|
21
|
+
line: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Which layers lie under a block's marks: the turn background, and hover while the turn is under the pointer. */
|
|
25
|
+
export interface TurnLayers {
|
|
26
|
+
background: boolean;
|
|
27
|
+
hover: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const NO_LAYERS: TurnLayers = { background: false, hover: false };
|
|
31
|
+
|
|
32
|
+
/** Percent of its color each layer adds on top of the layers below it. */
|
|
33
|
+
export interface TurnMix {
|
|
34
|
+
clock: number;
|
|
35
|
+
stripe: number;
|
|
36
|
+
background: number;
|
|
37
|
+
hover: number;
|
|
38
|
+
/** The hover layer's color: the turn's own hue, or one color for every turn. */
|
|
39
|
+
hoverColor?: RgbColor;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Default strengths, as the settings default to; without `hoverColor` the hover layer takes the turn's hue. */
|
|
43
|
+
export const DEFAULT_MIX: TurnMix = { clock: 10, stripe: 20, background: 8, hover: 10 };
|
|
44
|
+
|
|
45
|
+
const palettes = new WeakMap<Theme, { background?: RgbColor; mix: TurnMix; colors: Map<string, TurnColor[]> }>();
|
|
46
|
+
|
|
47
|
+
const sameRgb = (a: RgbColor | undefined, b: RgbColor | undefined): boolean =>
|
|
48
|
+
a === b || (a !== undefined && b !== undefined && a.r === b.r && a.g === b.g && a.b === b.b);
|
|
49
|
+
|
|
50
|
+
/** Common dark and light terminal backgrounds, for a terminal that does not report its own. */
|
|
51
|
+
const GUESSED_BACKGROUND = { dark: rgbColor(24, 24, 27), light: rgbColor(250, 250, 250) };
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The terminal background moved toward each layer's color by that layer's share. Layers add up: in one color, the
|
|
55
|
+
* shares sum; together they never pass the full colors, so the result stays between the background and them.
|
|
56
|
+
*/
|
|
57
|
+
function layered(base: RgbColor, parts: readonly { color: RgbColor; share: number }[]): Color {
|
|
58
|
+
const total = parts.reduce((sum, part) => sum + part.share, 0);
|
|
59
|
+
const scale = total > 1 ? 1 / total : 1;
|
|
60
|
+
const channel = (key: "r" | "g" | "b"): number => Math.round(Math.min(255, Math.max(0,
|
|
61
|
+
base[key] + parts.reduce((sum, part) => sum + part.share * scale * (part.color[key] - base[key]), 0))));
|
|
62
|
+
return rgbColor(channel("r"), channel("g"), channel("b"));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The palette for a theme over the terminal's background. Layers are additive: a mark's color moves the terminal
|
|
67
|
+
* background toward each layer under it and then toward its own color, by each layer's share, so every layer stays
|
|
68
|
+
* visible over the ones below. In one hue the shares simply sum, capped at the full hue. Without a reported
|
|
69
|
+
* background, the theme's appearance picks a guess.
|
|
70
|
+
*/
|
|
71
|
+
export function turnColor(theme: Theme, turn: number, background?: RgbColor, mix: TurnMix = DEFAULT_MIX,
|
|
72
|
+
layers: TurnLayers = NO_LAYERS): TurnColor | undefined {
|
|
73
|
+
if (turn <= 0) return undefined;
|
|
74
|
+
let palette = palettes.get(theme);
|
|
75
|
+
if (!palette || palette.background !== background || palette.mix.clock !== mix.clock || palette.mix.stripe !== mix.stripe ||
|
|
76
|
+
palette.mix.background !== mix.background || palette.mix.hover !== mix.hover || !sameRgb(palette.mix.hoverColor, mix.hoverColor)) {
|
|
77
|
+
palette = { background, mix: { ...mix }, colors: new Map() };
|
|
78
|
+
palettes.set(theme, palette);
|
|
79
|
+
}
|
|
80
|
+
const key = (layers.background ? "b" : "") + (layers.hover ? "h" : "");
|
|
81
|
+
let colors = palette.colors.get(key);
|
|
82
|
+
if (!colors) {
|
|
83
|
+
const appearance = Reflect.get(theme, "appearance") === "light" ? "light" : "dark";
|
|
84
|
+
const base: Color = background ? rgbColor(background.r, background.g, background.b) : GUESSED_BACKGROUND[appearance];
|
|
85
|
+
const baseRgb = colorToRgb(base);
|
|
86
|
+
const dark = colorToOkhsl(base).l < 0.5;
|
|
87
|
+
const mode = theme.getColorMode();
|
|
88
|
+
const share = (percent: number): number => Math.min(100, Math.max(0, percent)) / 100;
|
|
89
|
+
colors = [];
|
|
90
|
+
for (let index = 0; index < HUES; index++) {
|
|
91
|
+
const hue = colorToRgb(okhslColor((index * 360) / HUES + 15, 0.85, dark ? 0.62 : 0.55));
|
|
92
|
+
const hover = mix.hoverColor ?? hue;
|
|
93
|
+
const under = [
|
|
94
|
+
...(layers.background ? [{ color: hue, share: share(mix.background) }] : []),
|
|
95
|
+
...(layers.hover ? [{ color: hover, share: share(mix.hover) }] : []),
|
|
96
|
+
];
|
|
97
|
+
const stripe = layered(baseRgb, [...under, { color: hue, share: share(mix.stripe) }]);
|
|
98
|
+
colors.push({
|
|
99
|
+
clock: layered(baseRgb, [...under, { color: hue, share: share(mix.clock) }]),
|
|
100
|
+
fill: under.length ? backgroundAnsi(layered(baseRgb, under), mode) : undefined,
|
|
101
|
+
stripe: backgroundAnsi(stripe, mode),
|
|
102
|
+
line: foregroundAnsi(stripe, mode),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
palette.colors.set(key, colors);
|
|
106
|
+
}
|
|
107
|
+
return colors[((turn - 1) * STRIDE) % HUES];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A `#rrggbb` color, or undefined for anything else. */
|
|
111
|
+
export function parseHex(value: string): RgbColor | undefined {
|
|
112
|
+
const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(value.trim());
|
|
113
|
+
return match ? { r: parseInt(match[1], 16), g: parseInt(match[2], 16), b: parseInt(match[3], 16) } : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** SGR parameters that take arguments: extended foreground, background and underline colors. */
|
|
117
|
+
const EXTENDED = new Set(["38", "48", "58"]);
|
|
118
|
+
// eslint-disable-next-line no-control-regex
|
|
119
|
+
const SGR = /\x1b\[([0-9;:]*)m/g;
|
|
120
|
+
// eslint-disable-next-line no-control-regex
|
|
121
|
+
const ZONES = /^(?:\x1b\]133;[ABC](?:\x07|\x1b\\))+/;
|
|
122
|
+
|
|
123
|
+
/** Whether a sequence leaves the default background active. */
|
|
124
|
+
function resetsBackground(parameters: string): boolean {
|
|
125
|
+
if (parameters === "") return true;
|
|
126
|
+
const parts = parameters.split(";");
|
|
127
|
+
let reset = false;
|
|
128
|
+
for (let index = 0; index < parts.length; index++) {
|
|
129
|
+
const part = parts[index];
|
|
130
|
+
const code = Number(part.split(":")[0] || "0");
|
|
131
|
+
if (code === 0 || code === 49) reset = true;
|
|
132
|
+
else if (code === 48 || (code >= 40 && code <= 47) || (code >= 100 && code <= 107)) reset = false;
|
|
133
|
+
if (EXTENDED.has(part)) index += parts[index + 1] === "5" ? 2 : parts[index + 1] === "2" ? 4 : 0;
|
|
134
|
+
}
|
|
135
|
+
return reset;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Give every cell with the default background a color and put a prefix before the row. Native backgrounds stay as
|
|
140
|
+
* they are; image rows keep their placement, and shell zone markers stay at the start of the row.
|
|
141
|
+
*/
|
|
142
|
+
export function decorateLine(line: string, background: string | undefined, prefix = ""): string {
|
|
143
|
+
if (line.includes("\x1b_G") || line.includes("\x1b]1337;")) return prefix + line;
|
|
144
|
+
const zones = ZONES.exec(line)?.[0] ?? "";
|
|
145
|
+
const body = zones ? line.slice(zones.length) : line;
|
|
146
|
+
if (!background) return zones + prefix + body;
|
|
147
|
+
return zones + prefix + background + body.replace(SGR, (sequence, parameters: string) =>
|
|
148
|
+
resetsBackground(parameters) ? sequence + background : sequence);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const decorated = new WeakMap<readonly string[], { background?: string; prefix: string; source: string[]; lines: string[] }>();
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Decorate rows, reusing the result while a renderer returns the same rows. Native renderers may mutate and return
|
|
155
|
+
* the same array, so the cache compares the rows themselves, not only the array.
|
|
156
|
+
*/
|
|
157
|
+
export function decorateLines(lines: readonly string[], background: string | undefined, prefix = ""): readonly string[] {
|
|
158
|
+
if (!background && !prefix) return lines;
|
|
159
|
+
const cached = decorated.get(lines);
|
|
160
|
+
if (cached && cached.background === background && cached.prefix === prefix && cached.source.length === lines.length &&
|
|
161
|
+
cached.source.every((line, index) => line === lines[index])) return cached.lines;
|
|
162
|
+
const result = lines.map(line => decorateLine(line, background, prefix));
|
|
163
|
+
decorated.set(lines, { background, prefix, source: [...lines], lines: result });
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Pi reports pointer motion without a pressed button only outside terminal multiplexers, where it enables all-motion
|
|
169
|
+
* mouse tracking, and only while its mouse support is on. The terminal itself cannot be asked.
|
|
170
|
+
*/
|
|
171
|
+
export function pointerMotionAvailable(tui: TUI | undefined, env?: NodeJS.ProcessEnv): boolean {
|
|
172
|
+
if (tui && Reflect.get(tui, "mouseEnabled") === false) return false;
|
|
173
|
+
// Every frame asks; the environment Pi chose its mouse mode from does not change.
|
|
174
|
+
if (env) return outsideMultiplexer(env);
|
|
175
|
+
return processMotion ??= outsideMultiplexer(process.env);
|
|
176
|
+
}
|
|
177
|
+
let processMotion: boolean | undefined;
|
|
178
|
+
function outsideMultiplexer(env: NodeJS.ProcessEnv): boolean {
|
|
179
|
+
const term = env.TERM?.toLowerCase() ?? "";
|
|
180
|
+
return env.TMUX === undefined && env.ZELLIJ === undefined && env.STY === undefined &&
|
|
181
|
+
!term.startsWith("tmux") && !term.startsWith("screen");
|
|
182
|
+
}
|