From 60e2357dabbe0d52ce991283cfcce2824e43f90f Mon Sep 17 00:00:00 2001 From: Callum Alpass Date: Tue, 3 Nov 2026 02:05:13 +1100 Subject: [PATCH] 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. --- src/ui/App.tsx | 4 ++-- src/ui/editor.ts | 6 +++--- src/ui/terminalAttach.ts | 11 ++++++----- src/ui/useAutomations.ts | 4 ++-- src/ui/usePullRequests.ts | 8 ++++---- src/ui/useWorkspace.ts | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/ui/App.tsx b/src/ui/App.tsx index fbdb4b7..334ac05 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -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(); diff --git a/src/ui/editor.ts b/src/ui/editor.ts index f7add94..b6f42fb 100644 --- a/src/ui/editor.ts +++ b/src/ui/editor.ts @@ -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 { 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(); } } diff --git a/src/ui/terminalAttach.ts b/src/ui/terminalAttach.ts index 2cdd2ab..4c2f370 100644 --- a/src/ui/terminalAttach.ts +++ b/src/ui/terminalAttach.ts @@ -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 { 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(); - renderer.requestRender(); - onDone(message); + void renderer.resume().then(() => { + renderer.requestRender(); + onDone(message); + }); } } diff --git a/src/ui/useAutomations.ts b/src/ui/useAutomations.ts index c9e9434..95a4e1c 100644 --- a/src/ui/useAutomations.ts +++ b/src/ui/useAutomations.ts @@ -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); diff --git a/src/ui/usePullRequests.ts b/src/ui/usePullRequests.ts index d4dd69e..4e46758 100644 --- a/src/ui/usePullRequests.ts +++ b/src/ui/usePullRequests.ts @@ -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"); diff --git a/src/ui/useWorkspace.ts b/src/ui/useWorkspace.ts index ca1ab03..b840183 100644 --- a/src/ui/useWorkspace.ts +++ b/src/ui/useWorkspace.ts @@ -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); }); };