1// Copyright 2013 @atotto. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build darwin
6
7package clipboard
8
9import (
10 "os/exec"
11)
12
13var (
14 pasteCmdArgs = "pbpaste"
15 copyCmdArgs = "pbcopy"
16)
17
18func getPasteCommand() *exec.Cmd {
19 return exec.Command(pasteCmdArgs)
20}
21
22func getCopyCommand() *exec.Cmd {
23 return exec.Command(copyCmdArgs)
24}
25
26func readAll() (string, error) {
27 pasteCmd := getPasteCommand()
28 out, err := pasteCmd.Output()
29 if err != nil {
30 return "", err
31 }
32 return string(out), nil
33}
34
35func writeAll(text string) error {
36 copyCmd := getCopyCommand()
37 in, err := copyCmd.StdinPipe()
38 if err != nil {
39 return err
40 }
41
42 if err := copyCmd.Start(); err != nil {
43 return err
44 }
45 if _, err := in.Write([]byte(text)); err != nil {
46 return err
47 }
48 if err := in.Close(); err != nil {
49 return err
50 }
51 return copyCmd.Wait()
52}