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
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from kitty.clipboard_history import ClipboardHistory, match_score
|
||||
from kitty.clipboard_history import ClipboardHistory, match_score, text_from_mime_data
|
||||
|
||||
from .base import BaseTest
|
||||
|
||||
@@ -91,6 +91,40 @@ class TestClipboardHistory(BaseTest):
|
||||
self.ae(match_score('abc', 'cba'), -1)
|
||||
self.ae(match_score('Abc', 'xxabc'), 2) # matching is case insensitive
|
||||
|
||||
def test_text_from_bytes(self) -> None:
|
||||
self.ae(text_from_mime_data(b'hello'), 'hello')
|
||||
self.ae(text_from_mime_data('not bytes'), '')
|
||||
self.ae(text_from_mime_data(None), '')
|
||||
self.ae(text_from_mime_data(b'a\xffb'), 'a\ufffdb')
|
||||
|
||||
def test_text_from_chunker(self) -> None:
|
||||
# OSC 52 writes are streamed, so the clipboard stores a chunker factory not bytes
|
||||
chunks = [b'first ', b'second', b'']
|
||||
|
||||
def factory():
|
||||
it = iter(chunks)
|
||||
|
||||
def chunker():
|
||||
return next(it, b'')
|
||||
|
||||
return chunker
|
||||
|
||||
self.ae(text_from_mime_data(factory), 'first second')
|
||||
|
||||
def test_text_from_chunker_is_bounded(self) -> None:
|
||||
def factory():
|
||||
remaining = [b'x' * 1000] * 1000
|
||||
it = iter(remaining)
|
||||
|
||||
def chunker():
|
||||
return next(it, b'')
|
||||
|
||||
return chunker
|
||||
|
||||
ans = text_from_mime_data(factory, max_chars=10)
|
||||
self.assertLessEqual(len(ans), 40)
|
||||
self.assertTrue(ans)
|
||||
|
||||
def test_no_persistence_api(self) -> None:
|
||||
# The history is deliberately memory only: there is no save/load entry point to call
|
||||
h = ClipboardHistory()
|
||||
|
||||
Reference in new issue
Block a user