pi-render-btw 0.6.7 → 0.6.8
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 +1 -0
- package/extensions/btw-transcript.test.ts +26 -0
- package/extensions/btw.ts +49 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -93,6 +93,7 @@ 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
|
|
96
97
|
- `Esc` still dismisses BTW immediately while the overlay is focused
|
|
97
98
|
- BTW opens top-centered so the main session remains visible underneath it
|
|
98
99
|
|
|
@@ -119,4 +119,30 @@ describe("BTW transcript message layout", () => {
|
|
|
119
119
|
component.handleInput("\x10"); // Ctrl+P
|
|
120
120
|
expect(component.render(80).join("\n")).toContain("q:exit");
|
|
121
121
|
});
|
|
122
|
+
|
|
123
|
+
it("routes Ctrl+G to the external-editor callback", () => {
|
|
124
|
+
let opened = 0;
|
|
125
|
+
const externalKeybindings = {
|
|
126
|
+
matches: (data: string, action: string) =>
|
|
127
|
+
action === "app.editor.external" && data === "\x07",
|
|
128
|
+
} as any;
|
|
129
|
+
const component = new BtwOverlayComponent(
|
|
130
|
+
tui,
|
|
131
|
+
theme,
|
|
132
|
+
externalKeybindings,
|
|
133
|
+
() => [],
|
|
134
|
+
() => null,
|
|
135
|
+
() => "contextual",
|
|
136
|
+
() => {},
|
|
137
|
+
() => {},
|
|
138
|
+
() => {},
|
|
139
|
+
() => { opened++; },
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
component.setDraft("draft prompt");
|
|
143
|
+
component.handleInput("\x07"); // Ctrl+G
|
|
144
|
+
|
|
145
|
+
expect(opened).toBe(1);
|
|
146
|
+
expect(component.getDraft()).toBe("draft prompt");
|
|
147
|
+
});
|
|
122
148
|
});
|
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 {
|
|
@@ -1050,6 +1050,8 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1050
1050
|
private readonly onSubmitCallback: (value: string) => void;
|
|
1051
1051
|
private readonly onDismissCallback: () => void;
|
|
1052
1052
|
private readonly onUnfocusCallback: () => void;
|
|
1053
|
+
private readonly onExternalEditorCallback?: () => void;
|
|
1054
|
+
private externalEditorActive = false;
|
|
1053
1055
|
private readonly tui: TUI;
|
|
1054
1056
|
private readonly theme: ExtensionContext["ui"]["theme"];
|
|
1055
1057
|
private transcriptLines: string[] = [];
|
|
@@ -1089,6 +1091,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1089
1091
|
onSubmit: (value: string) => void,
|
|
1090
1092
|
onDismiss: () => void,
|
|
1091
1093
|
onUnfocus: () => void,
|
|
1094
|
+
onExternalEditor?: () => void,
|
|
1092
1095
|
) {
|
|
1093
1096
|
super();
|
|
1094
1097
|
this.tui = tui;
|
|
@@ -1099,6 +1102,7 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1099
1102
|
this.onSubmitCallback = onSubmit;
|
|
1100
1103
|
this.onDismissCallback = onDismiss;
|
|
1101
1104
|
this.onUnfocusCallback = onUnfocus;
|
|
1105
|
+
this.onExternalEditorCallback = onExternalEditor;
|
|
1102
1106
|
|
|
1103
1107
|
this.modeText = new Text("", 1, 0);
|
|
1104
1108
|
this.summaryText = new Text("", 1, 0);
|
|
@@ -1123,6 +1127,15 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1123
1127
|
|
|
1124
1128
|
const originalHandleInput = this.input.handleInput.bind(this.input);
|
|
1125
1129
|
this.input.handleInput = (data: string) => {
|
|
1130
|
+
if (keybindings.matches(data, "app.editor.external")) {
|
|
1131
|
+
if (this.onExternalEditorCallback) {
|
|
1132
|
+
this.onExternalEditorCallback();
|
|
1133
|
+
} else {
|
|
1134
|
+
void this.openExternalEditor();
|
|
1135
|
+
}
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1126
1139
|
// app.clear with non-empty input → clear input
|
|
1127
1140
|
if (keybindings.matches(data, "app.clear") && this.input.getValue().length > 0) {
|
|
1128
1141
|
this.input.setValue("");
|
|
@@ -1142,6 +1155,40 @@ export class BtwOverlayComponent extends Container implements Focusable {
|
|
|
1142
1155
|
this.refresh();
|
|
1143
1156
|
}
|
|
1144
1157
|
|
|
1158
|
+
private async openExternalEditor(): Promise<void> {
|
|
1159
|
+
if (this.externalEditorActive) return;
|
|
1160
|
+
this.externalEditorActive = true;
|
|
1161
|
+
|
|
1162
|
+
const editorCommand = process.env.VISUAL || process.env.EDITOR || (process.platform === "win32" ? "notepad" : "nano");
|
|
1163
|
+
const [editor, ...editorArgs] = editorCommand.split(/\s+/);
|
|
1164
|
+
const dir = mkdtempSync(path.join(tmpdir(), "pi-btw-editor-"));
|
|
1165
|
+
const filePath = path.join(dir, "prompt.md");
|
|
1166
|
+
|
|
1167
|
+
try {
|
|
1168
|
+
writeFileSync(filePath, this.input.getValue(), "utf8");
|
|
1169
|
+
this.tui.stop();
|
|
1170
|
+
process.stdout.write(`Launching external editor: ${editorCommand}\nPi will resume when the editor exits.\n`);
|
|
1171
|
+
|
|
1172
|
+
const status = await new Promise<number | null>((resolve) => {
|
|
1173
|
+
const child = spawn(editor, [...editorArgs, filePath], {
|
|
1174
|
+
stdio: "inherit",
|
|
1175
|
+
shell: process.platform === "win32",
|
|
1176
|
+
});
|
|
1177
|
+
child.on("error", () => resolve(null));
|
|
1178
|
+
child.on("close", (code) => resolve(code));
|
|
1179
|
+
});
|
|
1180
|
+
|
|
1181
|
+
if (status === 0) {
|
|
1182
|
+
this.input.setValue(readFileSync(filePath, "utf8").replace(/\n$/, ""));
|
|
1183
|
+
}
|
|
1184
|
+
} finally {
|
|
1185
|
+
rmSync(dir, { recursive: true, force: true });
|
|
1186
|
+
this.tui.start();
|
|
1187
|
+
this.tui.requestRender(true);
|
|
1188
|
+
this.externalEditorActive = false;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1145
1192
|
private frameLine(content: string, innerWidth: number): string {
|
|
1146
1193
|
const truncated = truncateToWidth(content, innerWidth, "");
|
|
1147
1194
|
const padding = Math.max(0, innerWidth - visibleWidth(truncated));
|