Skip to content

Commit

Permalink
Merge pull request #242 from ceccopierangiolieugenio/copykitten_clipb…
Browse files Browse the repository at this point in the history
…oard_support

Copykitten clipboard support
  • Loading branch information
ceccopierangiolieugenio authored Feb 22, 2024
2 parents f5353f2 + 931ed3e commit e0cfbf1
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 29 deletions.
105 changes: 80 additions & 25 deletions TermTk/TTkGui/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,96 @@
from TermTk.TTkCore.log import TTkLog

class TTkClipboard():
_clipboard = ''
__slots__ = ('_setText', '_text')
'''TTkClipboard
:ref:`Clipboard`
Example:
.. code:: python
from TermTk import TTkClipboard
# Initialize the clipboard manager
clipboard = TTkClipboard()
# Push some text to the clipboard
clipboard.setText("Example")
# Get the text from the clipboard
text = clipboard.text()
'''
_clipboard = ""
_manager = None
_setText = None
_text = None

def __init__(self) -> None:
if importlib.util.find_spec('pyperclip'):
import pyperclip as _c
self._setText = _c.copy
self._text = _c.paste
elif importlib.util.find_spec('pyperclip3'):
import pyperclip3 as _c
self._setText = _c.copy
self._text = _c.paste
elif importlib.util.find_spec('clipboard'):
import clipboard as _c
self._setText = _c.copy
self._text = _c.paste
else:
self._setText = None
self._text = None

def setText(self, text):
if not TTkClipboard._manager:
TTkClipboard._loadClipboardManager()

@staticmethod
def _loadClipboardManager():
try:
if importlib.util.find_spec('copykitten'):
TTkLog.info("Using 'copykitten' as clipboard manager")
import copykitten as _c
TTkClipboard._manager = _c
TTkClipboard._setText = _c.copy
TTkClipboard._text = _c.paste
elif importlib.util.find_spec('pyperclip'):
TTkLog.info("Using 'pyperclip' as clipboard manager")
import pyperclip as _c
TTkClipboard._manager = _c
TTkClipboard._setText = _c.copy
TTkClipboard._text = _c.paste
elif importlib.util.find_spec('pyperclip3'):
TTkLog.info("Using 'pyperclip3' as clipboard manager")
import pyperclip3 as _c
TTkClipboard._manager = _c
TTkClipboard._setText = _c.copy
TTkClipboard._text = _c.paste
elif importlib.util.find_spec('pyclip'):
TTkLog.info("Using 'pyclip' as clipboard manager")
import pyclip as _c
TTkClipboard._manager = _c
TTkClipboard._setText = _c.copy
TTkClipboard._text = _c.paste
elif importlib.util.find_spec('clipboard'):
TTkLog.info("Using 'clipboard' as clipboard manager")
import clipboard as _c
TTkClipboard._manager = _c
TTkClipboard._setText = _c.copy
TTkClipboard._text = _c.paste
else:
TTkLog.info("No clipboard manager found")
TTkClipboard._manager = "Not Found"
except Exception as e:
TTkLog.error("Clipboard error, try to export X11 if you are running this UI via SSH")
for line in str(e).split("\n"):
TTkLog.error(line)

@staticmethod
def setText(text):
'''setText'''
TTkClipboard._clipboard = text
if self._setText:
if TTkClipboard._setText:
try:
self._setText(str(text))
TTkClipboard._setText(str(text))
except Exception as e:
TTkLog.error("Clipboard error, try to export X11 if you are running this UI via SSH")
for line in str(e).split("\n"):
TTkLog.error(line)

def text(self):
if self._text:
txt = self._text()
@staticmethod
def text():
'''text'''
if TTkClipboard._text:
txt = TTkClipboard._text()
if txt == str(TTkClipboard._clipboard):
return TTkClipboard._clipboard
else:
return self._text()
return TTkClipboard._text()
return TTkClipboard._clipboard
4 changes: 2 additions & 2 deletions docs/source/info/features/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Main features

* Input/Mouse/Paste Event handling

* Drag and Drop
* :ref:`Drag and Drop <DnD>`

* Clipboard support
* :ref:`Clipboard` support

* Drawing primitives

Expand Down
63 changes: 62 additions & 1 deletion docs/source/info/resources/clipboard.rst
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
TBD
.. _clipboard:

=========
Clipboard
=========

.. _pyTermTk: https://github.com/ceccopierangiolieugenio/pyTermTk


pyTermTk_ include a clipboard wrapper :class:`~TermTk.TTkGui.clipboard.TTkClipboard`, around any of the following libraries:

- `copykitten <https://github.com/klavionik/copykitten>`_ - Robust, dependency-free way to use the system clipboard in Python.
- `pyperclip <https://github.com/asweigart/pyperclip>`_ - Python module for cross-platform clipboard functions.
- `pyperclip3 <https://pypi.org/project/pyperclip3>`_ / `pyclip <https://github.com/spyoungtech/pyclip>`_ - Cross-platform Clipboard module for Python with binary support.
- `clipboard <https://github.com/terryyin/clipboard>`_ - A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux.

.. raw:: html

<video width="800"
src="https://github.com/ceccopierangiolieugenio/pyTermTk/assets/8876552/55978bef-be18-4912-a4f1-4b26845325fa"
data-canonical-src="https://github.com/ceccopierangiolieugenio/pyTermTk/assets/8876552/55978bef-be18-4912-a4f1-4b26845325fa"
controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" ></video>

The basic pyTermTk_ does not include any of those clipboard managers.
An internal implementation whitin the scope of the app itself is still available.

If any of the previous listed clipboard managers are installed, pyTermTk_ is able to automatically detect and use them.

i.e.

.. code:: bash
# Assuming no clipboard managers are installed
# you can still copy/paste between editors in this session
# but no text is copied to/from the system clipboard
python3 demo/showcase/textedit.py
# if pyperclip is installed,
# pyTermTk defaults the clipboard manager to this tool
# any copy/paste is synced with the system clipboard
# it is possible to copy/paste from/to an external editor
pip install pyperclip
python3 demo/showcase/textedit.py
-----
Usage
-----

Once initialized the clipboard manager, 2 apis are provided that can be used to access the clipboard (:class:`~TermTk.TTkGui.clipboard.TTkClipboard.setText`, :class:`~TermTk.TTkGui.clipboard.TTkClipboard.text`)

.. code:: python
from TermTk import TTkClipboard
# Initialize the clipboard manager
clipboard = TTkClipboard()
# Push some text to the clipboard
clipboard.setText("Example")
# Get the text from the clipboard
text = clipboard.text()
6 changes: 6 additions & 0 deletions docs/source/info/resources/dragdrop.rst
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
.. _DnD:

=============
Drag and Drop
=============

TBD
4 changes: 3 additions & 1 deletion docs/source/info/resources/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ Resources
:maxdepth: 1
:hidden:

modal
clipboard
modal
dragdrop

0 comments on commit e0cfbf1

Please sign in to comment.