overlay-test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Overlay Test - validates overlay compositing with inline text inputs
  3. *
  4. * Usage: pi --extension ./examples/extensions/overlay-test.ts
  5. *
  6. * Run /overlay-test to show a floating overlay with:
  7. * - Inline text inputs within menu items
  8. * - Edge case tests (wide chars, styled text, emoji)
  9. */
  10. import type { ExtensionAPI, ExtensionCommandContext, Theme } from "@earendil-works/pi-coding-agent";
  11. import { CURSOR_MARKER, type Focusable, matchesKey, visibleWidth } from "@earendil-works/pi-tui";
  12. export default function (pi: ExtensionAPI) {
  13. pi.registerCommand("overlay-test", {
  14. description: "Test overlay rendering with edge cases",
  15. handler: async (_args: string, ctx: ExtensionCommandContext) => {
  16. const result = await ctx.ui.custom<{ action: string; query?: string } | undefined>(
  17. (_tui, theme, _keybindings, done) => new OverlayTestComponent(theme, done),
  18. { overlay: true },
  19. );
  20. if (result) {
  21. const msg = result.query ? `${result.action}: "${result.query}"` : result.action;
  22. ctx.ui.notify(msg, "info");
  23. }
  24. },
  25. });
  26. }
  27. class OverlayTestComponent implements Focusable {
  28. readonly width = 70;
  29. /** Focusable interface - set by TUI when focus changes */
  30. focused = false;
  31. private selected = 0;
  32. private items = [
  33. { label: "Search", hasInput: true, text: "", cursor: 0 },
  34. { label: "Run", hasInput: true, text: "", cursor: 0 },
  35. { label: "Settings", hasInput: false, text: "", cursor: 0 },
  36. { label: "Cancel", hasInput: false, text: "", cursor: 0 },
  37. ];
  38. private theme: Theme;
  39. private done: (result: { action: string; query?: string } | undefined) => void;
  40. constructor(theme: Theme, done: (result: { action: string; query?: string } | undefined) => void) {
  41. this.theme = theme;
  42. this.done = done;
  43. }
  44. handleInput(data: string): void {
  45. if (matchesKey(data, "escape")) {
  46. this.done(undefined);
  47. return;
  48. }
  49. const current = this.items[this.selected]!;
  50. if (matchesKey(data, "return")) {
  51. this.done({ action: current.label, query: current.hasInput ? current.text : undefined });
  52. return;
  53. }
  54. if (matchesKey(data, "up")) {
  55. this.selected = Math.max(0, this.selected - 1);
  56. } else if (matchesKey(data, "down")) {
  57. this.selected = Math.min(this.items.length - 1, this.selected + 1);
  58. } else if (current.hasInput) {
  59. if (matchesKey(data, "backspace")) {
  60. if (current.cursor > 0) {
  61. current.text = current.text.slice(0, current.cursor - 1) + current.text.slice(current.cursor);
  62. current.cursor--;
  63. }
  64. } else if (matchesKey(data, "left")) {
  65. current.cursor = Math.max(0, current.cursor - 1);
  66. } else if (matchesKey(data, "right")) {
  67. current.cursor = Math.min(current.text.length, current.cursor + 1);
  68. } else if (data.length === 1 && data.charCodeAt(0) >= 32) {
  69. current.text = current.text.slice(0, current.cursor) + data + current.text.slice(current.cursor);
  70. current.cursor++;
  71. }
  72. }
  73. }
  74. render(_width: number): string[] {
  75. const w = this.width;
  76. const th = this.theme;
  77. const innerW = w - 2;
  78. const lines: string[] = [];
  79. const pad = (s: string, len: number) => {
  80. const vis = visibleWidth(s);
  81. return s + " ".repeat(Math.max(0, len - vis));
  82. };
  83. const row = (content: string) => th.fg("border", "│") + pad(content, innerW) + th.fg("border", "│");
  84. lines.push(th.fg("border", `╭${"─".repeat(innerW)}╮`));
  85. lines.push(row(` ${th.fg("accent", "🧪 Overlay Test")}`));
  86. lines.push(row(""));
  87. // Edge cases - full width lines to test compositing at boundaries
  88. lines.push(row(` ${th.fg("dim", "─── Edge Cases (borders should align) ───")}`));
  89. lines.push(row(` Wide: ${th.fg("warning", "中文日本語한글テスト漢字繁體简体ひらがなカタカナ가나다라마바")}`));
  90. lines.push(
  91. row(
  92. ` Styled: ${th.fg("error", "RED")} ${th.fg("success", "GREEN")} ${th.fg("warning", "YELLOW")} ${th.fg("accent", "ACCENT")} ${th.fg("dim", "DIM")} ${th.fg("error", "more")} ${th.fg("success", "colors")}`,
  93. ),
  94. );
  95. lines.push(row(" Emoji: 👨‍👩‍👧‍👦 🇯🇵 🚀 💻 🎉 🔥 😀 🎯 🌟 💡 🎨 🔧 📦 🏆 🌈 🎪 🎭 🎬 🎮 🎲"));
  96. lines.push(row(""));
  97. // Menu with inline inputs
  98. lines.push(row(` ${th.fg("dim", "─── Actions ───")}`));
  99. for (let i = 0; i < this.items.length; i++) {
  100. const item = this.items[i]!;
  101. const isSelected = i === this.selected;
  102. const prefix = isSelected ? " ▶ " : " ";
  103. let content: string;
  104. if (item.hasInput) {
  105. const label = isSelected ? th.fg("accent", `${item.label}:`) : th.fg("text", `${item.label}:`);
  106. let inputDisplay = item.text;
  107. if (isSelected) {
  108. const before = inputDisplay.slice(0, item.cursor);
  109. const cursorChar = item.cursor < inputDisplay.length ? inputDisplay[item.cursor] : " ";
  110. const after = inputDisplay.slice(item.cursor + 1);
  111. // Emit hardware cursor marker for IME support when focused
  112. const marker = this.focused ? CURSOR_MARKER : "";
  113. inputDisplay = `${before}${marker}\x1b[7m${cursorChar}\x1b[27m${after}`;
  114. }
  115. content = `${prefix + label} ${inputDisplay}`;
  116. } else {
  117. content = prefix + (isSelected ? th.fg("accent", item.label) : th.fg("text", item.label));
  118. }
  119. lines.push(row(content));
  120. }
  121. lines.push(row(""));
  122. lines.push(row(` ${th.fg("dim", "↑↓ navigate • type to input • Enter select • Esc cancel")}`));
  123. lines.push(th.fg("border", `╰${"─".repeat(innerW)}╯`));
  124. return lines;
  125. }
  126. invalidate(): void {}
  127. dispose(): void {}
  128. }