feat(clipboard): keep an in-memory history of copied text
This commit is contained in:
3 files changed
+162
-4
No files matched your search
+23
-4
@@ -96,15 +96,33 @@ class Clipboard:
|
||||
self.clipboard_type = clipboard_type
|
||||
self.enabled = self.clipboard_type is ClipboardType.clipboard or supports_primary_selection
|
||||
|
||||
def set_text(self, x: str | bytes) -> None:
|
||||
def set_text(self, x: str | bytes, private: bool = False) -> None:
|
||||
if isinstance(x, str):
|
||||
x = x.encode('utf-8')
|
||||
self.set_mime({'text/plain': x})
|
||||
self.set_mime({'text/plain': x}, private=private)
|
||||
|
||||
def set_mime(self, data: Mapping[str, DataType]) -> None:
|
||||
def set_mime(self, data: Mapping[str, DataType], private: bool = False) -> None:
|
||||
if self.enabled and isinstance(data, dict):
|
||||
self.data = dict(data)
|
||||
set_clipboard_data_types(self.clipboard_type, tuple(self.data))
|
||||
self._record_in_history(private=private)
|
||||
|
||||
def _record_in_history(self, private: bool) -> None:
|
||||
# Only plain text is useful in the history, and only for the clipboard: the primary selection
|
||||
# changes constantly and would evict everything else. Private writes are dropped by the
|
||||
# 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):
|
||||
return
|
||||
try:
|
||||
text = data.decode('utf-8')
|
||||
history = getattr(get_boss(), 'clipboard_history', None)
|
||||
except Exception:
|
||||
return
|
||||
if history is not None:
|
||||
history.add(text, private=private)
|
||||
|
||||
def get_text(self) -> str:
|
||||
parts: list[bytes] = []
|
||||
@@ -309,7 +327,8 @@ class WriteRequest:
|
||||
if pos is not None:
|
||||
self.mime_map[alias] = pos
|
||||
x = {mime: self.tempfile.create_chunker(pos.start, pos.size) for mime, pos in self.mime_map.items()}
|
||||
cp.set_mime(x)
|
||||
# A write request that carries a password is private: never keep its data in the history
|
||||
cp.set_mime(x, private=bool(self.password))
|
||||
|
||||
def abort(self, msg: str) -> ValueError:
|
||||
# returns the error to raise, so that callers read as: raise self.abort(...)
|
||||
|
||||
Reference in new issue
Block a user