in_memory.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import annotations
  2. from collections import deque
  3. from .base import Clipboard, ClipboardData
  4. __all__ = [
  5. "InMemoryClipboard",
  6. ]
  7. class InMemoryClipboard(Clipboard):
  8. """
  9. Default clipboard implementation.
  10. Just keep the data in memory.
  11. This implements a kill-ring, for Emacs mode.
  12. """
  13. def __init__(self, data: ClipboardData | None = None, max_size: int = 60) -> None:
  14. assert max_size >= 1
  15. self.max_size = max_size
  16. self._ring: deque[ClipboardData] = deque()
  17. if data is not None:
  18. self.set_data(data)
  19. def set_data(self, data: ClipboardData) -> None:
  20. self._ring.appendleft(data)
  21. while len(self._ring) > self.max_size:
  22. self._ring.pop()
  23. def get_data(self) -> ClipboardData:
  24. if self._ring:
  25. return self._ring[0]
  26. else:
  27. return ClipboardData()
  28. def rotate(self) -> None:
  29. if self._ring:
  30. # Add the very first item at the end.
  31. self._ring.append(self._ring.popleft())