github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/clipboard/clipboard_darwin.go (about)

     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  
     7  package clipboard
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"os/exec"
    13  )
    14  
    15  var (
    16  	pasteCmdArgs = "pbpaste"
    17  	copyCmdArgs  = "pbcopy"
    18  )
    19  
    20  func getPasteCommand() *exec.Cmd {
    21  	cmd := exec.Command(pasteCmdArgs)
    22  	cmd.Env = []string{"LANG=en_US.UTF-8"}
    23  	return cmd
    24  }
    25  
    26  func getCopyCommand() *exec.Cmd {
    27  	return exec.Command(copyCmdArgs)
    28  }
    29  
    30  func readAll() (string, error) {
    31  	pasteCmd := getPasteCommand()
    32  	out, err := pasteCmd.Output()
    33  	if err != nil {
    34  		return "", err
    35  	}
    36  	return string(out), nil
    37  }
    38  
    39  func writeAll(text string) error {
    40  	copyCmd := getCopyCommand()
    41  	in, err := copyCmd.StdinPipe()
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	if err := copyCmd.Start(); err != nil {
    47  		return err
    48  	}
    49  	if _, err := in.Write([]byte(text)); err != nil {
    50  		return err
    51  	}
    52  	if err := in.Close(); err != nil {
    53  		return err
    54  	}
    55  	return copyCmd.Wait()
    56  }
    57  
    58  func Start() {
    59  }
    60  
    61  func Get() string {
    62  	str, err := readAll()
    63  	if err != nil {
    64  		fmt.Fprintln(os.Stderr, err)
    65  		return ""
    66  	}
    67  	return str
    68  }
    69  
    70  func GetPrimary() string {
    71  	return ""
    72  }
    73  
    74  func Set(text string) {
    75  	err := writeAll(text)
    76  	if err != nil {
    77  		fmt.Fprintln(os.Stderr, err)
    78  	}
    79  }