@mariozechner/pi-coding-agent 0.45.5 → 0.45.6

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.
@@ -0,0 +1,881 @@
1
+ /**
2
+ * Overlay QA Tests - comprehensive overlay positioning and edge case tests
3
+ *
4
+ * Usage: pi --extension ./examples/extensions/overlay-qa-tests.ts
5
+ *
6
+ * Commands:
7
+ * /overlay-animation - Real-time animation demo (~30 FPS, proves DOOM-like rendering works)
8
+ * /overlay-anchors - Cycle through all 9 anchor positions
9
+ * /overlay-margins - Test margin and offset options
10
+ * /overlay-stack - Test stacked overlays
11
+ * /overlay-overflow - Test width overflow with streaming process output
12
+ * /overlay-edge - Test overlay positioned at terminal edge
13
+ * /overlay-percent - Test percentage-based positioning
14
+ * /overlay-maxheight - Test maxHeight truncation
15
+ * /overlay-sidepanel - Responsive sidepanel (hides when terminal < 100 cols)
16
+ * /overlay-toggle - Toggle visibility demo (demonstrates OverlayHandle.setHidden)
17
+ */
18
+
19
+ import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@mariozechner/pi-coding-agent";
20
+ import type { OverlayAnchor, OverlayHandle, OverlayOptions, TUI } from "@mariozechner/pi-tui";
21
+ import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
22
+ import { spawn } from "child_process";
23
+
24
+ // Global handle for toggle demo (in real code, use a more elegant pattern)
25
+ let globalToggleHandle: OverlayHandle | null = null;
26
+
27
+ export default function (pi: ExtensionAPI) {
28
+ // Animation demo - proves overlays can handle real-time updates (like pi-doom would need)
29
+ pi.registerCommand("overlay-animation", {
30
+ description: "Test real-time animation in overlay (~30 FPS)",
31
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
32
+ await ctx.ui.custom<void>((tui, theme, _kb, done) => new AnimationDemoComponent(tui, theme, done), {
33
+ overlay: true,
34
+ overlayOptions: { anchor: "center", width: 50, maxHeight: 20 },
35
+ });
36
+ },
37
+ });
38
+
39
+ // Test all 9 anchor positions
40
+ pi.registerCommand("overlay-anchors", {
41
+ description: "Cycle through all anchor positions",
42
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
43
+ const anchors: OverlayAnchor[] = [
44
+ "top-left",
45
+ "top-center",
46
+ "top-right",
47
+ "left-center",
48
+ "center",
49
+ "right-center",
50
+ "bottom-left",
51
+ "bottom-center",
52
+ "bottom-right",
53
+ ];
54
+
55
+ let index = 0;
56
+ while (true) {
57
+ const result = await ctx.ui.custom<"next" | "confirm" | "cancel">(
58
+ (_tui, theme, _kb, done) => new AnchorTestComponent(theme, anchors[index]!, done),
59
+ {
60
+ overlay: true,
61
+ overlayOptions: { anchor: anchors[index], width: 40 },
62
+ },
63
+ );
64
+
65
+ if (result === "next") {
66
+ index = (index + 1) % anchors.length;
67
+ continue;
68
+ }
69
+ if (result === "confirm") {
70
+ ctx.ui.notify(`Selected: ${anchors[index]}`, "info");
71
+ }
72
+ break;
73
+ }
74
+ },
75
+ });
76
+
77
+ // Test margins and offsets
78
+ pi.registerCommand("overlay-margins", {
79
+ description: "Test margin and offset options",
80
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
81
+ const configs: { name: string; options: OverlayOptions }[] = [
82
+ { name: "No margin (top-left)", options: { anchor: "top-left", width: 35 } },
83
+ { name: "Margin: 3 all sides", options: { anchor: "top-left", width: 35, margin: 3 } },
84
+ {
85
+ name: "Margin: top=5, left=10",
86
+ options: { anchor: "top-left", width: 35, margin: { top: 5, left: 10 } },
87
+ },
88
+ { name: "Center + offset (10, -3)", options: { anchor: "center", width: 35, offsetX: 10, offsetY: -3 } },
89
+ { name: "Bottom-right, margin: 2", options: { anchor: "bottom-right", width: 35, margin: 2 } },
90
+ ];
91
+
92
+ let index = 0;
93
+ while (true) {
94
+ const result = await ctx.ui.custom<"next" | "close">(
95
+ (_tui, theme, _kb, done) => new MarginTestComponent(theme, configs[index]!, done),
96
+ {
97
+ overlay: true,
98
+ overlayOptions: configs[index]!.options,
99
+ },
100
+ );
101
+
102
+ if (result === "next") {
103
+ index = (index + 1) % configs.length;
104
+ continue;
105
+ }
106
+ break;
107
+ }
108
+ },
109
+ });
110
+
111
+ // Test stacked overlays
112
+ pi.registerCommand("overlay-stack", {
113
+ description: "Test stacked overlays",
114
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
115
+ // Three large overlays that overlap in the center area
116
+ // Each offset slightly so you can see the stacking
117
+
118
+ ctx.ui.notify("Showing overlay 1 (back)...", "info");
119
+ const p1 = ctx.ui.custom<string>(
120
+ (_tui, theme, _kb, done) => new StackOverlayComponent(theme, 1, "back (red border)", done),
121
+ {
122
+ overlay: true,
123
+ overlayOptions: { anchor: "center", width: 50, offsetX: -8, offsetY: -4, maxHeight: 15 },
124
+ },
125
+ );
126
+
127
+ await sleep(400);
128
+
129
+ ctx.ui.notify("Showing overlay 2 (middle)...", "info");
130
+ const p2 = ctx.ui.custom<string>(
131
+ (_tui, theme, _kb, done) => new StackOverlayComponent(theme, 2, "middle (green border)", done),
132
+ {
133
+ overlay: true,
134
+ overlayOptions: { anchor: "center", width: 50, offsetX: 0, offsetY: 0, maxHeight: 15 },
135
+ },
136
+ );
137
+
138
+ await sleep(400);
139
+
140
+ ctx.ui.notify("Showing overlay 3 (front)...", "info");
141
+ const p3 = ctx.ui.custom<string>(
142
+ (_tui, theme, _kb, done) => new StackOverlayComponent(theme, 3, "front (blue border)", done),
143
+ {
144
+ overlay: true,
145
+ overlayOptions: { anchor: "center", width: 50, offsetX: 8, offsetY: 4, maxHeight: 15 },
146
+ },
147
+ );
148
+
149
+ // Wait for all to close
150
+ const results = await Promise.all([p1, p2, p3]);
151
+ ctx.ui.notify(`Closed in order: ${results.join(", ")}`, "info");
152
+ },
153
+ });
154
+
155
+ // Test width overflow scenarios (original crash case) - streams real process output
156
+ pi.registerCommand("overlay-overflow", {
157
+ description: "Test width overflow with streaming process output",
158
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
159
+ await ctx.ui.custom<void>((tui, theme, _kb, done) => new StreamingOverflowComponent(tui, theme, done), {
160
+ overlay: true,
161
+ overlayOptions: { anchor: "center", width: 90, maxHeight: 20 },
162
+ });
163
+ },
164
+ });
165
+
166
+ // Test overlay at terminal edge
167
+ pi.registerCommand("overlay-edge", {
168
+ description: "Test overlay positioned at terminal edge",
169
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
170
+ await ctx.ui.custom<void>((_tui, theme, _kb, done) => new EdgeTestComponent(theme, done), {
171
+ overlay: true,
172
+ overlayOptions: { anchor: "right-center", width: 40, margin: { right: 0 } },
173
+ });
174
+ },
175
+ });
176
+
177
+ // Test percentage-based positioning
178
+ pi.registerCommand("overlay-percent", {
179
+ description: "Test percentage-based positioning",
180
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
181
+ const configs = [
182
+ { name: "rowPercent: 0 (top)", row: 0, col: 50 },
183
+ { name: "rowPercent: 50 (middle)", row: 50, col: 50 },
184
+ { name: "rowPercent: 100 (bottom)", row: 100, col: 50 },
185
+ { name: "colPercent: 0 (left)", row: 50, col: 0 },
186
+ { name: "colPercent: 100 (right)", row: 50, col: 100 },
187
+ ];
188
+
189
+ let index = 0;
190
+ while (true) {
191
+ const config = configs[index]!;
192
+ const result = await ctx.ui.custom<"next" | "close">(
193
+ (_tui, theme, _kb, done) => new PercentTestComponent(theme, config, done),
194
+ {
195
+ overlay: true,
196
+ overlayOptions: {
197
+ width: 30,
198
+ row: `${config.row}%`,
199
+ col: `${config.col}%`,
200
+ },
201
+ },
202
+ );
203
+
204
+ if (result === "next") {
205
+ index = (index + 1) % configs.length;
206
+ continue;
207
+ }
208
+ break;
209
+ }
210
+ },
211
+ });
212
+
213
+ // Test maxHeight
214
+ pi.registerCommand("overlay-maxheight", {
215
+ description: "Test maxHeight truncation",
216
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
217
+ await ctx.ui.custom<void>((_tui, theme, _kb, done) => new MaxHeightTestComponent(theme, done), {
218
+ overlay: true,
219
+ overlayOptions: { anchor: "center", width: 50, maxHeight: 10 },
220
+ });
221
+ },
222
+ });
223
+
224
+ // Test responsive sidepanel - only shows when terminal is wide enough
225
+ pi.registerCommand("overlay-sidepanel", {
226
+ description: "Test responsive sidepanel (hides when terminal < 100 cols)",
227
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
228
+ await ctx.ui.custom<void>((tui, theme, _kb, done) => new SidepanelComponent(tui, theme, done), {
229
+ overlay: true,
230
+ overlayOptions: {
231
+ anchor: "right-center",
232
+ width: "25%",
233
+ minWidth: 30,
234
+ margin: { right: 1 },
235
+ // Only show when terminal is wide enough
236
+ visible: (termWidth) => termWidth >= 100,
237
+ },
238
+ });
239
+ },
240
+ });
241
+
242
+ // Test toggle overlay - demonstrates OverlayHandle.setHidden() via onHandle callback
243
+ pi.registerCommand("overlay-toggle", {
244
+ description: "Test overlay toggle (press 't' to toggle visibility)",
245
+ handler: async (_args: string, ctx: ExtensionCommandContext) => {
246
+ await ctx.ui.custom<void>((tui, theme, _kb, done) => new ToggleDemoComponent(tui, theme, done), {
247
+ overlay: true,
248
+ overlayOptions: { anchor: "center", width: 50 },
249
+ // onHandle callback provides access to the OverlayHandle for visibility control
250
+ onHandle: (handle) => {
251
+ // Store handle globally so component can access it
252
+ // (In real code, you'd use a more elegant pattern like a store or event emitter)
253
+ globalToggleHandle = handle;
254
+ },
255
+ });
256
+ globalToggleHandle = null;
257
+ },
258
+ });
259
+ }
260
+
261
+ function sleep(ms: number): Promise<void> {
262
+ return new Promise((resolve) => setTimeout(resolve, ms));
263
+ }
264
+
265
+ // Base overlay component with common rendering
266
+ abstract class BaseOverlay {
267
+ constructor(protected theme: Theme) {}
268
+
269
+ protected box(lines: string[], width: number, title?: string): string[] {
270
+ const th = this.theme;
271
+ const innerW = Math.max(1, width - 2);
272
+ const result: string[] = [];
273
+
274
+ const titleStr = title ? truncateToWidth(` ${title} `, innerW) : "";
275
+ const titleW = visibleWidth(titleStr);
276
+ const topLine = "─".repeat(Math.floor((innerW - titleW) / 2));
277
+ const topLine2 = "─".repeat(Math.max(0, innerW - titleW - topLine.length));
278
+ result.push(th.fg("border", `╭${topLine}`) + th.fg("accent", titleStr) + th.fg("border", `${topLine2}╮`));
279
+
280
+ for (const line of lines) {
281
+ result.push(th.fg("border", "│") + truncateToWidth(line, innerW, "...", true) + th.fg("border", "│"));
282
+ }
283
+
284
+ result.push(th.fg("border", `╰${"─".repeat(innerW)}╯`));
285
+ return result;
286
+ }
287
+
288
+ invalidate(): void {}
289
+ dispose(): void {}
290
+ }
291
+
292
+ // Anchor position test
293
+ class AnchorTestComponent extends BaseOverlay {
294
+ constructor(
295
+ theme: Theme,
296
+ private anchor: OverlayAnchor,
297
+ private done: (result: "next" | "confirm" | "cancel") => void,
298
+ ) {
299
+ super(theme);
300
+ }
301
+
302
+ handleInput(data: string): void {
303
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
304
+ this.done("cancel");
305
+ } else if (matchesKey(data, "return")) {
306
+ this.done("confirm");
307
+ } else if (matchesKey(data, "space") || matchesKey(data, "right")) {
308
+ this.done("next");
309
+ }
310
+ }
311
+
312
+ render(width: number): string[] {
313
+ const th = this.theme;
314
+ return this.box(
315
+ [
316
+ "",
317
+ ` Current: ${th.fg("accent", this.anchor)}`,
318
+ "",
319
+ ` ${th.fg("dim", "Space/→ = next anchor")}`,
320
+ ` ${th.fg("dim", "Enter = confirm")}`,
321
+ ` ${th.fg("dim", "Esc = cancel")}`,
322
+ "",
323
+ ],
324
+ width,
325
+ "Anchor Test",
326
+ );
327
+ }
328
+ }
329
+
330
+ // Margin/offset test
331
+ class MarginTestComponent extends BaseOverlay {
332
+ constructor(
333
+ theme: Theme,
334
+ private config: { name: string; options: OverlayOptions },
335
+ private done: (result: "next" | "close") => void,
336
+ ) {
337
+ super(theme);
338
+ }
339
+
340
+ handleInput(data: string): void {
341
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
342
+ this.done("close");
343
+ } else if (matchesKey(data, "space") || matchesKey(data, "right")) {
344
+ this.done("next");
345
+ }
346
+ }
347
+
348
+ render(width: number): string[] {
349
+ const th = this.theme;
350
+ return this.box(
351
+ [
352
+ "",
353
+ ` ${th.fg("accent", this.config.name)}`,
354
+ "",
355
+ ` ${th.fg("dim", "Space/→ = next config")}`,
356
+ ` ${th.fg("dim", "Esc = close")}`,
357
+ "",
358
+ ],
359
+ width,
360
+ "Margin Test",
361
+ );
362
+ }
363
+ }
364
+
365
+ // Stacked overlay test
366
+ class StackOverlayComponent extends BaseOverlay {
367
+ constructor(
368
+ theme: Theme,
369
+ private num: number,
370
+ private position: string,
371
+ private done: (result: string) => void,
372
+ ) {
373
+ super(theme);
374
+ }
375
+
376
+ handleInput(data: string): void {
377
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c") || matchesKey(data, "return")) {
378
+ this.done(`Overlay ${this.num}`);
379
+ }
380
+ }
381
+
382
+ render(width: number): string[] {
383
+ const th = this.theme;
384
+ // Use different colors for each overlay to show stacking
385
+ const colors = ["error", "success", "accent"] as const;
386
+ const color = colors[(this.num - 1) % colors.length]!;
387
+ const innerW = Math.max(1, width - 2);
388
+ const border = (char: string) => th.fg(color, char);
389
+ const padLine = (s: string) => truncateToWidth(s, innerW, "...", true);
390
+ const lines: string[] = [];
391
+
392
+ lines.push(border(`╭${"─".repeat(innerW)}╮`));
393
+ lines.push(border("│") + padLine(` Overlay ${th.fg("accent", `#${this.num}`)}`) + border("│"));
394
+ lines.push(border("│") + padLine(` Layer: ${th.fg(color, this.position)}`) + border("│"));
395
+ lines.push(border("│") + padLine("") + border("│"));
396
+ // Add extra lines to make it taller
397
+ for (let i = 0; i < 5; i++) {
398
+ lines.push(border("│") + padLine(` ${"░".repeat(innerW - 2)} `) + border("│"));
399
+ }
400
+ lines.push(border("│") + padLine("") + border("│"));
401
+ lines.push(border("│") + padLine(th.fg("dim", " Press Enter/Esc to close")) + border("│"));
402
+ lines.push(border(`╰${"─".repeat(innerW)}╯`));
403
+
404
+ return lines;
405
+ }
406
+ }
407
+
408
+ // Streaming overflow test - spawns real process with colored output (original crash scenario)
409
+ class StreamingOverflowComponent extends BaseOverlay {
410
+ private lines: string[] = [];
411
+ private proc: ReturnType<typeof spawn> | null = null;
412
+ private scrollOffset = 0;
413
+ private maxVisibleLines = 15;
414
+ private finished = false;
415
+ private disposed = false;
416
+
417
+ constructor(
418
+ private tui: TUI,
419
+ theme: Theme,
420
+ private done: () => void,
421
+ ) {
422
+ super(theme);
423
+ this.startProcess();
424
+ }
425
+
426
+ private startProcess(): void {
427
+ // Run a command that produces many lines with ANSI colors
428
+ // Using find with -ls produces file listings, or use ls --color
429
+ this.proc = spawn("bash", [
430
+ "-c",
431
+ `
432
+ echo "Starting streaming overflow test (30+ seconds)..."
433
+ echo "This simulates subagent output with colors, hyperlinks, and long paths"
434
+ echo ""
435
+ for i in $(seq 1 100); do
436
+ # Simulate long file paths with OSC 8 hyperlinks (clickable) - tests width overflow
437
+ DIR="/Users/nicobailon/Documents/development/pi-mono/packages/coding-agent/src/modes/interactive"
438
+ FILE="\${DIR}/components/very-long-component-name-that-exceeds-width-\${i}.ts"
439
+ echo -e "\\033]8;;file://\${FILE}\\007▶ read: \${FILE}\\033]8;;\\007"
440
+
441
+ # Add some colored status messages with long text
442
+ if [ $((i % 5)) -eq 0 ]; then
443
+ echo -e " \\033[32m✓ Successfully processed \${i} files in /Users/nicobailon/Documents/development/pi-mono\\033[0m"
444
+ fi
445
+ if [ $((i % 7)) -eq 0 ]; then
446
+ echo -e " \\033[33m⚠ Warning: potential issue detected at line \${i} in very-long-component-name-that-exceeds-width.ts\\033[0m"
447
+ fi
448
+ if [ $((i % 11)) -eq 0 ]; then
449
+ echo -e " \\033[31m✗ Error: file not found /some/really/long/path/that/definitely/exceeds/the/overlay/width/limit/file-\${i}.ts\\033[0m"
450
+ fi
451
+ sleep 0.3
452
+ done
453
+ echo ""
454
+ echo -e "\\033[32m✓ Complete - 100 files processed in 30 seconds\\033[0m"
455
+ echo "Press Esc to close"
456
+ `,
457
+ ]);
458
+
459
+ this.proc.stdout?.on("data", (data: Buffer) => {
460
+ if (this.disposed) return; // Guard against callbacks after dispose
461
+ const text = data.toString();
462
+ const newLines = text.split("\n");
463
+ for (const line of newLines) {
464
+ if (line) this.lines.push(line);
465
+ }
466
+ // Auto-scroll to bottom
467
+ this.scrollOffset = Math.max(0, this.lines.length - this.maxVisibleLines);
468
+ this.tui.requestRender();
469
+ });
470
+
471
+ this.proc.stderr?.on("data", (data: Buffer) => {
472
+ if (this.disposed) return; // Guard against callbacks after dispose
473
+ this.lines.push(this.theme.fg("error", data.toString().trim()));
474
+ this.tui.requestRender();
475
+ });
476
+
477
+ this.proc.on("close", () => {
478
+ if (this.disposed) return; // Guard against callbacks after dispose
479
+ this.finished = true;
480
+ this.tui.requestRender();
481
+ });
482
+ }
483
+
484
+ handleInput(data: string): void {
485
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
486
+ this.proc?.kill();
487
+ this.done();
488
+ } else if (matchesKey(data, "up")) {
489
+ this.scrollOffset = Math.max(0, this.scrollOffset - 1);
490
+ this.tui.requestRender(); // Trigger re-render after scroll
491
+ } else if (matchesKey(data, "down")) {
492
+ this.scrollOffset = Math.min(Math.max(0, this.lines.length - this.maxVisibleLines), this.scrollOffset + 1);
493
+ this.tui.requestRender(); // Trigger re-render after scroll
494
+ }
495
+ }
496
+
497
+ render(width: number): string[] {
498
+ const th = this.theme;
499
+ const innerW = Math.max(1, width - 2);
500
+ const padLine = (s: string) => truncateToWidth(s, innerW, "...", true);
501
+ const border = (c: string) => th.fg("border", c);
502
+
503
+ const result: string[] = [];
504
+ const title = truncateToWidth(` Streaming Output (${this.lines.length} lines) `, innerW);
505
+ const titlePad = Math.max(0, innerW - visibleWidth(title));
506
+ result.push(border("╭") + th.fg("accent", title) + border(`${"─".repeat(titlePad)}╮`));
507
+
508
+ // Scroll indicators
509
+ const canScrollUp = this.scrollOffset > 0;
510
+ const canScrollDown = this.scrollOffset < this.lines.length - this.maxVisibleLines;
511
+ const scrollInfo = `↑${this.scrollOffset} | ↓${Math.max(0, this.lines.length - this.maxVisibleLines - this.scrollOffset)}`;
512
+
513
+ result.push(
514
+ border("│") + padLine(canScrollUp || canScrollDown ? th.fg("dim", ` ${scrollInfo}`) : "") + border("│"),
515
+ );
516
+
517
+ // Visible lines - truncate long lines to fit within border
518
+ const visibleLines = this.lines.slice(this.scrollOffset, this.scrollOffset + this.maxVisibleLines);
519
+ for (const line of visibleLines) {
520
+ result.push(border("│") + padLine(` ${line}`) + border("│"));
521
+ }
522
+
523
+ // Pad to maxVisibleLines
524
+ for (let i = visibleLines.length; i < this.maxVisibleLines; i++) {
525
+ result.push(border("│") + padLine("") + border("│"));
526
+ }
527
+
528
+ const status = this.finished ? th.fg("success", "✓ Done") : th.fg("warning", "● Running");
529
+ result.push(border("│") + padLine(` ${status} ${th.fg("dim", "| ↑↓ scroll | Esc close")}`) + border("│"));
530
+ result.push(border(`╰${"─".repeat(innerW)}╯`));
531
+
532
+ return result;
533
+ }
534
+
535
+ dispose(): void {
536
+ this.disposed = true;
537
+ this.proc?.kill();
538
+ }
539
+ }
540
+
541
+ // Edge position test
542
+ class EdgeTestComponent extends BaseOverlay {
543
+ constructor(
544
+ theme: Theme,
545
+ private done: () => void,
546
+ ) {
547
+ super(theme);
548
+ }
549
+
550
+ handleInput(data: string): void {
551
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
552
+ this.done();
553
+ }
554
+ }
555
+
556
+ render(width: number): string[] {
557
+ const th = this.theme;
558
+ return this.box(
559
+ [
560
+ "",
561
+ " This overlay is at the",
562
+ " right edge of terminal.",
563
+ "",
564
+ ` ${th.fg("dim", "Verify right border")}`,
565
+ ` ${th.fg("dim", "aligns with edge.")}`,
566
+ "",
567
+ ` ${th.fg("dim", "Press Esc to close")}`,
568
+ "",
569
+ ],
570
+ width,
571
+ "Edge Test",
572
+ );
573
+ }
574
+ }
575
+
576
+ // Percentage positioning test
577
+ class PercentTestComponent extends BaseOverlay {
578
+ constructor(
579
+ theme: Theme,
580
+ private config: { name: string; row: number; col: number },
581
+ private done: (result: "next" | "close") => void,
582
+ ) {
583
+ super(theme);
584
+ }
585
+
586
+ handleInput(data: string): void {
587
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
588
+ this.done("close");
589
+ } else if (matchesKey(data, "space") || matchesKey(data, "right")) {
590
+ this.done("next");
591
+ }
592
+ }
593
+
594
+ render(width: number): string[] {
595
+ const th = this.theme;
596
+ return this.box(
597
+ [
598
+ "",
599
+ ` ${th.fg("accent", this.config.name)}`,
600
+ "",
601
+ ` ${th.fg("dim", "Space/→ = next")}`,
602
+ ` ${th.fg("dim", "Esc = close")}`,
603
+ "",
604
+ ],
605
+ width,
606
+ "Percent Test",
607
+ );
608
+ }
609
+ }
610
+
611
+ // MaxHeight test - renders 20 lines, truncated to 10 by maxHeight
612
+ class MaxHeightTestComponent extends BaseOverlay {
613
+ constructor(
614
+ theme: Theme,
615
+ private done: () => void,
616
+ ) {
617
+ super(theme);
618
+ }
619
+
620
+ handleInput(data: string): void {
621
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
622
+ this.done();
623
+ }
624
+ }
625
+
626
+ render(width: number): string[] {
627
+ const th = this.theme;
628
+ // Intentionally render 21 lines - maxHeight: 10 will truncate to first 10
629
+ // You should see header + lines 1-6, with bottom border cut off
630
+ const contentLines: string[] = [
631
+ th.fg("warning", " ⚠ Rendering 21 lines, maxHeight: 10"),
632
+ th.fg("dim", " Lines 11-21 truncated (no bottom border)"),
633
+ "",
634
+ ];
635
+
636
+ for (let i = 1; i <= 14; i++) {
637
+ contentLines.push(` Line ${i} of 14`);
638
+ }
639
+
640
+ contentLines.push("", th.fg("dim", " Press Esc to close"));
641
+
642
+ return this.box(contentLines, width, "MaxHeight Test");
643
+ }
644
+ }
645
+
646
+ // Responsive sidepanel - demonstrates percentage width and visibility callback
647
+ class SidepanelComponent extends BaseOverlay {
648
+ private items = ["Dashboard", "Messages", "Settings", "Help", "About"];
649
+ private selectedIndex = 0;
650
+
651
+ constructor(
652
+ private tui: TUI,
653
+ theme: Theme,
654
+ private done: () => void,
655
+ ) {
656
+ super(theme);
657
+ }
658
+
659
+ handleInput(data: string): void {
660
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
661
+ this.done();
662
+ } else if (matchesKey(data, "up")) {
663
+ this.selectedIndex = Math.max(0, this.selectedIndex - 1);
664
+ this.tui.requestRender();
665
+ } else if (matchesKey(data, "down")) {
666
+ this.selectedIndex = Math.min(this.items.length - 1, this.selectedIndex + 1);
667
+ this.tui.requestRender();
668
+ } else if (matchesKey(data, "return")) {
669
+ // Could trigger an action here
670
+ this.tui.requestRender();
671
+ }
672
+ }
673
+
674
+ render(width: number): string[] {
675
+ const th = this.theme;
676
+ const innerW = Math.max(1, width - 2);
677
+ const padLine = (s: string) => truncateToWidth(s, innerW, "...", true);
678
+ const border = (c: string) => th.fg("border", c);
679
+ const lines: string[] = [];
680
+
681
+ // Header
682
+ lines.push(border(`╭${"─".repeat(innerW)}╮`));
683
+ lines.push(border("│") + padLine(th.fg("accent", " Responsive Sidepanel")) + border("│"));
684
+ lines.push(border("├") + border("─".repeat(innerW)) + border("┤"));
685
+
686
+ // Menu items
687
+ for (let i = 0; i < this.items.length; i++) {
688
+ const item = this.items[i]!;
689
+ const isSelected = i === this.selectedIndex;
690
+ const prefix = isSelected ? th.fg("accent", "→ ") : " ";
691
+ const text = isSelected ? th.fg("accent", item) : item;
692
+ lines.push(border("│") + padLine(`${prefix}${text}`) + border("│"));
693
+ }
694
+
695
+ // Footer with responsive behavior info
696
+ lines.push(border("├") + border("─".repeat(innerW)) + border("┤"));
697
+ lines.push(border("│") + padLine(th.fg("warning", " ⚠ Resize terminal < 100 cols")) + border("│"));
698
+ lines.push(border("│") + padLine(th.fg("warning", " to see panel auto-hide")) + border("│"));
699
+ lines.push(border("│") + padLine(th.fg("dim", " Uses visible: (w) => w >= 100")) + border("│"));
700
+ lines.push(border("│") + padLine(th.fg("dim", " ↑↓ navigate | Esc close")) + border("│"));
701
+ lines.push(border(`╰${"─".repeat(innerW)}╯`));
702
+
703
+ return lines;
704
+ }
705
+ }
706
+
707
+ // Animation demo - proves overlays can handle real-time updates like pi-doom
708
+ class AnimationDemoComponent extends BaseOverlay {
709
+ private frame = 0;
710
+ private interval: ReturnType<typeof setInterval> | null = null;
711
+ private fps = 0;
712
+ private lastFpsUpdate = Date.now();
713
+ private framesSinceLastFps = 0;
714
+
715
+ constructor(
716
+ private tui: TUI,
717
+ theme: Theme,
718
+ private done: () => void,
719
+ ) {
720
+ super(theme);
721
+ this.startAnimation();
722
+ }
723
+
724
+ private startAnimation(): void {
725
+ // Run at ~30 FPS (same as DOOM target)
726
+ this.interval = setInterval(() => {
727
+ this.frame++;
728
+ this.framesSinceLastFps++;
729
+
730
+ // Update FPS counter every second
731
+ const now = Date.now();
732
+ if (now - this.lastFpsUpdate >= 1000) {
733
+ this.fps = this.framesSinceLastFps;
734
+ this.framesSinceLastFps = 0;
735
+ this.lastFpsUpdate = now;
736
+ }
737
+
738
+ this.tui.requestRender();
739
+ }, 1000 / 30);
740
+ }
741
+
742
+ handleInput(data: string): void {
743
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
744
+ this.dispose();
745
+ this.done();
746
+ }
747
+ }
748
+
749
+ render(width: number): string[] {
750
+ const th = this.theme;
751
+ const innerW = Math.max(1, width - 2);
752
+ const padLine = (s: string) => truncateToWidth(s, innerW, "...", true);
753
+ const border = (c: string) => th.fg("border", c);
754
+
755
+ const lines: string[] = [];
756
+ lines.push(border(`╭${"─".repeat(innerW)}╮`));
757
+ lines.push(border("│") + padLine(th.fg("accent", " Animation Demo (~30 FPS)")) + border("│"));
758
+ lines.push(border("│") + padLine(``) + border("│"));
759
+ lines.push(border("│") + padLine(` Frame: ${th.fg("accent", String(this.frame))}`) + border("│"));
760
+ lines.push(border("│") + padLine(` FPS: ${th.fg("success", String(this.fps))}`) + border("│"));
761
+ lines.push(border("│") + padLine(``) + border("│"));
762
+
763
+ // Animated content - bouncing bar
764
+ const barWidth = Math.max(12, innerW - 4); // Ensure enough space for bar
765
+ const pos = Math.max(0, Math.floor(((Math.sin(this.frame / 10) + 1) * (barWidth - 10)) / 2));
766
+ const bar = " ".repeat(pos) + th.fg("accent", "██████████") + " ".repeat(Math.max(0, barWidth - 10 - pos));
767
+ lines.push(border("│") + padLine(` ${bar}`) + border("│"));
768
+
769
+ // Spinning character
770
+ const spinChars = ["◐", "◓", "◑", "◒"];
771
+ const spin = spinChars[this.frame % spinChars.length];
772
+ lines.push(border("│") + padLine(` Spinner: ${th.fg("warning", spin!)}`) + border("│"));
773
+
774
+ // Color cycling
775
+ const hue = (this.frame * 3) % 360;
776
+ const rgb = hslToRgb(hue / 360, 0.8, 0.5);
777
+ const colorBlock = `\x1b[48;2;${rgb[0]};${rgb[1]};${rgb[2]}m${" ".repeat(10)}\x1b[0m`;
778
+ lines.push(border("│") + padLine(` Color: ${colorBlock}`) + border("│"));
779
+
780
+ lines.push(border("│") + padLine(``) + border("│"));
781
+ lines.push(border("│") + padLine(th.fg("dim", " This proves overlays can handle")) + border("│"));
782
+ lines.push(border("│") + padLine(th.fg("dim", " real-time game-like rendering.")) + border("│"));
783
+ lines.push(border("│") + padLine(th.fg("dim", " (pi-doom uses same approach)")) + border("│"));
784
+ lines.push(border("│") + padLine(``) + border("│"));
785
+ lines.push(border("│") + padLine(th.fg("dim", " Press Esc to close")) + border("│"));
786
+ lines.push(border(`╰${"─".repeat(innerW)}╯`));
787
+
788
+ return lines;
789
+ }
790
+
791
+ dispose(): void {
792
+ if (this.interval) {
793
+ clearInterval(this.interval);
794
+ this.interval = null;
795
+ }
796
+ }
797
+ }
798
+
799
+ // HSL to RGB helper for color cycling animation
800
+ function hslToRgb(h: number, s: number, l: number): [number, number, number] {
801
+ let r: number, g: number, b: number;
802
+ if (s === 0) {
803
+ r = g = b = l;
804
+ } else {
805
+ const hue2rgb = (p: number, q: number, t: number) => {
806
+ if (t < 0) t += 1;
807
+ if (t > 1) t -= 1;
808
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
809
+ if (t < 1 / 2) return q;
810
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
811
+ return p;
812
+ };
813
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
814
+ const p = 2 * l - q;
815
+ r = hue2rgb(p, q, h + 1 / 3);
816
+ g = hue2rgb(p, q, h);
817
+ b = hue2rgb(p, q, h - 1 / 3);
818
+ }
819
+ return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
820
+ }
821
+
822
+ // Toggle demo - demonstrates OverlayHandle.setHidden() via onHandle callback
823
+ class ToggleDemoComponent extends BaseOverlay {
824
+ private toggleCount = 0;
825
+ private isToggling = false;
826
+
827
+ constructor(
828
+ private tui: TUI,
829
+ theme: Theme,
830
+ private done: () => void,
831
+ ) {
832
+ super(theme);
833
+ }
834
+
835
+ handleInput(data: string): void {
836
+ if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
837
+ this.done();
838
+ } else if (matchesKey(data, "t") && globalToggleHandle && !this.isToggling) {
839
+ // Demonstrate toggle by hiding for 1 second then showing again
840
+ // (In real usage, a global keybinding would control visibility)
841
+ this.isToggling = true;
842
+ this.toggleCount++;
843
+ globalToggleHandle.setHidden(true);
844
+
845
+ // Auto-restore after 1 second to demonstrate the API
846
+ setTimeout(() => {
847
+ if (globalToggleHandle) {
848
+ globalToggleHandle.setHidden(false);
849
+ this.isToggling = false;
850
+ this.tui.requestRender();
851
+ }
852
+ }, 1000);
853
+ }
854
+ }
855
+
856
+ render(width: number): string[] {
857
+ const th = this.theme;
858
+ return this.box(
859
+ [
860
+ "",
861
+ th.fg("accent", " Toggle Demo"),
862
+ "",
863
+ " This overlay demonstrates the",
864
+ " onHandle callback API.",
865
+ "",
866
+ ` Toggle count: ${th.fg("accent", String(this.toggleCount))}`,
867
+ "",
868
+ th.fg("dim", " Press 't' to hide for 1 second"),
869
+ th.fg("dim", " (demonstrates setHidden API)"),
870
+ "",
871
+ th.fg("dim", " In real usage, a global keybinding"),
872
+ th.fg("dim", " would toggle visibility externally."),
873
+ "",
874
+ th.fg("dim", " Press Esc to close"),
875
+ "",
876
+ ],
877
+ width,
878
+ "Toggle Demo",
879
+ );
880
+ }
881
+ }