diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-06-27 06:05:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-27 06:05:59 -0700 |
commit | 842788675e9c4f0d416626ea3d8a9dac5e00b595 (patch) | |
tree | 15c42850f2d624ed60565876d20889ca8b7cf8ff /cmd2/clipboard.py | |
parent | f0c98ac10e60995bbf2dff9848931109cac2f96e (diff) | |
parent | 3c8880e40bf653e098577de94be7eb2171dbc6b4 (diff) | |
download | cmd2-git-842788675e9c4f0d416626ea3d8a9dac5e00b595.tar.gz |
Merge branch 'master' into autocompleter
Diffstat (limited to 'cmd2/clipboard.py')
-rw-r--r-- | cmd2/clipboard.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/cmd2/clipboard.py b/cmd2/clipboard.py new file mode 100644 index 00000000..e0d1fc03 --- /dev/null +++ b/cmd2/clipboard.py @@ -0,0 +1,49 @@ +# coding=utf-8 +""" +This module provides basic ability to copy from and paste to the clipboard/pastebuffer. +""" +import sys + +import pyperclip + +# Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure +try: + from pyperclip.exceptions import PyperclipException +except ImportError: # pragma: no cover + # noinspection PyUnresolvedReferences + from pyperclip import PyperclipException + +# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux +# noinspection PyUnresolvedReferences +try: + # Get the version of the pyperclip module as a float + pyperclip_ver = float('.'.join(pyperclip.__version__.split('.')[:2])) + + # The extraneous output bug in pyperclip on Linux using xclip was fixed in more recent versions of pyperclip + if sys.platform.startswith('linux') and pyperclip_ver < 1.6: + # Avoid extraneous output to stderr from xclip when clipboard is empty at cost of overwriting clipboard contents + pyperclip.copy('') + else: + # Try getting the contents of the clipboard + _ = pyperclip.paste() +except PyperclipException: + can_clip = False +else: + can_clip = True + + +def get_paste_buffer() -> str: + """Get the contents of the clipboard / paste buffer. + + :return: contents of the clipboard + """ + pb_str = pyperclip.paste() + return pb_str + + +def write_to_paste_buffer(txt: str) -> None: + """Copy text to the clipboard / paste buffer. + + :param txt: text to copy to the clipboard + """ + pyperclip.copy(txt) |