|
| 1 | +"""System clipboard helpers. |
| 2 | +
|
| 3 | +Terminal clipboard access is platform and terminal dependent. These helpers try |
| 4 | +native OS commands first, then pyperclip, and leave terminal-mediated OSC52 to |
| 5 | +the caller because Textual owns the active driver. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from importlib import import_module |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +CLIPBOARD_TIMEOUT_S = 1.5 |
| 18 | + |
| 19 | + |
| 20 | +def get_system_clipboard_text() -> str: |
| 21 | + """Return text from the system clipboard, or an empty string if unavailable.""" |
| 22 | + text = _get_with_native_command() |
| 23 | + if text is not None: |
| 24 | + return text |
| 25 | + |
| 26 | + try: |
| 27 | + pyperclip = _load_pyperclip() |
| 28 | + return pyperclip.paste() or "" |
| 29 | + except Exception: |
| 30 | + return "" |
| 31 | + |
| 32 | + |
| 33 | +def copy_to_system_clipboard(text: str) -> bool: |
| 34 | + """Copy text to the system clipboard using the best available backend.""" |
| 35 | + if _copy_with_native_command(text): |
| 36 | + return True |
| 37 | + |
| 38 | + try: |
| 39 | + pyperclip = _load_pyperclip() |
| 40 | + pyperclip.copy(text) |
| 41 | + return True |
| 42 | + except Exception: |
| 43 | + return False |
| 44 | + |
| 45 | + |
| 46 | +def _load_pyperclip() -> Any: |
| 47 | + return import_module("pyperclip") |
| 48 | + |
| 49 | + |
| 50 | +def _get_with_native_command() -> str | None: |
| 51 | + if sys.platform == "darwin": |
| 52 | + return _run_text_output_command(["pbpaste"]) |
| 53 | + |
| 54 | + if sys.platform == "win32": |
| 55 | + command = _powershell_command() |
| 56 | + if command: |
| 57 | + return _run_text_output_command(command + ["Get-Clipboard", "-Raw"]) |
| 58 | + return None |
| 59 | + |
| 60 | + for command in _linux_paste_commands(): |
| 61 | + text = _run_text_output_command(command) |
| 62 | + if text is not None: |
| 63 | + return text |
| 64 | + |
| 65 | + return None |
| 66 | + |
| 67 | + |
| 68 | +def _copy_with_native_command(text: str) -> bool: |
| 69 | + if sys.platform == "darwin": |
| 70 | + return _run_text_input_command(["pbcopy"], text) |
| 71 | + |
| 72 | + if sys.platform == "win32": |
| 73 | + command = _powershell_command() |
| 74 | + if command and _run_text_input_command(command + ["Set-Clipboard -Value ([Console]::In.ReadToEnd())"], text): |
| 75 | + return True |
| 76 | + return _run_text_input_command(["clip.exe"], text) |
| 77 | + |
| 78 | + return any(_run_text_input_command(command, text) for command in _linux_copy_commands()) |
| 79 | + |
| 80 | + |
| 81 | +def _linux_paste_commands() -> list[list[str]]: |
| 82 | + commands: list[list[str]] = [] |
| 83 | + if os.environ.get("WAYLAND_DISPLAY"): |
| 84 | + commands.append(["wl-paste", "--type", "text"]) |
| 85 | + if os.environ.get("DISPLAY"): |
| 86 | + commands.extend( |
| 87 | + [ |
| 88 | + ["xclip", "-selection", "clipboard", "-out"], |
| 89 | + ["xsel", "--clipboard", "--output"], |
| 90 | + ] |
| 91 | + ) |
| 92 | + if not commands: |
| 93 | + commands.extend( |
| 94 | + [ |
| 95 | + ["wl-paste", "--type", "text"], |
| 96 | + ["xclip", "-selection", "clipboard", "-out"], |
| 97 | + ["xsel", "--clipboard", "--output"], |
| 98 | + ] |
| 99 | + ) |
| 100 | + return commands |
| 101 | + |
| 102 | + |
| 103 | +def _linux_copy_commands() -> list[list[str]]: |
| 104 | + commands: list[list[str]] = [] |
| 105 | + if os.environ.get("WAYLAND_DISPLAY"): |
| 106 | + commands.append(["wl-copy", "--type", "text/plain"]) |
| 107 | + if os.environ.get("DISPLAY"): |
| 108 | + commands.extend( |
| 109 | + [ |
| 110 | + ["xclip", "-selection", "clipboard"], |
| 111 | + ["xsel", "--clipboard", "--input"], |
| 112 | + ] |
| 113 | + ) |
| 114 | + if not commands: |
| 115 | + commands.extend( |
| 116 | + [ |
| 117 | + ["wl-copy", "--type", "text/plain"], |
| 118 | + ["xclip", "-selection", "clipboard"], |
| 119 | + ["xsel", "--clipboard", "--input"], |
| 120 | + ] |
| 121 | + ) |
| 122 | + return commands |
| 123 | + |
| 124 | + |
| 125 | +def _powershell_command() -> list[str] | None: |
| 126 | + for executable in ("pwsh", "powershell.exe", "powershell"): |
| 127 | + if shutil.which(executable): |
| 128 | + return [executable, "-NoProfile", "-NonInteractive", "-Command"] |
| 129 | + return None |
| 130 | + |
| 131 | + |
| 132 | +def _run_text_output_command(command: list[str]) -> str | None: |
| 133 | + if not shutil.which(command[0]): |
| 134 | + return None |
| 135 | + try: |
| 136 | + completed = subprocess.run( |
| 137 | + command, |
| 138 | + capture_output=True, |
| 139 | + check=False, |
| 140 | + text=True, |
| 141 | + timeout=CLIPBOARD_TIMEOUT_S, |
| 142 | + ) |
| 143 | + except Exception: |
| 144 | + return None |
| 145 | + if completed.returncode != 0: |
| 146 | + return None |
| 147 | + return completed.stdout |
| 148 | + |
| 149 | + |
| 150 | +def _run_text_input_command(command: list[str], text: str) -> bool: |
| 151 | + if not shutil.which(command[0]): |
| 152 | + return False |
| 153 | + try: |
| 154 | + completed = subprocess.run( |
| 155 | + command, |
| 156 | + check=False, |
| 157 | + input=text, |
| 158 | + stdout=subprocess.DEVNULL, |
| 159 | + stderr=subprocess.DEVNULL, |
| 160 | + text=True, |
| 161 | + timeout=CLIPBOARD_TIMEOUT_S, |
| 162 | + ) |
| 163 | + except Exception: |
| 164 | + return False |
| 165 | + return completed.returncode == 0 |
0 commit comments