github.com/puellanivis/breton@v0.2.16/lib/files/clipboard/exec.go (about)

     1  package clipboard
     2  
     3  import (
     4  	"bytes"
     5  	"net/url"
     6  	"os"
     7  	"os/exec"
     8  	"time"
     9  
    10  	"github.com/puellanivis/breton/lib/files/wrapper"
    11  )
    12  
    13  type execClip struct {
    14  	name  *url.URL
    15  	paste []string
    16  	copy  []string
    17  }
    18  
    19  func newExecClip(name string, target ...string) {
    20  	if len(target) < 1 {
    21  		target = append(target, name)
    22  	}
    23  
    24  	clipboards[name] = &execClip{
    25  		name: &url.URL{
    26  			Scheme: "clipboard",
    27  			Opaque: name,
    28  		},
    29  		paste: append(pasteCmd, append(selParam, target...)...),
    30  		copy:  append(copyCmd, append(selParam, target...)...),
    31  	}
    32  }
    33  
    34  func (c *execClip) Read() ([]byte, error) {
    35  	return exec.Command(c.paste[0], c.paste[1:]...).Output()
    36  }
    37  
    38  func (c *execClip) Write(b []byte) error {
    39  	cmd := exec.Command(c.copy[0], c.copy[1:]...)
    40  
    41  	cmd.Stdin = bytes.NewReader(b)
    42  
    43  	return cmd.Run()
    44  }
    45  
    46  func (c *execClip) Stat() (os.FileInfo, error) {
    47  	b, err := c.Read()
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return wrapper.NewInfo(c.name, len(b), time.Now()), nil
    53  }