Skip to content
Merged
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
21 changes: 21 additions & 0 deletions packages/app/e2e/terminal/terminal-input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { runTerminal, waitTerminalFocusIdle } from "../actions"
import { test } from "../fixtures"
import { terminalSelector } from "../selectors"
import { terminalToggleKey } from "../utils"

// Regression coverage for #696: a returns-value inversion in the Ghostty
// custom key handler swallowed every keystroke before it could reach the
// PTY. The compile-time defense is the typed wrapper in
// `@/utils/terminal-key-handler`; this spec keeps the full keyboard → PTY →
// render path under test so a future break in any layer surfaces here.
test("terminal forwards typed characters to the underlying PTY", async ({ page, gotoSession }) => {
await gotoSession()

const terminal = page.locator(terminalSelector).first()
Comment thread
Astro-Han marked this conversation as resolved.
const visible = await terminal.isVisible().catch(() => false)
if (!visible) await page.keyboard.press(terminalToggleKey)
await waitTerminalFocusIdle(page, { term: terminal })

const token = `E2E_INPUT_${Date.now()}`
await runTerminal(page, { cmd: `echo ${token}`, token })
})
17 changes: 10 additions & 7 deletions packages/app/src/components/terminal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { withAlpha } from "@opencode-ai/ui/theme/color"
import { useTheme } from "@opencode-ai/ui/theme/context"
import { resolveThemeVariant } from "@opencode-ai/ui/theme/resolve"
Expand All @@ -16,6 +16,7 @@
import type { RuntimePTY, TerminalSnapshot, TerminalTab } from "@/context/terminal-types"
import { terminalAttr, terminalProbe } from "@/testing/terminal"
import { disposeIfDisposable, getHoveredLinkText, setOptionIfSupported } from "@/utils/runtime-adapters"
import { attachKeyHandler } from "@/utils/terminal-key-handler"
import { terminalWebSocketURL } from "@/utils/terminal-websocket-url"
import { terminalWriter } from "@/utils/terminal-writer"

Expand Down Expand Up @@ -392,25 +393,27 @@
}),
)

t.attachCustomKeyEventHandler((event) => {
attachKeyHandler(t, (event) => {
const key = event.key.toLowerCase()

if (event.ctrlKey && event.shiftKey && !event.metaKey && key === "c") {
document.execCommand("copy")
return true
return "block"
}

// Dispatch terminal toggles ourselves because Ghostty focus can swallow
// the keydown before the app-level command listener receives it.
const config = settings.keybinds.get(TOGGLE_TERMINAL_ID) ?? DEFAULT_TOGGLE_TERMINAL_KEYBIND
const keybinds = parseKeybind(config)

if (!matchKeybind(keybinds, event)) return true
if (matchKeybind(keybinds, event)) {
event.preventDefault()
event.stopPropagation()
queueMicrotask(() => command.trigger(TOGGLE_TERMINAL_ID, "keybind"))
return "block"
}

event.preventDefault()
event.stopPropagation()
queueMicrotask(() => command.trigger(TOGGLE_TERMINAL_ID, "keybind"))
return false
return "passthrough"
})
Comment thread
Astro-Han marked this conversation as resolved.

const fit = new mod.FitAddon()
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/utils/terminal-key-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Terminal as Term } from "ghostty-web"

export type KeyHandlerResult = "block" | "passthrough"

// Ghostty's customKeyEventHandler returns `true` to BLOCK default handling —
// opposite of xterm.js, which uses `true` to mean "continue normal processing".
// This wrapper exposes an enum so callers cannot accidentally invert the
// boolean and silently swallow every keystroke (see issue #696).
export const attachKeyHandler = (term: Term, handler: (event: KeyboardEvent) => KeyHandlerResult) => {
term.attachCustomKeyEventHandler((event) => handler(event) === "block")
}
Loading