mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-29 16:54:59 +00:00
Merge 2ef26582dd
into 3caab85931
This commit is contained in:
commit
553662fe66
1 changed files with 23 additions and 2 deletions
|
@ -18,12 +18,16 @@ class ClipboardWatcher:
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Start watching clipboard for changes"""
|
"""Start watching clipboard for changes"""
|
||||||
self.stop_event = threading.Event()
|
self.stop_event = threading.Event()
|
||||||
self.last_clipboard = pyperclip.paste()
|
# guard against non-UTF8 bytes in the clipboard
|
||||||
|
try:
|
||||||
|
self.last_clipboard = pyperclip.paste()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
self.last_clipboard = ""
|
||||||
|
|
||||||
def watch_clipboard():
|
def watch_clipboard():
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
try:
|
try:
|
||||||
current = pyperclip.paste()
|
current = self._safe_paste()
|
||||||
if current != self.last_clipboard:
|
if current != self.last_clipboard:
|
||||||
self.last_clipboard = current
|
self.last_clipboard = current
|
||||||
self.io.interrupt_input()
|
self.io.interrupt_input()
|
||||||
|
@ -51,6 +55,23 @@ class ClipboardWatcher:
|
||||||
self.watcher_thread = None
|
self.watcher_thread = None
|
||||||
self.stop_event = None
|
self.stop_event = None
|
||||||
|
|
||||||
|
def _safe_paste(self) -> str:
|
||||||
|
"""
|
||||||
|
Attempt a normal pyperclip.paste(); if it raises a UnicodeDecodeError
|
||||||
|
fall back to a raw xclip call and replace invalid bytes.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return pyperclip.paste()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# linux fallback via xclip
|
||||||
|
import subprocess, shlex
|
||||||
|
try:
|
||||||
|
raw = subprocess.check_output(shlex.split("xclip -selection clipboard -o"))
|
||||||
|
# replace any invalid bytes with <20>
|
||||||
|
return raw.decode("utf-8", errors="replace")
|
||||||
|
except Exception:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Example usage of the clipboard watcher"""
|
"""Example usage of the clipboard watcher"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue