| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- // Copyright 2013 @atotto. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // +build freebsd linux netbsd openbsd solaris dragonfly
- package clipboard
- import (
- "errors"
- "os"
- "os/exec"
- )
- const (
- xsel = "xsel"
- xclip = "xclip"
- powershellExe = "powershell.exe"
- clipExe = "clip.exe"
- wlcopy = "wl-copy"
- wlpaste = "wl-paste"
- termuxClipboardGet = "termux-clipboard-get"
- termuxClipboardSet = "termux-clipboard-set"
- )
- var (
- Primary bool
- trimDos bool
- pasteCmdArgs []string
- copyCmdArgs []string
- xselPasteArgs = []string{xsel, "--output", "--clipboard"}
- xselCopyArgs = []string{xsel, "--input", "--clipboard"}
- xclipPasteArgs = []string{xclip, "-out", "-selection", "clipboard"}
- xclipCopyArgs = []string{xclip, "-in", "-selection", "clipboard"}
- powershellExePasteArgs = []string{powershellExe, "Get-Clipboard"}
- clipExeCopyArgs = []string{clipExe}
- wlpasteArgs = []string{wlpaste, "--no-newline"}
- wlcopyArgs = []string{wlcopy}
- termuxPasteArgs = []string{termuxClipboardGet}
- termuxCopyArgs = []string{termuxClipboardSet}
- missingCommands = errors.New("No clipboard utilities available. Please install xsel, xclip, wl-clipboard or Termux:API add-on for termux-clipboard-get/set.")
- )
- func init() {
- if os.Getenv("WAYLAND_DISPLAY") != "" {
- pasteCmdArgs = wlpasteArgs
- copyCmdArgs = wlcopyArgs
- if _, err := exec.LookPath(wlcopy); err == nil {
- if _, err := exec.LookPath(wlpaste); err == nil {
- return
- }
- }
- }
- pasteCmdArgs = xclipPasteArgs
- copyCmdArgs = xclipCopyArgs
- if _, err := exec.LookPath(xclip); err == nil {
- return
- }
- pasteCmdArgs = xselPasteArgs
- copyCmdArgs = xselCopyArgs
- if _, err := exec.LookPath(xsel); err == nil {
- return
- }
- pasteCmdArgs = termuxPasteArgs
- copyCmdArgs = termuxCopyArgs
- if _, err := exec.LookPath(termuxClipboardSet); err == nil {
- if _, err := exec.LookPath(termuxClipboardGet); err == nil {
- return
- }
- }
- pasteCmdArgs = powershellExePasteArgs
- copyCmdArgs = clipExeCopyArgs
- trimDos = true
- if _, err := exec.LookPath(clipExe); err == nil {
- if _, err := exec.LookPath(powershellExe); err == nil {
- return
- }
- }
- Unsupported = true
- }
- func getPasteCommand() *exec.Cmd {
- if Primary {
- pasteCmdArgs = pasteCmdArgs[:1]
- }
- return exec.Command(pasteCmdArgs[0], pasteCmdArgs[1:]...)
- }
- func getCopyCommand() *exec.Cmd {
- if Primary {
- copyCmdArgs = copyCmdArgs[:1]
- }
- return exec.Command(copyCmdArgs[0], copyCmdArgs[1:]...)
- }
- func readAll() (string, error) {
- if Unsupported {
- return "", missingCommands
- }
- pasteCmd := getPasteCommand()
- out, err := pasteCmd.Output()
- if err != nil {
- return "", err
- }
- result := string(out)
- if trimDos && len(result) > 1 {
- result = result[:len(result)-2]
- }
- return result, nil
- }
- func writeAll(text string) error {
- if Unsupported {
- return missingCommands
- }
- copyCmd := getCopyCommand()
- in, err := copyCmd.StdinPipe()
- if err != nil {
- return err
- }
- if err := copyCmd.Start(); err != nil {
- return err
- }
- if _, err := in.Write([]byte(text)); err != nil {
- return err
- }
- if err := in.Close(); err != nil {
- return err
- }
- return copyCmd.Wait()
- }
|