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 3 -Open mentioned files in Neovim beside Pi.
3 +Open mentioned files in an editor beside Pi.
22 -- `session_start` reports missing WezTerm, `$EDITOR`, or pane context.
22 +- `session_start` reports a missing editor or supported host context.
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
Modified239 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");
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");
252 + `require("node:fs").appendFileSync(${JSON.stringify(marker)}, JSON.stringify(process.argv.slice(1)) + "\\n"); process.exit(0);\n`,
254 + const target
= file(
root,
"termux file.ts");
255 + session
= await createTestSession({
257 + extensionFactories: [openExtension],
259 + PATH:
`${bin}${delimiter}${process.env.PATH ?? ""}`,
261 + TERMUX_VERSION:
"0.118.3",
262 + TERMUX__USER_ID:
"10",
263 + WEZTERM_PANE:
undefined,
264 + NODE_OPTIONS:
`--require=${preload}`,
267 + const extension
= session.session.extensionRunner.extensions[
0];
268 + await extension.commands
271 + '"termux file.ts"',
272 + session.session.extensionRunner.
createCommandContext(),
275 + const launches
= (
await linesEventually(marker,
1)).
map((line)
=> 278 + expect(launches).
toEqual([
280 + join(
root,
"startservice"),
284 + "com.termux/com.termux.app.RunCommandService",
286 + "com.termux.RUN_COMMAND",
288 + "com.termux.RUN_COMMAND_PATH",
291 + "com.termux.RUN_COMMAND_ARGUMENTS",
294 + "com.termux.RUN_COMMAND_WORKDIR",
297 + "com.termux.RUN_COMMAND_BACKGROUND",
300 + "com.termux.RUN_COMMAND_SESSION_ACTION",
303 + "com.termux.RUN_COMMAND_LABEL",
307 + expect(am).
toContain(
"am");
308 + expect(session.events.
uiCallsFor(
"notify")).
toEqual([]);
242 311 it(
"warns when its required host context is unavailable",
async ()
=> {
243 312 const root
= tempRoot();
244 313 session
= await createTestSession({
246 315 extensionFactories: [openExtension],
247 - env: { PATH:
"", EDITOR:
undefined, WEZTERM_PANE:
undefined },
319 + TERMUX_VERSION:
undefined,
320 + WEZTERM_PANE:
undefined,
251 325 session.events.
uiCallsFor(
"notify").
map((call)
=> call.args),
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
Modified357 -async function launchInSplit(
357 +function termuxArguments(
358 + userId
: string | undefined,
363 + const androidUser
= /^(?:0|[1-9][0-9]*)$/u.
test(userId
?? "")
364 + ? (userId
as string)
371 + "com.termux/com.termux.app.RunCommandService",
373 + "com.termux.RUN_COMMAND",
375 + "com.termux.RUN_COMMAND_PATH",
378 + "com.termux.RUN_COMMAND_ARGUMENTS",
381 + "com.termux.RUN_COMMAND_WORKDIR",
384 + "com.termux.RUN_COMMAND_BACKGROUND",
387 + "com.termux.RUN_COMMAND_SESSION_ACTION",
390 + "com.termux.RUN_COMMAND_LABEL",
395 +async function launchDetached(
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, {
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");
423 465 if (
!path)
return;
426 - await launchInSplit(wezterm, editor, paneId, ctx.cwd, path);
468 + if (wezterm
&& paneId) {
469 + await launchDetached(
471 + splitArguments(paneId, ctx.cwd, editor, path),
475 + await launchDetached(
477 + termuxArguments(
process.
env.TERMUX__USER_ID, ctx.cwd, editor, path),
427 481 }
catch (error) {
429 483 `/open failed: ${error instanceof Error ? error.message : String(error)}`,
441 495 extractTextPaths,
extensions/open/package.json
Modified 7 7 "@earendil-works/pi-tui":
"*" 10 - "description":
"Open mentioned files in Neovim beside Pi.",
10 + "description":
"Open mentioned files in an editor beside Pi.",