import platform
import subprocess
import uuid


def _call(args):
    return subprocess.call(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def _copy_available(command):
    if platform.system() == "Windows":
        return _call(["where", command]) == 0
    return _call(["which", command]) == 0


def get_system_copy_command():
    if platform.system() == "Windows" and _copy_available("clip"):
        return "clip"

    if platform.system() == "Darwin" and _copy_available("pbcopy"):
        return "pbcopy"

    for command in ["xsel", "xclip", "wl-copy"]:
        if _copy_available(command):
            return command


def _popen(args, **kwargs):
    return subprocess.Popen(args, stdin=subprocess.PIPE)


commands = {
    "clip": ["clip"],
    "pbcopy": ["pbcopy"],
    "wl-copy": ["wl-copy"],
    "xsel": ["xsel", "--clipboard", "--input"],
    "xclip": ["xclip", "-selection", "clipboard"],
}


def copy(text):
    command = get_system_copy_command()
    if command is None:
        raise (Exception("No software available on your system to copy to clipboard"))
    args = commands[command]
    if platform.system() == "Windows":
        p = _popen(args)
    else:
        p = _popen(args, close_fds=True)
    p.communicate(input=text.encode('ascii'))

@@ -1,12 +1,22 @@
+import os%0A
 import platf
@@ -524,16 +524,122 @@
 bcopy%22%0A%0A
+    if os.getenv(%22WAYLAND_DISPLAY%22) is not None and _copy_available(%22wl-copy%22):%0A        return %22wl-copy%22%0A%0A
     for 
@@ -669,19 +669,8 @@
 lip%22
-, %22wl-copy%22
 %5D:%0A 

Check DISPLAY_WAYLAND env variable to decide whether to use wl-copy (#500)