Skip to content
Open
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
31 changes: 28 additions & 3 deletions packages/opencode/src/cli/cmd/tui/util/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,35 @@ const getClipboardy = lazy(async () => {
function writeOsc52(text: string): void {
if (!process.stdout.isTTY) return
const base64 = Buffer.from(text).toString("base64")

if (process.env["TMUX"]) {
const osc52 = `\x1b]52;c;${base64}\x07`
process.stdout.write(`\x1bPtmux;\x1b${osc52}\x1b\\`)
return
}

if (process.env["STY"]) {
// Screen's DCS passthrough buffer is limited to MAXSTR = 768 bytes.
// The \x1b]52;c; prefix takes 8 bytes, leaving 759 for the first chunk.
// Subsequent chunks have no prefix and tolerate up to 767 bytes.
// Use 700-byte chunks for a safe margin, joining with \x1b\\\x1bP
// to terminate and restart the DCS passthrough. The terminal
// accumulates the partial OSC stream until the final \x07.
const chunkSize = 700
let sequence = `\x1bP\x1b]52;c;`

for (let i = 0; i < base64.length; i += chunkSize) {
if (i > 0) sequence += `\x1b\\\x1bP`
sequence += base64.slice(i, i + chunkSize)
}

sequence += `\x07\x1b\\`
process.stdout.write(sequence)
return
}

const osc52 = `\x1b]52;c;${base64}\x07`
const passthrough = process.env["TMUX"] || process.env["STY"]
const sequence = passthrough ? `\x1bPtmux;\x1b${osc52}\x1b\\` : osc52
process.stdout.write(sequence)
process.stdout.write(osc52)
}

export interface Content {
Expand Down
Loading