|
| 1 | +// libghostty-vt has no clipboard callback, so OSC 52 writes are framed here, |
| 2 | +// alongside the bytes Ghostty parses. Reads (`?`) are never answered: replying |
| 3 | +// would send this client's clipboard to whatever process asked for it. |
| 4 | + |
| 5 | +// Base64 characters, about 768 KiB of copied text. Longer sequences are dropped |
| 6 | +// instead of buffered so a broken stream cannot grow memory without bound. |
| 7 | +export const MAX_OSC52_PAYLOAD_LENGTH = 1024 * 1024; |
| 8 | + |
| 9 | +const PREFIX = "52;"; |
| 10 | +// BEL ends the OSC, ESC may start ST, CAN and SUB abort it. |
| 11 | +// oxlint-disable-next-line no-control-regex -- matching VT control bytes is the point. |
| 12 | +const OSC_CONTROL = /[\x07\x18\x1a\x1b]/g; |
| 13 | +// The browser has one clipboard, so the system and both X11 selections map to it. |
| 14 | +const SELECTION = /^[cps0-7]*$/; |
| 15 | +const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); |
| 16 | + |
| 17 | +function decodeClipboardWrite(body: string): string | null { |
| 18 | + const separator = body.indexOf(";", PREFIX.length); |
| 19 | + if (separator === -1) return null; |
| 20 | + const selection = body.slice(PREFIX.length, separator); |
| 21 | + if (!SELECTION.test(selection) || (selection !== "" && !/[cps]/.test(selection))) return null; |
| 22 | + const encoded = body.slice(separator + 1); |
| 23 | + if (encoded === "" || encoded === "?") return null; |
| 24 | + try { |
| 25 | + const binary = atob(encoded); |
| 26 | + const bytes = new Uint8Array(binary.length); |
| 27 | + for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index); |
| 28 | + const text = decoder.decode(bytes); |
| 29 | + return text.length > 0 ? text : null; |
| 30 | + } catch { |
| 31 | + return null; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** Streams live terminal output and reports each complete OSC 52 clipboard write. */ |
| 36 | +export class Osc52Scanner { |
| 37 | + private state: "ground" | "escape" | "osc" | "oscEscape" = "ground"; |
| 38 | + // The OSC body so far, or null once it can no longer be a clipboard write. |
| 39 | + private body: string | null = null; |
| 40 | + private readonly onCopy: (text: string) => void; |
| 41 | + |
| 42 | + constructor(onCopy: (text: string) => void) { |
| 43 | + this.onCopy = onCopy; |
| 44 | + } |
| 45 | + |
| 46 | + /** Forgets any partial sequence, for when the terminal's contents are replaced. */ |
| 47 | + reset(): void { |
| 48 | + this.state = "ground"; |
| 49 | + this.body = null; |
| 50 | + } |
| 51 | + |
| 52 | + write(data: string): void { |
| 53 | + let index = 0; |
| 54 | + while (index < data.length) { |
| 55 | + switch (this.state) { |
| 56 | + case "ground": { |
| 57 | + const escape = data.indexOf("\x1b", index); |
| 58 | + if (escape === -1) return; |
| 59 | + this.state = "escape"; |
| 60 | + index = escape + 1; |
| 61 | + break; |
| 62 | + } |
| 63 | + case "escape": { |
| 64 | + const char = data[index]; |
| 65 | + index += 1; |
| 66 | + if (char === "]") { |
| 67 | + this.state = "osc"; |
| 68 | + this.body = ""; |
| 69 | + } else if (char !== "\x1b") { |
| 70 | + this.state = "ground"; |
| 71 | + } |
| 72 | + break; |
| 73 | + } |
| 74 | + case "osc": { |
| 75 | + OSC_CONTROL.lastIndex = index; |
| 76 | + const control = OSC_CONTROL.exec(data); |
| 77 | + const end = control?.index ?? data.length; |
| 78 | + this.append(data.slice(index, end)); |
| 79 | + if (!control) return; |
| 80 | + index = end + 1; |
| 81 | + if (control[0] === "\x07") this.finish(); |
| 82 | + else if (control[0] === "\x1b") this.state = "oscEscape"; |
| 83 | + else this.reset(); |
| 84 | + break; |
| 85 | + } |
| 86 | + case "oscEscape": { |
| 87 | + if (data[index] === "\\") { |
| 88 | + index += 1; |
| 89 | + this.finish(); |
| 90 | + } else { |
| 91 | + // Any other escape aborts the OSC and begins a new sequence. |
| 92 | + this.state = "escape"; |
| 93 | + this.body = null; |
| 94 | + } |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private append(text: string): void { |
| 102 | + if (this.body === null || text.length === 0) return; |
| 103 | + if (this.body.length + text.length > MAX_OSC52_PAYLOAD_LENGTH) { |
| 104 | + this.body = null; |
| 105 | + return; |
| 106 | + } |
| 107 | + const checkPrefix = this.body.length < PREFIX.length; |
| 108 | + this.body += text; |
| 109 | + if (checkPrefix && !PREFIX.startsWith(this.body.slice(0, PREFIX.length))) this.body = null; |
| 110 | + } |
| 111 | + |
| 112 | + private finish(): void { |
| 113 | + const body = this.body; |
| 114 | + this.reset(); |
| 115 | + const text = body === null ? null : decodeClipboardWrite(body); |
| 116 | + if (text !== null) this.onCopy(text); |
| 117 | + } |
| 118 | +} |
0 commit comments