repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

feat(open): support Termux editor sessions

a9b007b10508c2d10eb9b410ca0c72feac8c781b

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (4)

extensions/open/README.md

Modified
1 1 # open
3 -Open mentioned files in Neovim beside Pi.
3 +Open mentioned files in an editor beside Pi.
5 5 ## Install / load
20 20 Hooks:
22 -- `session_start` reports missing WezTerm, `$EDITOR`, or pane context.
22 +- `session_start` reports a missing editor or supported host context.
24 24 Settings:
25 25 none.
31 31 Paths are deduplicated and limited to 20.
32 32 The picker keeps paths prominent and shows the source as its description.
34 -The selected file opens through `$EDITOR` in a new right-hand WezTerm split using 69 percent of Pi's pane.
34 +The selected file opens through `$EDITOR` using the first available host:
36 +- WezTerm opens a new right-hand split using 69 percent of Pi's pane.
37 +- Termux starts a new foreground terminal session through its `RUN_COMMAND` intent.
35 39 The editor starts in the project root and receives an absolute file path.
36 -Launch is asynchronous; the pane closes when the editor exits.
40 +Launch is asynchronous; the pane or session closes when the editor exits.
41 +Termux requires `allow-external-apps = true` in `~/.termux/termux.properties`.

extensions/open/__tests__/harness.test.ts

