refactor: convert inserter module to package with wayland backend

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vitali Graf
2026-04-08 10:34:07 +02:00
parent f9402c427c
commit b47045ab9b
4 changed files with 41 additions and 33 deletions
+48
View File
@@ -0,0 +1,48 @@
"""Text-Einfügung via wl-copy + ydotool Ctrl+V (Linux/Wayland)."""
import asyncio
import logging
logger = logging.getLogger(__name__)
PASTE_DELAY = 0.2
class WaylandInserter:
async def _run_capture(self, *cmd: str) -> bytes:
"""Führt einen Befehl aus und gibt stdout zurück."""
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
logger.warning("Befehl fehlgeschlagen: %s (stderr: %s)", cmd, stderr.decode())
return stdout
async def _run_fire(self, *cmd: str) -> None:
"""Führt einen Befehl aus ohne auf Pipes zu warten."""
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
)
await proc.wait()
async def insert(self, text: str) -> None:
"""Fügt Text ins aktive Fenster ein via Clipboard + Ctrl+V."""
if not text:
return
old_clipboard = await self._run_capture("wl-paste", "--no-newline")
try:
await self._run_fire("wl-copy", "--", text)
await asyncio.sleep(0.05)
await self._run_fire("ydotool", "key", "29:1", "47:1", "47:0", "29:0")
await asyncio.sleep(PASTE_DELAY)
finally:
await self._run_fire("wl-copy", "--", old_clipboard.decode())
logger.info("Text eingefügt (%d Zeichen)", len(text))