MCPcopy Create free account
hub / github.com/BuddySirJava/SSH-Studio / _copy_text_to_clipboard

Method _copy_text_to_clipboard

src/ui/host_editor.py:1324–1381  ·  view source on GitHub ↗
(self, text: str)

Source from the content-addressed store, hash-verified

1322 self._show_message(_(f"Failed to copy command: {str(e)}"))
1323
1324 def _copy_text_to_clipboard(self, text: str) -> bool:
1325 try:
1326 display = Gdk.Display.get_default()
1327 if not display:
1328 raise RuntimeError("no display")
1329 clipboard = display.get_clipboard()
1330 bytes_utf8 = GLib.Bytes.new(text.encode("utf-8"))
1331 providers = [
1332 Gdk.ContentProvider.new_for_bytes(
1333 "text/plain;charset=utf-8", bytes_utf8
1334 ),
1335 Gdk.ContentProvider.new_for_bytes("text/plain", bytes_utf8),
1336 ]
1337 provider = (
1338 Gdk.ContentProvider.new_union(providers)
1339 if hasattr(Gdk.ContentProvider, "new_union")
1340 else providers[0]
1341 )
1342 self._last_clip_provider = provider
1343 if hasattr(clipboard, "set_content"):
1344 clipboard.set_content(provider)
1345 elif hasattr(clipboard, "set"):
1346 clipboard.set(provider)
1347 elif hasattr(clipboard, "set_text"):
1348 clipboard.set_text(text)
1349 else:
1350 raise RuntimeError("unsupported clipboard api")
1351 try:
1352 primary = display.get_primary_clipboard()
1353 if primary:
1354 if hasattr(primary, "set_content"):
1355 primary.set_content(self._last_clip_provider)
1356 elif hasattr(primary, "set"):
1357 primary.set(self._last_clip_provider)
1358 elif hasattr(primary, "set_text"):
1359 primary.set_text(text)
1360 except Exception:
1361 pass
1362 return True
1363 except Exception:
1364 pass
1365 try:
1366 import subprocess as _sub
1367
1368 for cmd in [
1369 ["wl-copy"],
1370 ["xclip", "-selection", "clipboard"],
1371 ["xsel", "--clipboard", "--input"],
1372 ]:
1373 try:
1374 res = _sub.run(cmd, input=text, text=True, capture_output=True)
1375 if res.returncode == 0:
1376 return True
1377 except Exception:
1378 continue
1379 except Exception:
1380 pass
1381 return False

Callers 1

_on_copy_ssh_commandMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected