pi-render-btw 0.6.7 → 0.6.9

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/README.md CHANGED
@@ -93,6 +93,8 @@ pi install /path/to/pi-render-btw
93
93
 
94
94
  - `Alt+/` toggles focus between BTW and the main editor without closing the overlay
95
95
  - `Ctrl+Alt+W` is a fallback focus toggle for terminals that do not deliver `Alt+/` as a usable shortcut
96
+ - `Ctrl+G` opens the current BTW draft in `$VISUAL`/`$EDITOR` (falling back to `nano`) and reads it back when the editor exits successfully
97
+ - the composer uses Pi's native editor behavior for undo, paste, prompt history, and Tab file-path completion relative to the BTW working directory
96
98
  - `Esc` still dismisses BTW immediately while the overlay is focused
97
99
  - BTW opens top-centered so the main session remains visible underneath it
98
100
 
@@ -10,8 +10,10 @@ const theme = {
10
10
 
11
11
  const keybindings = { matches: () => false } as any;
12
12
  const tui = {
13
- terminal: { write: () => {} },
13
+ terminal: { write: () => {}, rows: 30 },
14
14
  requestRender: () => {},
15
+ start: () => {},
16
+ stop: () => {},
15
17
  } as any;
16
18
 
17
19
  function transcriptLines(entries: unknown[]): string[] {
@@ -119,4 +121,48 @@ describe("BTW transcript message layout", () => {
119
121
  component.handleInput("\x10"); // Ctrl+P
120
122
  expect(component.render(80).join("\n")).toContain("q:exit");
121
123
  });
124
+
125
+ it("setDraft/getDraft round-trips through editor getText/setText", () => {
126
+ const component = new BtwOverlayComponent(
127
+ tui,
128
+ theme,
129
+ keybindings,
130
+ () => [],
131
+ () => null,
132
+ () => "contextual",
133
+ () => {},
134
+ () => {},
135
+ () => {},
136
+ );
137
+
138
+ const testValue = "hello from the editor";
139
+ component.setDraft(testValue);
140
+ expect(component.getDraft()).toBe(testValue);
141
+ });
142
+
143
+ it("routes Ctrl+G to the external-editor callback", () => {
144
+ let opened = 0;
145
+ const externalKeybindings = {
146
+ matches: (data: string, action: string) =>
147
+ action === "app.editor.external" && data === "\x07",
148
+ } as any;
149
+ const component = new BtwOverlayComponent(
150
+ tui,
151
+ theme,
152
+ externalKeybindings,
153
+ () => [],
154
+ () => null,
155
+ () => "contextual",
156
+ () => {},
157
+ () => {},
158
+ () => {},
159
+ () => { opened++; },
160
+ );
161
+
162
+ component.setDraft("draft prompt");
163
+ component.handleInput("\x07"); // Ctrl+G
164
+
165
+ expect(opened).toBe(1);
166
+ expect(component.getDraft()).toBe("draft prompt");
167
+ });
122
168
  });
package/extensions/btw.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { spawnSync } from "node:child_process";
2
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
4
  import path from "node:path";