Modified
239 239 expect(wezterm).toContain("wezterm");
242 + it("launches files in a new foreground Termux session", async () => {
243 + const root = tempRoot();
244 + const bin = join(root, "bin");
245 + mkdirSync(bin);
246 + const am = fakeExecutable(bin, "am");
247 + const editor = fakeExecutable(bin, "nvim");
248 + const marker = join(root, "launches.jsonl");
249 + const preload = join(root, "record.cjs");
250 + writeFileSync(
251 + preload,
252 + `require("node:fs").appendFileSync(${JSON.stringify(marker)}, JSON.stringify(process.argv.slice(1)) + "\\n"); process.exit(0);\n`,
253 + );
254 + const target = file(root, "termux file.ts");
255 + session = await createTestSession({
256 + cwd: root,
257 + extensionFactories: [openExtension],
258 + env: {
259 + PATH: `${bin}${delimiter}${process.env.PATH ?? ""}`,
260 + EDITOR: "nvim",
261 + TERMUX_VERSION: "0.118.3",
262 + TERMUX__USER_ID: "10",
263 + WEZTERM_PANE: undefined,
264 + NODE_OPTIONS: `--require=${preload}`,
265 + },
266 + });
267 + const extension = session.session.extensionRunner.extensions[0];
268 + await extension.commands
269 + .get("open")
270 + .handler(
271 + '"termux file.ts"',
272 + session.session.extensionRunner.createCommandContext(),
273 + );
275 + const launches = (await linesEventually(marker, 1)).map((line) =>
276 + JSON.parse(line),
277 + );
278 + expect(launches).toEqual([
280 + join(root, "startservice"),
281 + "--user",
282 + "10",
283 + "-n",
284 + "com.termux/com.termux.app.RunCommandService",
285 + "-a",
286 + "com.termux.RUN_COMMAND",
287 + "--es",
288 + "com.termux.RUN_COMMAND_PATH",
289 + editor,
290 + "--esa",
291 + "com.termux.RUN_COMMAND_ARGUMENTS",
292 + target,
293 + "--es",
294 + "com.termux.RUN_COMMAND_WORKDIR",
295 + root,
296 + "--ez",
297 + "com.termux.RUN_COMMAND_BACKGROUND",
298 + "false",
299 + "--es",
300 + "com.termux.RUN_COMMAND_SESSION_ACTION",
301 + "0",
302 + "--es",
303 + "com.termux.RUN_COMMAND_LABEL",
304 + "Pi open",
305 + ],
306 + ]);
307 + expect(am).toContain("am");
308 + expect(session.events.uiCallsFor("notify")).toEqual([]);
309 + });
242 311 it("warns when its required host context is unavailable", async () => {
243 312 const root = tempRoot();
244 313 session = await createTestSession({
245 314 cwd: root,
246 315 extensionFactories: [openExtension],
247 - env: { PATH: "", EDITOR: undefined, WEZTERM_PANE: undefined },
316 + env: {
317 + PATH: "",
318 + EDITOR: undefined,
319 + TERMUX_VERSION: undefined,
320 + WEZTERM_PANE: undefined,
321 + },
250 324 expect(
251 325 session.events.uiCallsFor("notify").map((call) => call.args),
252 326 ).toEqual([
253 - ["/open: wezterm is not on PATH", "warning"],
254 327 ["/open: EDITOR is not set", "warning"],
328 + ["/open: wezterm is not on PATH", "warning"],
255 329 ["/open: WEZTERM_PANE is not set", "warning"],

extensions/open/index.ts

Modified
357 -async function launchInSplit(
358 - wezterm: string,
359 - editor: string,
360 - paneId: string,
357 +function termuxArguments(
358 + userId: string | undefined,
361 359 cwd: string,
360 + editor: string,
362 361 path: string,
362 +): string[] {
363 + const androidUser = /^(?:0|[1-9][0-9]*)$/u.test(userId ?? "")
364 + ? (userId as string)
365 + : "0";
366 + return [
367 + "startservice",
368 + "--user",
369 + androidUser,
370 + "-n",
371 + "com.termux/com.termux.app.RunCommandService",
372 + "-a",
373 + "com.termux.RUN_COMMAND",
374 + "--es",
375 + "com.termux.RUN_COMMAND_PATH",
376 + editor,
377 + "--esa",
378 + "com.termux.RUN_COMMAND_ARGUMENTS",
379 + path,
380 + "--es",
381 + "com.termux.RUN_COMMAND_WORKDIR",
382 + cwd,
383 + "--ez",
384 + "com.termux.RUN_COMMAND_BACKGROUND",
385 + "false",
386 + "--es",
387 + "com.termux.RUN_COMMAND_SESSION_ACTION",
388 + "0",
389 + "--es",
390 + "com.termux.RUN_COMMAND_LABEL",
391 + "Pi open",
392 + ];
395 +async function launchDetached(
396 + command: string,
397 + args: string[],
398 + cwd: string,
363 399 ): Promise<void> {
364 400 await new Promise<void>((resolveLaunch, reject) => {
365 - const child = spawn(wezterm, splitArguments(paneId, cwd, editor, path), {
401 + const child = spawn(command, args, {
367 403 detached: true,
368 404 stdio: "ignore",
379 415 export default function openExtension(pi: ExtensionAPI): void {
380 416 const wezterm = executableOnPath("wezterm");
417 + const am = executableOnPath("am");
381 418 const editorName = process.env.EDITOR?.trim();
382 419 const editor = editorName ? executableOnPath(editorName) : undefined;
420 + const paneId = process.env.WEZTERM_PANE;
421 + const inTermux = Boolean(process.env.TERMUX_VERSION);
422 + const weztermReady = Boolean(wezterm && paneId);
423 + const termuxReady = Boolean(inTermux && am);
384 425 pi.on("session_start", (_event, ctx) => {
385 - if (!wezterm) ctx.ui.notify("/open: wezterm is not on PATH", "warning");
386 426 if (!editorName) ctx.ui.notify("/open: EDITOR is not set", "warning");
387 427 else if (!editor)
388 428 ctx.ui.notify(`/open: EDITOR '${editorName}' is not on PATH`, "warning");
389 - if (!process.env.WEZTERM_PANE)
390 - ctx.ui.notify("/open: WEZTERM_PANE is not set", "warning");
429 + if (!weztermReady && !termuxReady) {
430 + if (!wezterm) ctx.ui.notify("/open: wezterm is not on PATH", "warning");
431 + if (!paneId) ctx.ui.notify("/open: WEZTERM_PANE is not set", "warning");
432 + if (inTermux && !am) ctx.ui.notify("/open: am is not on PATH", "warning");
393 436 pi.registerCommand("open", {
394 - description: "Open a file in a Neovim WezTerm split",
437 + description: "Open a file beside Pi",
395 438 async handler(args, ctx) {
396 - const paneId = process.env.WEZTERM_PANE;
397 - if (!wezterm || !editor || !paneId) {
439 + if (!editor || (!weztermReady && !termuxReady)) {
398 440 ctx.ui.notify("/open unavailable; see startup warnings", "error");
399 441 return;
423 465 if (!path) return;
426 - await launchInSplit(wezterm, editor, paneId, ctx.cwd, path);
468 + if (wezterm && paneId) {
469 + await launchDetached(
470 + wezterm,
471 + splitArguments(paneId, ctx.cwd, editor, path),
472 + ctx.cwd,
473 + );
474 + } else if (am) {
475 + await launchDetached(
476 + am,
477 + termuxArguments(process.env.TERMUX__USER_ID, ctx.cwd, editor, path),
478 + ctx.cwd,
479 + );
427 481 } catch (error) {
428 482 ctx.ui.notify(
429 483 `/open failed: ${error instanceof Error ? error.message : String(error)}`,
441 495 extractTextPaths,
442 496 fuzzyCandidates,
443 497 splitArguments,
498 + termuxArguments,

extensions/open/package.json

Modified
7 7 "@earendil-works/pi-tui": "*"
9 9 "license": "MIT",
10 - "description": "Open mentioned files in Neovim beside Pi.",
10 + "description": "Open mentioned files in an editor beside Pi.",
11 11 "keywords": [
12 12 "pi",
13 13 "pi-extension"