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
7 changes: 7 additions & 0 deletions .github/workflows/nix-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ jobs:
- name: Setup Nix
uses: DeterminateSystems/nix-installer-action@v21

- name: Test macOS terminal restoration
if: runner.os == 'macOS'
run: |
set -euo pipefail
echo "Testing macOS terminal restoration..."
node ./script/macos-terminal-test.js

- name: Build desktop via flake
run: |
set -euo pipefail
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ jobs:
workspaces: packages/desktop/src-tauri
shared-key: ${{ matrix.settings.target }}

- name: Test macOS terminal restoration
if: runner.os == 'macOS'
run: |
set -euo pipefail
echo "Testing macOS terminal restoration..."
node ./script/macos-terminal-test.js

- name: Prepare
run: |
cd packages/desktop
Expand Down
63 changes: 60 additions & 3 deletions packages/opencode/src/cli/cmd/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,58 @@ export function tui(input: {
resolve()
}

// macOS-specific signal handling
const setupSignalHandlers = (renderer: any) => {
// Handle Ctrl+C gracefully
const handleSigint = () => {
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

// Handle terminal closure
const handleSighup = () => {
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

// Handle termination signal
const handleSigterm = () => {
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

process.on("SIGINT", handleSigint)
process.on("SIGHUP", handleSighup)
process.on("SIGTERM", handleSigterm)

return () => {
process.off("SIGINT", handleSigint)
process.off("SIGHUP", handleSighup)
process.off("SIGTERM", handleSigterm)
}
}

render(
() => {
const renderer = useRenderer()
const cleanupSignalHandlers = setupSignalHandlers(renderer)

return (
<ErrorBoundary
fallback={(error, reset) => <ErrorComponent error={error} reset={reset} onExit={onExit} mode={mode} />}
fallback={(error, reset) => (
<ErrorComponent
error={error}
reset={reset}
onExit={async () => {
cleanupSignalHandlers()
await onExit()
}}
mode={mode}
/>
)}
>
<ArgsProvider {...input.args}>
<ExitProvider onExit={onExit}>
Expand Down Expand Up @@ -514,9 +561,19 @@ function App() {
keybind: "terminal_suspend",
category: "System",
onSelect: () => {
process.once("SIGCONT", () => {
// Enhanced macOS suspend handling
const resumeHandler = () => {
// Restore terminal state on macOS
process.stdout.write("\x1b[?47h") // Enable alternate screen
process.stdout.write("\x1b[?1049h") // Save cursor and use alternate screen buffer
renderer.resume()
})
}

process.once("SIGCONT", resumeHandler)

// Clear alternate screen before suspend on macOS
process.stdout.write("\x1b[?1049l") // Use normal screen buffer
process.stdout.write("\x1b[?47l") // Disable alternate screen

renderer.suspend()
// pid=0 means send the signal to all processes in the process group
Expand Down
13 changes: 13 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/exit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ export const { use: useExit, provider: ExitProvider } = createSimpleContext({
init: (input: { onExit?: () => Promise<void> }) => {
const renderer = useRenderer()
return async (reason?: any) => {
// macOS terminal restoration
try {
// Reset terminal to known good state
process.stdout.write("\x1bc") // Reset terminal
process.stdout.write("\x1b[?1049l") // Restore normal screen buffer
process.stdout.write("\x1b[?47l") // Disable alternate screen
process.stdout.write("\x1b[0m") // Reset attributes
process.stdout.write("\x1b[2J") // Clear screen
process.stdout.write("\x1b[H") // Move cursor to top-left
} catch (e) {
// Ignore write errors during shutdown
}

// Reset window title before destroying renderer
renderer.setTerminalTitle("")
renderer.destroy()
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/cli/cmd/tui/util/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { RGBA } from "@opentui/core"

export function isMacOSTerminal(): boolean {
return (
process.platform === "darwin" &&
(process.env.TERM_PROGRAM === "Apple_Terminal" ||
process.env.TERM_PROGRAM === "iTerm.app" ||
(process.env.TERM?.includes("xterm") ?? false) ||
(process.env.TERM?.includes("screen") ?? false))
)
}

export namespace Terminal {
export type Colors = Awaited<ReturnType<typeof colors>>
/**
Expand Down
99 changes: 99 additions & 0 deletions script/macos-terminal-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env node

// Simple test for terminal restoration changes
// This script simulates the signal handling we added

console.log("Testing macOS terminal restoration...")

// Test isMacOSTerminal function
const isMacOSTerminal = () => {
return (
process.platform === "darwin" &&
(process.env.TERM_PROGRAM === "Apple_Terminal" ||
process.env.TERM_PROGRAM === "iTerm.app" ||
process.env.TERM?.includes("xterm") ||
process.env.TERM?.includes("screen"))
)
}

console.log("Platform:", process.platform)
console.log("Terminal:", process.env.TERM_PROGRAM || process.env.TERM)
console.log("Is macOS terminal:", isMacOSTerminal())

// Test escape sequences
const testTerminalReset = () => {
try {
process.stdout.write("\x1bc") // Reset terminal
process.stdout.write("\x1b[?1049l") // Restore normal screen buffer
process.stdout.write("\x1b[?47l") // Disable alternate screen
process.stdout.write("\x1b[0m") // Reset attributes
process.stdout.write("\x1b[2J") // Clear screen
process.stdout.write("\x1b[H") // Move cursor to top-left
console.log("✓ Terminal reset sequences executed successfully")
} catch (e) {
console.log("✗ Error executing terminal reset:", e.message)
}
}

console.log("Testing terminal reset sequences...")
testTerminalReset()

// Test signal handling setup
let signalHandlersCleared = false

const setupSignalHandlers = (renderer) => {
const handleSigint = () => {
console.log("SIGINT received - would clean up and exit")
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

const handleSighup = () => {
console.log("SIGHUP received - would clean up and exit")
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

const handleSigterm = () => {
console.log("SIGTERM received - would clean up and exit")
renderer.setTerminalTitle("")
renderer.destroy()
process.exit(0)
}

process.on("SIGINT", handleSigint)
process.on("SIGHUP", handleSighup)
process.on("SIGTERM", handleSigterm)

return () => {
process.off("SIGINT", handleSigint)
process.off("SIGHUP", handleSighup)
process.off("SIGTERM", handleSigterm)
signalHandlersCleared = true
console.log("✓ Signal handlers cleared")
}
}

// Mock renderer for testing
const mockRenderer = {
setTerminalTitle: (title) => console.log(`Setting terminal title: "${title}"`),
destroy: () => console.log("Renderer destroy called"),
}

console.log("Testing signal handler setup...")
const cleanup = setupSignalHandlers(mockRenderer)

console.log("Sending SIGINT signal...")
process.emit("SIGINT")

setTimeout(() => {
if (signalHandlersCleared) {
console.log("✓ All tests passed!")
process.exit(0)
} else {
console.log("✗ Signal handlers not cleared properly")
process.exit(1)
}
}, 100)