fix(clipboard): record streamed clipboard writes in the history
OSC 52 writes are committed as a chunker factory rather than bytes, so the history silently dropped every text copy made by a program. Extract the text from either representation, bounded so a huge write is not materialized.
This commit is contained in:
3 files changed
+68
-5
No files matched your search
+5
-4
@@ -9,6 +9,7 @@ from gettext import gettext as _
|
||||
from tempfile import TemporaryFile
|
||||
from typing import IO, NamedTuple, Union
|
||||
|
||||
from .clipboard_history import text_from_mime_data
|
||||
from .conf.utils import uniq
|
||||
from .constants import supports_primary_selection
|
||||
from .fast_data_types import (
|
||||
@@ -113,15 +114,15 @@ class Clipboard:
|
||||
# history itself, so that secrets never reach it.
|
||||
if self.clipboard_type is not ClipboardType.clipboard:
|
||||
return
|
||||
data = self.data.get('text/plain')
|
||||
if not isinstance(data, bytes):
|
||||
if 'text/plain' not in self.data:
|
||||
return
|
||||
try:
|
||||
text = data.decode('utf-8')
|
||||
# The payload can be a chunker rather than bytes, as writes are streamed from a temp file
|
||||
text = text_from_mime_data(self.data['text/plain'])
|
||||
history = getattr(get_boss(), 'clipboard_history', None)
|
||||
except Exception:
|
||||
return
|
||||
if history is not None:
|
||||
if text and history is not None:
|
||||
history.add(text, private=private)
|
||||
|
||||
def get_text(self) -> str:
|
||||
|
||||
@@ -15,6 +15,34 @@ MAX_ENTRY_CHARS = 64 * 1024
|
||||
MAX_TOTAL_CHARS = 1024 * 1024
|
||||
|
||||
|
||||
def text_from_mime_data(data: object, max_chars: int = MAX_ENTRY_CHARS) -> str:
|
||||
"""Extract text from a clipboard MIME payload, or an empty string when it is not text.
|
||||
|
||||
Clipboard payloads are either raw bytes or a factory returning a chunker, as the data can be
|
||||
streamed from a temporary file. Only the start of the payload is materialized, since an entry
|
||||
longer than max_chars is not kept anyway.
|
||||
"""
|
||||
limit = 4 * max_chars # a UTF-8 character takes at most four bytes
|
||||
if isinstance(data, bytes | bytearray | memoryview):
|
||||
raw = bytes(data[:limit])
|
||||
elif callable(data):
|
||||
chunks: list[bytes] = []
|
||||
size = 0
|
||||
chunker = data()
|
||||
while True:
|
||||
q = chunker()
|
||||
if not q:
|
||||
break
|
||||
chunks.append(q)
|
||||
size += len(q)
|
||||
if size >= limit:
|
||||
break
|
||||
raw = b''.join(chunks)[:limit]
|
||||
else:
|
||||
return ''
|
||||
return raw.decode('utf-8', 'replace')
|
||||
|
||||
|
||||
def match_score(needle: str, haystack: str) -> int:
|
||||
"""Return a score, lower is better, or -1 when haystack does not contain needle as a subsequence.
|
||||
|
||||
|
||||
Reference in new issue
Block a user