feat: add inserter module with clipboard paste and restore
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"""Text-Einfügung via Clipboard und wtype."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PASTE_DELAY = 0.2 # Sekunden Wartezeit vor Clipboard-Restore
|
||||
|
||||
|
||||
class Inserter:
|
||||
async def _run(self, *cmd: str) -> tuple[bytes, int]:
|
||||
"""Führt einen Befehl aus, gibt (stdout, returncode) 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, proc.returncode
|
||||
|
||||
async def insert(self, text: str) -> None:
|
||||
"""Fügt Text ins aktive Fenster ein via Clipboard + Ctrl+V."""
|
||||
if not text:
|
||||
return
|
||||
|
||||
# 1. Aktuelle Zwischenablage sichern
|
||||
old_clipboard, _ = await self._run("wl-paste")
|
||||
|
||||
try:
|
||||
# 2. Text in Zwischenablage kopieren
|
||||
await self._run("wl-copy", text)
|
||||
|
||||
# 3. Ctrl+V simulieren
|
||||
await self._run("wtype", "-M", "ctrl", "v", "-m", "ctrl")
|
||||
|
||||
# 4. Kurz warten, dann Clipboard wiederherstellen
|
||||
await asyncio.sleep(PASTE_DELAY)
|
||||
finally:
|
||||
# Clipboard immer wiederherstellen
|
||||
await self._run("wl-copy", old_clipboard.decode())
|
||||
|
||||
logger.info("Text eingefügt (%d Zeichen)", len(text))
|
||||
Reference in New Issue
Block a user