aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/prompt-toolkit/py3/prompt_toolkit/clipboard/in_memory.py
blob: d6e010e2325097328dc7757845ea15910e4d49a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

from collections import deque
from typing import Deque

from .base import Clipboard, ClipboardData

__all__ = [
    "InMemoryClipboard",
]


class InMemoryClipboard(Clipboard):
    """
    Default clipboard implementation.
    Just keep the data in memory.

    This implements a kill-ring, for Emacs mode.
    """

    def __init__(self, data: ClipboardData | None = None, max_size: int = 60) -> None:
        assert max_size >= 1

        self.max_size = max_size
        self._ring: Deque[ClipboardData] = deque()

        if data is not None:
            self.set_data(data)

    def set_data(self, data: ClipboardData) -> None:
        self._ring.appendleft(data)

        while len(self._ring) > self.max_size:
            self._ring.pop()

    def get_data(self) -> ClipboardData:
        if self._ring:
            return self._ring[0]
        else:
            return ClipboardData()

    def rotate(self) -> None:
        if self._ring:
            # Add the very first item at the end.
            self._ring.append(self._ring.popleft())