Files
t3tui/src/ui/editor.ts
T
Callum Alpass 626d37a4e9 New threads, model and mode picker, alerts, $EDITOR and terminal attach
- c starts a draft thread (project, model, mode, worktree) launched with orchestration.launchThread
- m picks model, provider options and access mode from the server's providers, following the web's provider lock; model changes ride on the next message
- Alerts port T3's notification gate: bell, OSC 9/99 desktop notifications when unfocused, in-app flash
- ctrl+g edits the message in $EDITOR; ctrl+j attaches to the thread's T3 terminal
- Credential gains terminal:read
2026-11-02 18:55:47 +11:00

28 lines
1.1 KiB
TypeScript

import type { CliRenderer } from "@opentui/core";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
/**
* Hands the terminal to $VISUAL or $EDITOR on a copy of `text` and returns what was saved,
* or null if the editor failed. The renderer is suspended meanwhile.
*/
export function editInEditor(renderer: CliRenderer, text: string): string | null {
const editor = process.env.VISUAL || process.env.EDITOR || "vi";
const dir = mkdtempSync(join(tmpdir(), "tui3-"));
const file = join(dir, "prompt.md");
writeFileSync(file, text);
renderer.suspend();
try {
// Through the shell, so EDITOR="code --wait" works.
const result = spawnSync(process.env.SHELL || "sh", ["-c", `${editor} "$1"`, "sh", file], { stdio: "inherit" });
if (result.status !== 0) return null;
return readFileSync(file, "utf8").replace(/\n$/, "");
} finally {
rmSync(dir, { recursive: true, force: true });
renderer.resume();
renderer.requestRender();
}
}