5
5
  import {
@@ -32,7 +32,8 @@ import {
32
32
  type OverlayHandle,
33
33
  type TUI,
34
34
  } from "@earendil-works/pi-tui";
35
- import { TextArea } from "./text-area.ts";
35
+ import { CustomEditor } from "@earendil-works/pi-coding-agent";
36
+ import { CombinedAutocompleteProvider, type EditorTheme } from "@earendil-works/pi-tui";
36
37
  import {
37
38
  buildPeekBlocks,
38
39
  createPeekState,
@@ -1038,7 +1039,7 @@ function buildTranscriptBadge(
1038
1039
  }
1039
1040
 
1040
1041
  export class BtwOverlayComponent extends Container implements Focusable {
1041
- private readonly input: TextArea;
1042
+ private readonly input: CustomEditor;
1042
1043
  private readonly transcript: Container;
1043
1044
  private readonly statusText: Text;
1044
1045
  private readonly modeText: Text;
@@ -1050,6 +1051,8 @@ export class BtwOverlayComponent extends Container implements Focusable {
1050
1051
  private readonly onSubmitCallback: (value: string) => void;
1051
1052
  private readonly onDismissCallback: () => void;
1052
1053
  private readonly onUnfocusCallback: () => void;
1054
+ private readonly onExternalEditorCallback?: () => void;
1055
+ private externalEditorActive = false;
1053
1056
  private readonly tui: TUI;
1054
1057
  private readonly theme: ExtensionContext["ui"]["theme"];
1055
1058
  private transcriptLines: string[] = [];
@@ -1089,6 +1092,8 @@ export class BtwOverlayComponent extends Container implements Focusable {
1089
1092
  onSubmit: (value: string) => void,
1090
1093
  onDismiss: () => void,
1091
1094
  onUnfocus: () => void,
1095
+ onExternalEditor?: () => void,
1096
+ cwd?: string,
1092
1097
  ) {
1093
1098
  super();
1094
1099
  this.tui = tui;
@@ -1099,14 +1104,29 @@ export class BtwOverlayComponent extends Container implements Focusable {
1099
1104
  this.onSubmitCallback = onSubmit;
1100
1105
  this.onDismissCallback = onDismiss;
1101
1106
  this.onUnfocusCallback = onUnfocus;
1107
+ this.onExternalEditorCallback = onExternalEditor;
1102
1108
 
1103
1109
  this.modeText = new Text("", 1, 0);
1104
1110
  this.summaryText = new Text("", 1, 0);
1105
1111
  this.transcript = new Container();
1106
1112
  this.statusText = new Text("", 1, 0);
1107
1113
 
1108
- this.input = new TextArea();
1114
+ const editorTheme: EditorTheme = {
1115
+ borderColor: (text: string) => theme.fg("borderMuted", text),
1116
+ selectList: {
1117
+ selectedPrefix: (text: string) => theme.fg("accent", text),
1118
+ selectedText: (text: string) => theme.fg("accent", text),
1119
+ description: (text: string) => theme.fg("muted", text),
1120
+ scrollInfo: (text: string) => theme.fg("muted", text),
1121
+ noMatch: (text: string) => theme.fg("muted", text),
1122
+ },
1123
+ };
1124
+ // Pi may expose KeybindingsManager from a different installed package copy;
1125
+ // both variants provide the runtime `matches()` interface CustomEditor uses.
1126
+ const editorKeybindings = keybindings as unknown as ConstructorParameters<typeof CustomEditor>[2];
1127
+ this.input = new CustomEditor(tui, editorTheme, editorKeybindings);
1109
1128
  this.input.onSubmit = (value) => {
1129
+ this.input.addToHistory(value);
1110
1130
  this.followTranscript = true;
1111
1131
  this.onSubmitCallback(value);
1112
1132
  };
@@ -1114,6 +1134,11 @@ export class BtwOverlayComponent extends Container implements Focusable {
1114
1134
  this.onDismissCallback();
1115
1135
  };
1116
1136
 
1137
+ // Wire up autocomplete (file-path completion only, no commands)
1138
+ const cwdPath = cwd ?? process.cwd();
1139
+ const autocompleteProvider = new CombinedAutocompleteProvider([], cwdPath);
1140
+ this.input.setAutocompleteProvider(autocompleteProvider);
1141
+
1117
1142
  this.peekState = createPeekState();
1118
1143
  this.hintsText = new Text("", 1, 0);
1119
1144
  this.mdTheme = getMarkdownTheme();
@@ -1123,25 +1148,65 @@ export class BtwOverlayComponent extends Container implements Focusable {
1123
1148
 
1124
1149
  const originalHandleInput = this.input.handleInput.bind(this.input);
1125
1150
  this.input.handleInput = (data: string) => {
1151
+ if (keybindings.matches(data, "app.editor.external")) {
1152
+ if (this.onExternalEditorCallback) {
1153
+ this.onExternalEditorCallback();
1154
+ } else {
1155
+ void this.openExternalEditor();
1156
+ }
1157
+ return;
1158
+ }
1159
+
1126
1160
  // app.clear with non-empty input → clear input
1127
- if (keybindings.matches(data, "app.clear") && this.input.getValue().length > 0) {
1128
- this.input.setValue("");
1161
+ if (keybindings.matches(data, "app.clear") && this.input.getText().length > 0) {
1162
+ this.input.setText("");
1129
1163
  this.tui.requestRender();
1130
1164
  return;
1131
1165
  }
1132
1166
 
1133
1167
  // Everything else (including Escape → dismiss, Enter → submit,
1134
- // Ctrl+Enter → newline) goes to TextArea's own handler.
1168
+ // Ctrl+Enter → newline) goes to CustomEditor's own handler
1169
+ // (CustomEditor delegates app keybindings, then Editor).
1135
1170
  originalHandleInput(data);
1136
-
1137
- // After TextArea processed the key, check if Escape was pressed
1138
- // (TextArea calls onEscape which calls onDismissCallback).
1139
- // tui.select.cancel is handled by TextArea internally.
1140
1171
  };
1141
1172
 
1142
1173
  this.refresh();
1143
1174
  }
1144
1175
 
1176
+ private async openExternalEditor(): Promise<void> {
1177
+ if (this.externalEditorActive) return;
1178
+ this.externalEditorActive = true;
1179
+
1180
+ const editorCommand = process.env.VISUAL || process.env.EDITOR || (process.platform === "win32" ? "notepad" : "nano");
1181
+ const [editor, ...editorArgs] = editorCommand.split(/\s+/);
1182
+ const dir = mkdtempSync(path.join(tmpdir(), "pi-btw-editor-"));
1183
+ const filePath = path.join(dir, "prompt.md");
1184
+
1185
+ try {
1186
+ writeFileSync(filePath, this.input.getText(), "utf8");
1187
+ this.tui.stop();
1188
+ process.stdout.write(`Launching external editor: ${editorCommand}\nPi will resume when the editor exits.\n`);
1189
+
1190
+ const status = await new Promise<number | null>((resolve) => {
1191
+ const child = spawn(editor, [...editorArgs, filePath], {
1192
+ stdio: "inherit",
1193
+ shell: process.platform === "win32",
1194
+ });
1195
+ child.on("error", () => resolve(null));
1196
+ child.on("close", (code) => resolve(code));
1197
+ });
1198
+
1199
+ if (status === 0) {
1200
+ this.input.setText(readFileSync(filePath, "utf8").replace(/\n$/, ""));
1201
+ }
1202
+ } finally {
1203
+ rmSync(dir, { recursive: true, force: true });
1204
+ this.tui.start();
1205
+ this.tui.requestRender(true);
1206
+ this.externalEditorActive = false;
1207
+ }
1208
+ }
1209
+
1145
1210
  private frameLine(content: string, innerWidth: number): string {
1146
1211
  const truncated = truncateToWidth(content, innerWidth, "");
1147
1212
  const padding = Math.max(0, innerWidth - visibleWidth(truncated));
@@ -1420,12 +1485,12 @@ export class BtwOverlayComponent extends Container implements Focusable {
1420
1485
  }
1421
1486
 
1422
1487
  setDraft(value: string): void {
1423
- this.input.setValue(value);
1488
+ this.input.setText(value);
1424
1489
  this.tui.requestRender();
1425
1490
  }
1426
1491
 
1427
1492
  getDraft(): string {
1428
- return this.input.getValue();
1493
+ return this.input.getText();
1429
1494
  }
1430
1495
 
1431
1496
  getTranscriptEntries(): BtwTranscript {
@@ -2321,6 +2386,8 @@ export default function (pi: ExtensionAPI) {
2321
2386
  overlayRuntime?.handle?.unfocus();
2322
2387
  overlayRuntime?.refresh?.();
2323
2388
  },
2389
+ undefined,
2390
+ ctx.cwd,
2324
2391
  );
2325
2392
 
2326
2393
  overlay.focused = runtime.handle?.isFocused() ?? true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-render-btw",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "description": "A pi extension for parallel side conversations with /btw — with markdown rendering in the floating window",
5
5
  "type": "module",
6
6
  "license": "MIT",