Skip to content

Commit f203b17

Browse files
author
jeankrlo2929
committed
fix(tui): reset terminal state on crash/exit (Windows)
On Windows, when OpenCode exits unexpectedly or crashes, the terminal is left in a broken state showing raw ANSI escape sequences as literal text (e.g. "555;59;86m") and the cursor may be hidden. Add resetTerminal() in thread.ts and attach.ts that: - Sends \x1b[0m to reset all ANSI attributes (colors, bold, etc.) - Sends \x1b[?25h to restore the cursor - Sends \x1b[?1049l to exit the alternate screen buffer - Sends \x1b[?7h to re-enable auto-wrap Also registers a process.on("exit") handler in thread.ts as a backstop that runs even on process.exit() or uncaught exceptions. Fixes: anomalyco#21277
1 parent 3c96bf8 commit f203b17

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/attach.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { TuiConfig } from "@/config/tui"
66
import { Instance } from "@/project/instance"
77
import { existsSync } from "fs"
88

9+
function resetTerminal() {
10+
if (!process.stdout.isTTY) return
11+
process.stdout.write(
12+
"\x1b[0m" + // reset all attributes (colors, bold, etc.)
13+
"\x1b[?25h" + // show cursor
14+
"\x1b[?1049l" + // exit alternate screen buffer
15+
"\x1b[?7h", // re-enable auto-wrap
16+
)
17+
}
18+
919
export const AttachCommand = cmd({
1020
command: "attach <url>",
1121
describe: "attach to a running opencode server",
@@ -83,6 +93,7 @@ export const AttachCommand = cmd({
8393
})
8494
} finally {
8595
unguard?.()
96+
resetTerminal()
8697
}
8798
},
8899
})

‎packages/opencode/src/cli/cmd/tui/thread.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ declare global {
2121
const OPENCODE_WORKER_PATH: string
2222
}
2323

24+
function resetTerminal() {
25+
if (!process.stdout.isTTY) return
26+
process.stdout.write(
27+
"\x1b[0m" + // reset all attributes (colors, bold, etc.)
28+
"\x1b[?25h" + // show cursor
29+
"\x1b[?1049l" + // exit alternate screen buffer
30+
"\x1b[?7h", // re-enable auto-wrap
31+
)
32+
}
33+
34+
// Backstop: runs even on process.exit() or uncaught crash
35+
process.on("exit", resetTerminal)
36+
2437
type RpcClient = ReturnType<typeof Rpc.client<typeof rpc>>
2538

2639
function createWorkerFetch(client: RpcClient): typeof fetch {
@@ -226,6 +239,7 @@ export const TuiThreadCommand = cmd({
226239
}
227240
} finally {
228241
unguard?.()
242+
resetTerminal()
229243
}
230244
process.exit(0)
231245
},

0 commit comments

Comments
 (0)