1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// 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
package clipboard
import "os/exec"
const (
xsel = "xsel"
xclip = "xclip"
)
var (
internalClipboards map[string]string
)
func init() {
if _, err := exec.LookPath(xclip); err == nil {
if err := exec.Command("xclip", "-o").Run(); err == nil {
return
}
}
if _, err := exec.LookPath(xsel); err == nil {
if err := exec.Command("xsel").Run(); err == nil {
return
}
}
internalClipboards = make(map[string]string)
Unsupported = true
}
func copyCommand(register string) []string {
if _, err := exec.LookPath(xclip); err == nil {
return []string{xclip, "-in", "-selection", register}
}
if _, err := exec.LookPath(xsel); err == nil {
return []string{xsel, "--input", "--" + register}
}
return []string{}
}
func pasteCommand(register string) []string {
if _, err := exec.LookPath(xclip); err == nil {
return []string{xclip, "-out", "-selection", register}
}
if _, err := exec.LookPath(xsel); err == nil {
return []string{xsel, "--output", "--" + register}
}
return []string{}
}
func getPasteCommand(register string) *exec.Cmd {
pasteCmdArgs := pasteCommand(register)
return exec.Command(pasteCmdArgs[0], pasteCmdArgs[1:]...)
}
func getCopyCommand(register string) *exec.Cmd {
copyCmdArgs := copyCommand(register)
return exec.Command(copyCmdArgs[0], copyCmdArgs[1:]...)
}
func readAll(register string) (string, error) {
if Unsupported {
if text, ok := internalClipboards[register]; ok {
return text, nil
}
return "", nil
}
pasteCmd := getPasteCommand(register)
out, err := pasteCmd.Output()
if err != nil {
return "", err
}
return string(out), nil
}
func writeAll(text string, register string) error {
if Unsupported {
internalClipboards[register] = text
return nil
}
copyCmd := getCopyCommand(register)
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()
}
|