Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Termux support #105

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
12 changes: 10 additions & 2 deletions pyperclip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
On Mac, the module uses pbcopy and pbpaste, which should come with the os.
On Linux, install xclip or xsel via package manager. For example, in Debian:
sudo apt-get install xclip
On Android, install the Termux app for terminal emulation. Then install
the Termux:API plugin, which provide support for the copy and paste commands.

Otherwise on Linux, you will need the gtk or PyQt4 modules installed.

gtk and PyQt4 modules are not available for Python 3,
and this module does not work with PyGObject yet.
"""
__version__ = '1.5.27'
__version__ = '1.6.3'

import platform
import os
import subprocess
from .clipboards import (init_osx_clipboard,
from .clipboards import (init_osx_clipboard, init_termux_clipboard,
init_gtk_clipboard, init_qt_clipboard,
init_xclip_clipboard, init_xsel_clipboard,
init_klipper_clipboard, init_no_clipboard)
Expand Down Expand Up @@ -79,6 +81,11 @@ def determine_clipboard():
return init_xsel_clipboard()
if _executable_exists("klipper") and _executable_exists("qdbus"):
return init_klipper_clipboard()
elif _executable_exists("termux-clipboard-get"):
# FIXME this command will exist even if the Termux:API plugin is not installed
# if it's not installed, copy and paste will just hang
# we need a reliable way to determine whether the plugin is installed
return init_termux_clipboard()

return init_no_clipboard()

Expand All @@ -87,6 +94,7 @@ def set_clipboard(clipboard):
global copy, paste

clipboard_types = {'osx': init_osx_clipboard,
'termux': init_termux_clipboard,
'gtk': init_gtk_clipboard,
'qt': init_qt_clipboard,
'xclip': init_xclip_clipboard,
Expand Down
21 changes: 21 additions & 0 deletions pyperclip/clipboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ def paste_osx():
return copy_osx, paste_osx


def init_termux_clipboard():
"""return a copy and paste function for Termux,
a terminal application for Android.

more information: https://termux.com/"""

def copy_termux(text):
p = subprocess.Popen('termux-clipboard-set',
stdin=subprocess.PIPE, close_fds=True)
p.communicate(input=text.encode('utf-8'))

def paste_termux():
p = subprocess.Popen('termux-clipboard-get',
stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
# get rid of the trailing newline
return stdout.decode('utf-8')[:-1]

return copy_termux, paste_termux


def init_gtk_clipboard():
import gtk

Expand Down
5 changes: 4 additions & 1 deletion tests/test_copy_paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from pyperclip import _executable_exists, HAS_DISPLAY
from pyperclip.clipboards import (init_osx_clipboard,
from pyperclip.clipboards import (init_osx_clipboard, init_termux_clipboard,
init_gtk_clipboard, init_qt_clipboard,
init_xclip_clipboard, init_xsel_clipboard,
init_klipper_clipboard, init_no_clipboard)
Expand Down Expand Up @@ -72,6 +72,9 @@ class TestWindows(_TestClipboard):
if os.name == 'nt' or platform.system() == 'Windows':
clipboard = init_windows_clipboard()

class TestTermux(_TestClipboard):
if _executable_exists('termux-clipboard-get'):
clipboard = init_termux_clipboard()

class TestOSX(_TestClipboard):
if os.name == 'mac' or platform.system() == 'Darwin':
Expand Down