-
Notifications
You must be signed in to change notification settings - Fork 4
/
clipboard.go
57 lines (52 loc) · 1001 Bytes
/
clipboard.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
package main
import (
"io"
"os/exec"
"runtime"
"github.com/stretto-editor/gocui"
)
func copy() error {
//http://stackoverflow.com/questions/10781516/how-to-pipe-several-commands-in-go
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
return nil
}
c1 := exec.Command("xclip", "-o")
c2 := exec.Command("xclip", "-i", "-selection", "c")
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
if err := c1.Start(); err != nil {
return err
}
if err := c2.Start(); err != nil {
return err
}
if err := c1.Wait(); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
if err := c2.Wait(); err != nil {
return err
}
return nil
}
func paste(v *gocui.View) error {
if runtime.GOOS == "windows" {
return nil
}
out, err := exec.Command("xclip", "-o", "-selection", "c").Output()
s := string(out)
if err != nil {
return err
}
for _, r := range s {
if rune(r) == '\n' {
v.EditNewLine()
} else {
v.EditWrite(rune(r))
}
}
return nil
}