diff --git a/.travis.yml b/.travis.yml index 5bd5ae3..4032de9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,17 @@ language: go go: - - go1.4.3 - - go1.5.4 - - go1.6.4 - - go1.7.6 - - go1.8.7 - - go1.9.4 - - go1.10 + - "1.4" + - "1.5" + - "1.6" + - "1.7" + - "1.8" + - "1.9" + - "1.10" + - "1.11" + - "1.12" + - "1.13" + - "1.14" before_install: - export DISPLAY=:99.0 diff --git a/README.md b/README.md index 41fdd57..c130126 100644 --- a/README.md +++ b/README.md @@ -4,45 +4,40 @@ # Clipboard for Go -Provide copying and pasting to the Clipboard for Go. +Provide functionality for copying from and pasting to the clipboard. -Build: +### Build - $ go get github.com/atotto/clipboard + $ go get -u github.com/atotto/clipboard -Platforms: +### Platforms -* OSX -* Windows 7 (probably work on other Windows) -* Linux, Unix (requires 'xclip' or 'xsel' command to be installed) +* macOS +* Windows 7 (probably works on later editions too) +* Linux, Unix (requires `xclip` and `xsel` to be installed (or `wlpaste` and `wlcopy`, for Wayland) - -Document: +### Documentation * http://godoc.org/github.com/atotto/clipboard -Notes: +### Notes -* Text string only -* UTF-8 text encoding only (no conversion) +* For functions that takes or return a string, only UTF-8 is supported -TODO: +### TODO -* Clipboard watcher(?) +- [ ] Clipboard watcher(?) -## Commands: +## Commands paste shell command: - $ go get github.com/atotto/clipboard/cmd/gopaste + $ go get -u github.com/atotto/clipboard/cmd/gopaste $ # example: $ gopaste > document.txt copy shell command: - $ go get github.com/atotto/clipboard/cmd/gocopy + $ go get -u github.com/atotto/clipboard/cmd/gocopy $ # example: $ cat document.txt | gocopy - - - diff --git a/clipboard.go b/clipboard.go index d7907d3..dec4584 100644 --- a/clipboard.go +++ b/clipboard.go @@ -5,16 +5,26 @@ // Package clipboard read/write on clipboard package clipboard -// ReadAll read string from clipboard +// ReadAll will read a string from the clipboard func ReadAll() (string, error) { return readAll() } -// WriteAll write string to clipboard +// WriteAll will write a string to the clipboard func WriteAll(text string) error { return writeAll(text) } +// ReadAllBytes will read bytes from the clipboard +func ReadAllBytes() ([]byte, error) { + return readAllBytes() +} + +// WriteAllBytes will write bytes to the clipboard +func WriteAllBytes(b []byte) error { + return writeAllBytes(b) +} + // Unsupported might be set true during clipboard init, to help callers decide // whether or not to offer clipboard options. var Unsupported bool diff --git a/clipboard_test.go b/clipboard_test.go index 075e657..0a81a11 100644 --- a/clipboard_test.go +++ b/clipboard_test.go @@ -28,6 +28,37 @@ func TestCopyAndPaste(t *testing.T) { } } +func TestCopyAndPasteBytes(t *testing.T) { + expected := []byte{0, 1, 2, 3, 4, 5, 6, 7} + + err := WriteAllBytes(expected) + if err != nil { + t.Fatal(err) + } + + actual, err := ReadAllBytes() + if err != nil { + t.Fatal(err) + } + + min := func(a, b int) int { + if a < b { + return a + } + return b + } + + // For testing the test + // expected[0] = 42 + + for i := 0; i < min(len(actual), len(expected)); i++ { + if actual[i] != expected[i] { + t.Errorf("want %s, got %s", expected, actual) + break + } + } +} + func TestMultiCopyAndPaste(t *testing.T) { expected1 := "French: éèêëàùœç" expected2 := "Weird UTF-8: 💩☃" @@ -71,3 +102,17 @@ func BenchmarkWriteAll(b *testing.B) { WriteAll(text) } } + +func BenchmarkReadAllBytes(b *testing.B) { + for i := 0; i < b.N; i++ { + ReadAllBytes() + } +} + +var bs = []byte("いろはにほへと") + +func BenchmarkWriteAllBytes(b *testing.B) { + for i := 0; i < b.N; i++ { + WriteAllBytes(bs) + } +} diff --git a/clipboard_unix.go b/clipboard_unix.go index 8f5e23c..8fda135 100644 --- a/clipboard_unix.go +++ b/clipboard_unix.go @@ -15,8 +15,8 @@ import ( const ( xsel = "xsel" xclip = "xclip" - wlcopy = "wl-copy" - wlpaste = "wl-paste" + wlcopy = "wl-copy" + wlpaste = "wl-paste" termuxClipboardGet = "termux-clipboard-get" termuxClipboardSet = "termux-clipboard-set" ) @@ -34,18 +34,18 @@ var ( xclipCopyArgs = []string{xclip, "-in", "-selection", "clipboard"} wlpasteArgs = []string{wlpaste, "--no-newline"} - wlcopyArgs = []string{wlcopy} + wlcopyArgs = []string{wlcopy} termuxPasteArgs = []string{termuxClipboardGet} termuxCopyArgs = []string{termuxClipboardSet} - missingCommands = errors.New("No clipboard utilities available. Please install xsel, xclip, wl-clipboard or Termux:API add-on for termux-clipboard-get/set.") + errMissingCommands = errors.New("No clipboard utilities available. Please install xsel, xclip, wl-clipboard or Termux:API add-on for termux-clipboard-get/set.") ) func init() { if os.Getenv("WAYLAND_DISPLAY") != "" { - pasteCmdArgs = wlpasteArgs; - copyCmdArgs = wlcopyArgs; + pasteCmdArgs = wlpasteArgs + copyCmdArgs = wlcopyArgs if _, err := exec.LookPath(wlcopy); err == nil { if _, err := exec.LookPath(wlpaste); err == nil { @@ -94,21 +94,29 @@ func getCopyCommand() *exec.Cmd { return exec.Command(copyCmdArgs[0], copyCmdArgs[1:]...) } -func readAll() (string, error) { +func readAllBytes() ([]byte, error) { if Unsupported { - return "", missingCommands + return []byte{}, errMissingCommands } pasteCmd := getPasteCommand() out, err := pasteCmd.Output() + if err != nil { + return []byte{}, err + } + return out, nil +} + +func readAll() (string, error) { + b, err := readAllBytes() if err != nil { return "", err } - return string(out), nil + return string(b), nil } -func writeAll(text string) error { +func writeAllBytes(b []byte) error { if Unsupported { - return missingCommands + return errMissingCommands } copyCmd := getCopyCommand() in, err := copyCmd.StdinPipe() @@ -119,7 +127,7 @@ func writeAll(text string) error { if err := copyCmd.Start(); err != nil { return err } - if _, err := in.Write([]byte(text)); err != nil { + if _, err := in.Write(b); err != nil { return err } if err := in.Close(); err != nil { @@ -127,3 +135,7 @@ func writeAll(text string) error { } return copyCmd.Wait() } + +func writeAll(text string) error { + return writeAllBytes([]byte(text)) +}