Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/opencode/src/cli/cmd/tui/attach.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cmd } from "../cmd"
import { UI } from "@/cli/ui"
import { tui } from "./app"
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
import { win32DisableProcessedInput, win32InstallCtrlCGuard, win32SetUtf8CodePage } from "./win32"

export const AttachCommand = cmd({
command: "attach <url>",
Expand Down Expand Up @@ -40,6 +40,7 @@ export const AttachCommand = cmd({
const unguard = win32InstallCtrlCGuard()
try {
win32DisableProcessedInput()
win32SetUtf8CodePage()

if (args.fork && !args.continue && !args.session) {
UI.error("--fork requires --continue or --session")
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/cli/cmd/tui/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { withNetworkOptions, resolveNetworkOptions } from "@/cli/network"
import { Filesystem } from "@/util/filesystem"
import type { Event } from "@opencode-ai/sdk/v2"
import type { EventSource } from "./context/sdk"
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
import { win32DisableProcessedInput, win32InstallCtrlCGuard, win32SetUtf8CodePage } from "./win32"

declare global {
const OPENCODE_WORKER_PATH: string
Expand Down Expand Up @@ -87,6 +87,7 @@ export const TuiThreadCommand = cmd({
// Must be the very first thing — disables CTRL_C_EVENT before any Worker
// spawn or async work so the OS cannot kill the process group.
win32DisableProcessedInput()
win32SetUtf8CodePage()

if (args.fork && !args.continue && !args.session) {
UI.error("--fork requires --continue or --session")
Expand Down
15 changes: 15 additions & 0 deletions packages/opencode/src/cli/cmd/tui/win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { dlopen, ptr } from "bun:ffi"

const STD_INPUT_HANDLE = -10
const ENABLE_PROCESSED_INPUT = 0x0001
const CP_UTF8 = 65001

const kernel = () =>
dlopen("kernel32.dll", {
GetStdHandle: { args: ["i32"], returns: "ptr" },
GetConsoleMode: { args: ["ptr", "ptr"], returns: "i32" },
SetConsoleMode: { args: ["ptr", "u32"], returns: "i32" },
FlushConsoleInputBuffer: { args: ["ptr"], returns: "i32" },
SetConsoleCP: { args: ["u32"], returns: "i32" },
SetConsoleOutputCP: { args: ["u32"], returns: "i32" },
})

let k32: ReturnType<typeof kernel> | undefined
Expand All @@ -23,6 +26,18 @@ function load() {
}
}

/**
* Set console code page to UTF-8 for proper Unicode input/output.
* This fixes character encoding issues when using CJK (Chinese, Japanese, Korean)
* and other non-ASCII input methods on Windows.
*/
export function win32SetUtf8CodePage() {
if (process.platform !== "win32") return
if (!load()) return
k32!.symbols.SetConsoleCP(CP_UTF8)
k32!.symbols.SetConsoleOutputCP(CP_UTF8)
}

/**
* Clear ENABLE_PROCESSED_INPUT on the console stdin handle.
*/
Expand Down
Loading