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