xclip verbose error output.

This commit is contained in:
Miroslav Šedivý 2021-01-28 21:19:31 +01:00
parent 11e74459ac
commit 4602f5f6cf

View File

@ -1,17 +1,40 @@
package desktop
import (
"fmt"
"bytes"
"os/exec"
"strings"
)
func (manager *DesktopManagerCtx) ReadClipboard() (string, error) {
out, err := exec.Command("xclip", "-selection", "clipboard", "-o").Output()
return string(out), err
cmd := exec.Command("xclip", "-selection", "clipboard", "-o")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
msg := strings.TrimSpace(string(stderr.Bytes()))
return "", fmt.Errorf("%s", msg)
}
return string(stdout.Bytes()), nil
}
func (manager *DesktopManagerCtx) WriteClipboard(data string) error {
cmd := exec.Command("xclip", "-selection", "clipboard", "-i")
cmd.Stdin = strings.NewReader(data)
return cmd.Run()
cmd.Stdin = strings.NewReader(data)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
msg := strings.TrimSpace(string(stderr.Bytes()))
return fmt.Errorf("%s", msg)
}
return nil
}