Wait for the renderer to suspend and resume

OpenTUI 0.8 suspends and resumes asynchronously and refuses to resume
mid-transition, so $EDITOR and an attached terminal wait for each step.
This commit is contained in:
Callum Alpass committed 2026-11-03 02:05:13 +11:00
1 parent b35263b41b
commit 60e2357dab
6 files changed
+16 -15

No files matched your search

+2 -2
View File
@@ -516,10 +516,10 @@ export function App(props: { readonly session: T3Session; readonly config: T3tui
const workspace = useWorkspace({ session, renderer, live, thread: threadShell, project, serverConfig: serverConfig?.config ?? null, draft: draft !== null, diff, openPicker, confirm, run, say });
/** The composer's text in $EDITOR. */
const openEditor = () => {
const openEditor = async () => {
const composer = composerRef.current;
if (!composer) return;
const edited = editInEditor(renderer, composer.plainText);
const edited = await editInEditor(renderer, composer.plainText);
if (edited === null) return say("The editor exited with an error; the message is unchanged.", true);
composer.setText(edited);
composer.gotoBufferEnd();
+3 -3
View File
@@ -8,12 +8,12 @@ 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 {
export async function editInEditor(renderer: CliRenderer, text: string): Promise<string | null> {
const editor = process.env.VISUAL || process.env.EDITOR || "vi";
const dir = mkdtempSync(join(tmpdir(), "t3tui-"));
const file = join(dir, "prompt.md");
writeFileSync(file, text);
renderer.suspend();
await 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" });
@@ -21,7 +21,7 @@ export function editInEditor(renderer: CliRenderer, text: string): string | null
return readFileSync(file, "utf8").replace(/\n$/, "");
} finally {
rmSync(dir, { recursive: true, force: true });
renderer.resume();
await renderer.resume();
renderer.requestRender();
}
}
+4 -3
View File
@@ -22,13 +22,13 @@ export interface TerminalTarget {
* keystrokes sent to it. Ctrl+J comes back. The terminal keeps running on the server.
* Note that a terminal's size is shared by everyone attached, so attaching resizes it.
*/
export function attachTerminal(renderer: CliRenderer, session: T3Session, target: TerminalTarget, onDone: (message?: string) => void): void {
export async function attachTerminal(renderer: CliRenderer, session: T3Session, target: TerminalTarget, onDone: (message?: string) => void): Promise<void> {
const stdin = process.stdin;
const stdout = process.stdout;
const size = () => ({ cols: stdout.columns || 80, rows: stdout.rows || 24 });
let finished = false;
renderer.suspend();
await renderer.suspend();
stdout.write("\x1b[?1049h\x1b[H\x1b[2J");
stdout.write(`\x1b]2;t3tui · ${target.label} · ctrl+j to return\x07`);
stdin.setRawMode?.(true);
@@ -83,8 +83,9 @@ export function attachTerminal(renderer: CliRenderer, session: T3Session, target
stdout.off("resize", onResize);
stdout.write(RESET);
stdout.write("\x1b]2;t3tui\x07");
renderer.resume();
void renderer.resume().then(() => {
renderer.requestRender();
onDone(message);
});
}
}
+2 -2
View File
@@ -25,8 +25,8 @@ export function useAutomations(deps: {
}): Automations {
const { session, renderer, projects, defaults, confirm, run, say } = deps;
const edit = (template: string, existing?: ScheduledTask) => {
const edited = editInEditor(renderer, template);
const edit = async (template: string, existing?: ScheduledTask) => {
const edited = await editInEditor(renderer, template);
if (edited === null) return say("The editor exited with an error; the automation is unchanged.", true);
const parsed = parseAutomation(edited, projects, existing);
if (!parsed.ok) return say(parsed.error, true);
+4 -4
View File
@@ -181,13 +181,13 @@ export function usePullRequests(deps: PullRequestDeps) {
if (current) void load(current.link, current.reference);
};
const comment = () => {
const comment = async () => {
const current = loadedRef.current;
const canComment = current?.detail?.capabilities.comment && current.detail.viewerPermissions.comment;
if (!current?.detail || !canComment) {
return deps.say("Commenting is not available on this pull request.", true);
}
const edited = editInEditor(deps.renderer, "");
const edited = await editInEditor(deps.renderer, "");
if (edited === null) return deps.say("The editor exited with an error; nothing was posted.", true);
const body = edited.trim();
if (!body) return deps.say("Nothing to post.", true);
@@ -215,8 +215,8 @@ export function usePullRequests(deps: PullRequestDeps) {
});
if (offered.length === 0) return deps.say("Reviewing is not available on this pull request.", true);
const writeReview = (verdict: (typeof VERDICTS)[number]) => {
const edited = editInEditor(deps.renderer, "");
const writeReview = async (verdict: (typeof VERDICTS)[number]) => {
const edited = await editInEditor(deps.renderer, "");
if (edited === null) return deps.say("The editor exited with an error; the review was not submitted.", true);
const body = edited;
const summaryRequired = sourceControlClients.get(detail.provider).reviewSummaryRequired("request-changes");
+1 -1
View File
@@ -83,7 +83,7 @@ export function useWorkspace(input: {
...terminal,
label: resolveTerminalSessionLabel(terminal.terminalId, terminal),
};
attachTerminal(renderer, session, target, (message) => {
void attachTerminal(renderer, session, target, (message) => {
if (message) say(message);
});
};