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:
BlackAngelTV committed 2026-11-11 21:32:09 +01:00
1 parent 831852a89f
commit 44b9aa166e
3 files changed
+68 -5

No files matched your search

+28
View File
@@ -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.