aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/prompt-toolkit/py3/prompt_toolkit/clipboard/base.py
blob: b05275ba63bfbd57c0096d3ba0fb1a079504035b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Clipboard for command line interface.
"""
from __future__ import annotations

from abc import ABCMeta, abstractmethod
from typing import Callable

from prompt_toolkit.selection import SelectionType

__all__ = [
    "Clipboard",
    "ClipboardData",
    "DummyClipboard",
    "DynamicClipboard",
]


class ClipboardData:
    """
    Text on the clipboard.

    :param text: string
    :param type: :class:`~prompt_toolkit.selection.SelectionType`
    """

    def __init__(
        self, text: str = "", type: SelectionType = SelectionType.CHARACTERS
    ) -> None:
        self.text = text
        self.type = type


class Clipboard(metaclass=ABCMeta):
    """
    Abstract baseclass for clipboards.
    (An implementation can be in memory, it can share the X11 or Windows
    keyboard, or can be persistent.)
    """

    @abstractmethod
    def set_data(self, data: ClipboardData) -> None:
        """
        Set data to the clipboard.

        :param data: :class:`~.ClipboardData` instance.
        """

    def set_text(self, text: str) -> None:  # Not abstract.
        """
        Shortcut for setting plain text on clipboard.
        """
        self.set_data(ClipboardData(text))

    def rotate(self) -> None:
        """
        For Emacs mode, rotate the kill ring.
        """

    @abstractmethod
    def get_data(self) -> ClipboardData:
        """
        Return clipboard data.
        """


class DummyClipboard(Clipboard):
    """
    Clipboard implementation that doesn't remember anything.
    """

    def set_data(self, data: ClipboardData) -> None:
        pass

    def set_text(self, text: str) -> None:
        pass

    def rotate(self) -> None:
        pass

    def get_data(self) -> ClipboardData:
        return ClipboardData()


class DynamicClipboard(Clipboard):
    """
    Clipboard class that can dynamically returns any Clipboard.

    :param get_clipboard: Callable that returns a :class:`.Clipboard` instance.
    """

    def __init__(self, get_clipboard: Callable[[], Clipboard | None]) -> None:
        self.get_clipboard = get_clipboard

    def _clipboard(self) -> Clipboard:
        return self.get_clipboard() or DummyClipboard()

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

    def set_text(self, text: str) -> None:
        self._clipboard().set_text(text)

    def rotate(self) -> None:
        self._clipboard().rotate()

    def get_data(self) -> ClipboardData:
        return self._clipboard().get_data()