diff --git a/clip/clipClearProcess.go b/clip/clipClearProcess.go index bb116da..ad0a4f9 100644 --- a/clip/clipClearProcess.go +++ b/clip/clipClearProcess.go @@ -13,7 +13,10 @@ import ( // ClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed. // assignedContents can be omitted to clear the clipboard immediately and unconditionally. func ClearProcess(assignedContents string) error { - cmdPaste, cmdClear := getClipCommands() + cmdPaste, cmdClear, err := getClipCommands() + if err != nil { + return errors.New("unable to determine clipboard platform: " + err.Error()) + } clearClipboard := func() error { err := cmdClear.Run() diff --git a/clip/copy_UNIX.go b/clip/copy_UNIX.go index b012abf..f8e4f9b 100644 --- a/clip/copy_UNIX.go +++ b/clip/copy_UNIX.go @@ -13,32 +13,48 @@ import ( // CopyString copies a string to the clipboard. func CopyString(clearClipboardAutomatically bool, copySubject string) error { // determine whether to use wl-copy (Wayland) or xclip (X11) - var envSet, isWayland bool // track whether environment variables are set + sessionIsWayland, err := isWayland() + if err != nil { + return err + } var cmdCopy *exec.Cmd - if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet { + if sessionIsWayland { cmdCopy = exec.Command("wl-copy", "-t", "text/plain") - isWayland = true - } else if _, envSet = os.LookupEnv("DISPLAY"); envSet { - cmdCopy = exec.Command("xclip", "-sel", "c", "-t", "text/plain") } else { - return errors.New("clipboard platform could not be determined") + cmdCopy = exec.Command("xclip", "-sel", "c", "-t", "text/plain") } _ = back.WriteToStdin(cmdCopy, copySubject) - err := cmdCopy.Run() + err = cmdCopy.Run() if err != nil { return errors.New("unable to copy to clipboard: " + err.Error()) } if clearClipboardAutomatically { - LaunchClipClearProcess(copySubject, isWayland) + LaunchClipClearProcess(copySubject, sessionIsWayland) } return nil } // getClipCommands returns the commands for pasting and clearing the clipboard contents. -func getClipCommands() (*exec.Cmd, *exec.Cmd) { - if os.Args[2] == "true" { // wayland - return exec.Command("wl-paste"), exec.Command("wl-copy", "-c") +func getClipCommands() (*exec.Cmd, *exec.Cmd, error) { + sessionIsWayland, err := isWayland() + if err != nil { + return nil, nil, err + } + if sessionIsWayland { + return exec.Command("wl-paste"), exec.Command("wl-copy", "-c"), nil + } + return exec.Command("xclip", "-o", "-sel", "c"), exec.Command("xclip", "-i", "/dev/null", "-sel", "c"), nil +} + +// isWayland returns a boolean indicating whether the current session is Wayland. +func isWayland() (bool, error) { + var envSet bool // track whether environment variables are set + if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet { + return true, nil + } else if _, envSet = os.LookupEnv("DISPLAY"); envSet { + return false, nil + } else { + return false, errors.New("unable to detect Wayland or X11 session") } - return exec.Command("xclip", "-o", "-sel", "c"), exec.Command("xclip", "-i", "/dev/null", "-sel", "c") }