Debian Codegraph @

clipboard.go

raw symbols SCIP
1 package clipper
2
3 import (
4 "fmt"
5 "os/exec"
6 )
7
8 type Clipboard interface {
9 // Init initializes the clipboard and returns an error if it is not
10 // accessible
11 Init() error
12 // ReadAll returns the contents of the clipboard register 'reg'
13 ReadAll(reg string) ([]byte, error)
14 // WriteAll writes 'p' to the clipboard register 'reg'
15 WriteAll(reg string, p []byte) error
16 }
17
18 const (
19 RegClipboard = "clipboard"
20 RegPrimary = "primary"
21 )
22
23 type ErrInvalidReg struct {
24 Reg string
25 }
26
27 func (e *ErrInvalidReg) Error() string {
28 return fmt.Sprintf("invalid register: %s", e.Reg)
29 }
30
31 func verify(clip Clipboard, cmds ...string) error {
32 for _, cmd := range cmds {
33 if _, err := exec.LookPath(cmd); err != nil {
34 // command not installed
35 return err
36 }
37 }
38 _, err := clip.ReadAll(RegClipboard)
39 if err == nil {
40 // reading clipboard worked
41 return nil
42 }
43 // reading could fail if the clipboard has no contents, so check if writing
44 // works in that case
45 return clip.WriteAll(RegClipboard, []byte{})
46 }
47
48 func write(cmd *exec.Cmd, p []byte) error {
49 in, err := cmd.StdinPipe()
50 if err != nil {
51 return err
52 }
53 if err := cmd.Start(); err != nil {
54 return err
55 }
56 if _, err := in.Write(p); err != nil {
57 return err
58 }
59 in.Close()
60 return cmd.Wait()
61 }