forked from zyedidia/clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard_unix.go
152 lines (127 loc) · 3.54 KB
/
clipboard_unix.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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"
wlcopy = "wl-copy"
wlpaste = "wl-paste"
termuxClipboardGet = "termux-clipboard-get"
termuxClipboardSet = "termux-clipboard-set"
)
var (
pasteCmdArgs map[string][]string
copyCmdArgs map[string][]string
xselPasteArgs = map[string][]string{
"primary": []string{xsel, "--output"},
"clipboard": []string{xsel, "--output", "--clipboard"},
}
xselCopyArgs = map[string][]string{
"primary": []string{xsel, "--input"},
"clipboard": []string{xsel, "--input", "--clipboard"},
}
xclipPasteArgs = map[string][]string{
"primary": []string{xclip, "-out"},
"clipboard": []string{xclip, "-out", "-selection", "clipboard"},
}
xclipCopyArgs = map[string][]string{
"primary": []string{xclip, "-in"},
"clipboard": []string{xclip, "-in", "-selection", "clipboard"},
}
wlpasteArgs = map[string][]string{
"primary": []string{wlpaste, "--no-newline", "--primary"},
"clipboard": []string{wlpaste, "--no-newline"},
}
wlcopyArgs = map[string][]string{
"primary": []string{wlcopy, "--primary"},
"clipboard": []string{wlcopy},
}
termuxPasteArgs = map[string][]string{
"primary": []string{termuxClipboardGet},
"clipboard": []string{termuxClipboardGet},
}
termuxCopyArgs = map[string][]string{
"primary": []string{termuxClipboardSet},
"clipboard": []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.")
internalClipboards map[string]string
)
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
}
}
internalClipboards = make(map[string]string)
Unsupported = true
}
func getPasteCommand(register string) *exec.Cmd {
return exec.Command(pasteCmdArgs[register][0], pasteCmdArgs[register][1:]...)
}
func getCopyCommand(register string) *exec.Cmd {
return exec.Command(copyCmdArgs[register][0], copyCmdArgs[register][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, 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()
}