(
text: string,
{
successMessage,
errorMessage,
durationMs,
suppressGlobalMessage = false,
}: CopyToClipboardOptions = {},
)
| 84 | } |
| 85 | |
| 86 | export async function copyTextToClipboard( |
| 87 | text: string, |
| 88 | { |
| 89 | successMessage, |
| 90 | errorMessage, |
| 91 | durationMs, |
| 92 | suppressGlobalMessage = false, |
| 93 | }: CopyToClipboardOptions = {}, |
| 94 | ) { |
| 95 | if (!text || text.trim().length === 0) { |
| 96 | return |
| 97 | } |
| 98 | |
| 99 | try { |
| 100 | let copied: boolean |
| 101 | if (isRemoteSession()) { |
| 102 | // Remote/SSH: prefer renderer OSC 52 (through render pipeline), |
| 103 | // then our manual OSC 52, then platform tools |
| 104 | copied = tryCopyViaRenderer(text) || tryCopyViaOsc52(text) || tryCopyViaPlatformTool(text) |
| 105 | } else { |
| 106 | // Local: prefer platform tools (reliable with tmux), |
| 107 | // then renderer OSC 52, then our manual OSC 52 as fallback |
| 108 | copied = tryCopyViaPlatformTool(text) || tryCopyViaRenderer(text) || tryCopyViaOsc52(text) |
| 109 | } |
| 110 | |
| 111 | if (!copied) { |
| 112 | throw new Error('No clipboard method available') |
| 113 | } |
| 114 | |
| 115 | if (!suppressGlobalMessage) { |
| 116 | const message = |
| 117 | successMessage !== undefined |
| 118 | ? successMessage |
| 119 | : getDefaultSuccessMessage(text) |
| 120 | if (message) { |
| 121 | showClipboardMessage(message, { durationMs }) |
| 122 | } |
| 123 | } |
| 124 | } catch (error) { |
| 125 | logger.error(error, 'Failed to copy to clipboard') |
| 126 | if (!suppressGlobalMessage) { |
| 127 | showClipboardMessage(errorMessage ?? 'Failed to copy to clipboard', { |
| 128 | durationMs, |
| 129 | }) |
| 130 | } |
| 131 | throw error |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | export function clearClipboardMessage() { |
| 136 | if (clearTimer) { |
no test coverage detected