* Shell out to a native clipboard utility as a safety net for OSC 52. * Only called when not in an SSH session (over SSH, these would write to * the remote machine's clipboard — OSC 52 is the right path there). * Fire-and-forget: failures are silent since OSC 52 may have succeeded.
(text: string)
| 169 | * Fire-and-forget: failures are silent since OSC 52 may have succeeded. |
| 170 | */ |
| 171 | function copyNative(text: string): void { |
| 172 | const opts = { input: text, useCwd: false, timeout: 2000 } |
| 173 | switch (process.platform) { |
| 174 | case 'darwin': |
| 175 | void execFileNoThrow('pbcopy', [], opts) |
| 176 | return |
| 177 | case 'linux': { |
| 178 | if (linuxCopy === null) return |
| 179 | if (linuxCopy === 'wl-copy') { |
| 180 | void execFileNoThrow('wl-copy', [], opts) |
| 181 | return |
| 182 | } |
| 183 | if (linuxCopy === 'xclip') { |
| 184 | void execFileNoThrow('xclip', ['-selection', 'clipboard'], opts) |
| 185 | return |
| 186 | } |
| 187 | if (linuxCopy === 'xsel') { |
| 188 | void execFileNoThrow('xsel', ['--clipboard', '--input'], opts) |
| 189 | return |
| 190 | } |
| 191 | // First call: probe wl-copy (Wayland) then xclip/xsel (X11), cache winner. |
| 192 | void execFileNoThrow('wl-copy', [], opts).then(r => { |
| 193 | if (r.code === 0) { |
| 194 | linuxCopy = 'wl-copy' |
| 195 | return |
| 196 | } |
| 197 | void execFileNoThrow('xclip', ['-selection', 'clipboard'], opts).then( |
| 198 | r2 => { |
| 199 | if (r2.code === 0) { |
| 200 | linuxCopy = 'xclip' |
| 201 | return |
| 202 | } |
| 203 | void execFileNoThrow('xsel', ['--clipboard', '--input'], opts).then( |
| 204 | r3 => { |
| 205 | linuxCopy = r3.code === 0 ? 'xsel' : null |
| 206 | }, |
| 207 | ) |
| 208 | }, |
| 209 | ) |
| 210 | }) |
| 211 | return |
| 212 | } |
| 213 | case 'win32': |
| 214 | // clip.exe is always available on Windows. Unicode handling is |
| 215 | // imperfect (system locale encoding) but good enough for a fallback. |
| 216 | void execFileNoThrow('clip', [], opts) |
| 217 | return |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** @internal test-only */ |
| 222 | export function _resetLinuxCopyCache(): void { |
no test coverage detected