TS ``copyOrWriteToFile`` (copy.tsx:81-94), with the honest clipboard-failure divergence (module docstring). ``char_count`` is Python codepoints; TS ``text.length`` is UTF-16 code units (astral chars differ by one).
(text: str, filename: str)
| 183 | |
| 184 | |
| 185 | def _copy_or_write(text: str, filename: str) -> str: |
| 186 | """TS ``copyOrWriteToFile`` (copy.tsx:81-94), with the honest clipboard-failure |
| 187 | divergence (module docstring). ``char_count`` is Python codepoints; TS |
| 188 | ``text.length`` is UTF-16 code units (astral chars differ by one).""" |
| 189 | copied = _set_clipboard(text) |
| 190 | line_count = text.count("\n") + 1 |
| 191 | char_count = len(text) |
| 192 | try: |
| 193 | path = _write_to_file(text, filename) |
| 194 | except Exception as exc: |
| 195 | if copied: |
| 196 | return f"Copied to clipboard ({char_count} characters, {line_count} lines)" |
| 197 | return f"Failed to copy: {exc}" |
| 198 | if copied: |
| 199 | return ( |
| 200 | f"Copied to clipboard ({char_count} characters, {line_count} lines)\n" |
| 201 | f"Also written to {path}" |
| 202 | ) |
| 203 | return f"Written to {path} ({char_count} characters, {line_count} lines)" |
| 204 | |
| 205 | |
| 206 | def _copy_full_response_enabled() -> bool: |
no test coverage detected