From 82b8bda35a85d6e39b6535b969850908668530ca Mon Sep 17 00:00:00 2001 From: d-tsuji Date: Sun, 10 May 2020 09:12:21 +0900 Subject: [PATCH] initial commit --- .github/workflows/test.yaml | 29 +++++++++++++++++++++++++++++ .gitignore | 25 +++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ Makefile | 33 +++++++++++++++++++++++++++++++++ README.md | 17 +++++++++++++++++ clipboard.go | 11 +++++++++++ clipboard_darwin.go | 20 ++++++++++++++++++++ clipboard_test.go | 21 +++++++++++++++++++++ clipboard_windows.go | 15 +++++++++++++++ go.mod | 12 ++++++++++++ go.sum | 13 +++++++++++++ 11 files changed, 217 insertions(+) create mode 100644 .github/workflows/test.yaml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 clipboard.go create mode 100644 clipboard_darwin.go create mode 100644 clipboard_test.go create mode 100644 clipboard_windows.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..df6cf79 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,29 @@ +name: test +on: [push, pull_request] +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: +# - ubuntu-latest + - macOS-latest + - windows-latest + steps: + - name: Checkout code + uses: actions/checkout@master + - name: Setup X server on ubuntu-latest + if: runner.os == 'Linux' + run: | + sudo apt update -y + sudo apt install -y xorg openbox + sudo startx + - name: Setup Go + uses: actions/setup-go@v1 + with: + go-version: 1.x + - name: Add $GOPATH/bin to $PATH + run: echo "::add-path::$(go env GOPATH)/bin" + - name: Test + run: make test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bc5e31 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# GoLand +.idea/ + +# Executable Binary +clipboard + +# Go test coverage +c.out +coverage.html diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..92fde69 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Tsuji Daishiro + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3185834 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +.PHONY: all build test lint clean deps devel-deps + +BIN := clipboard +BUILD_LDFLAGS := "-s -w" +GOBIN ?= $(shell go env GOPATH)/bin +export GO111MODULE=on + +all: clean build + +deps: + go mod tidy + +devel-deps: deps + GO111MODULE=off go get -u \ + golang.org/x/lint/golint + +build: clean + go build -ldflags=$(BUILD_LDFLAGS) -o $(BIN) + +test: deps + go test -v -race -count=1 ./... + +test-cover: deps + go test -v -race -count=1 ./... -cover -coverprofile=c.out + go tool cover -html=c.out -o coverage.html + +lint: devel-deps + go vet ./... + $(GOBIN)/golint -set_exit_status ./... + +clean: + rm -rf $(BIN) + go clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..97e57fd --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# clipboard + +"clipboard" is a multi-platform clipboard library in Go. + +## Abstract + +- This is clipboard library in Go, which runs on multiple platforms. +- External clipboard package is not required. + +## Supported Platforms + +- Windows +- macOS + +### ⚠WIP⚠ + +- Linux, Unix (X11) diff --git a/clipboard.go b/clipboard.go new file mode 100644 index 0000000..2225c9e --- /dev/null +++ b/clipboard.go @@ -0,0 +1,11 @@ +package clipboard + +// Get returns the current text data of the clipboard. +func Get() (string, error) { + return get() +} + +// Set sets the current text data of the clipboard. +func Set(text string) error { + return set(text) +} diff --git a/clipboard_darwin.go b/clipboard_darwin.go new file mode 100644 index 0000000..c732e3a --- /dev/null +++ b/clipboard_darwin.go @@ -0,0 +1,20 @@ +// +build darwin + +package clipboard + +import ( + "git.wow.st/gmp/clip" + "golang.org/x/xerrors" +) + +func set(text string) error { + ok := clip.Set(text) + if !ok { + return xerrors.New("nothing to set string") + } + return nil +} + +func get() (string, error) { + return clip.Get(), nil +} diff --git a/clipboard_test.go b/clipboard_test.go new file mode 100644 index 0000000..1df6b5b --- /dev/null +++ b/clipboard_test.go @@ -0,0 +1,21 @@ +package clipboard + +import ( + "testing" +) + +func TestCopyAndPaste(t *testing.T) { + want := "gopher" + if err := Set(want); err != nil { + t.Error(err) + } + + got, err := Get() + if err != nil { + t.Error(err) + } + + if got != want { + t.Errorf("CopyAndPaste mismatch: got %v, want %v", got, want) + } +} diff --git a/clipboard_windows.go b/clipboard_windows.go new file mode 100644 index 0000000..7456801 --- /dev/null +++ b/clipboard_windows.go @@ -0,0 +1,15 @@ +// +build windows + +package clipboard + +import "github.com/lxn/walk" + +func get() (string, error) { + c := walk.Clipboard() + return c.Text() +} + +func set(text string) error { + c := walk.Clipboard() + return c.SetText(text) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9342d46 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/d-tsuji/clipboard + +go 1.14 + +require ( + git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5 + github.com/lxn/walk v0.0.0-20191128110447-55ccb3a9f5c1 + github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4 // indirect + golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 + gopkg.in/Knetic/govaluate.v3 v3.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0eae13e --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5 h1:OKeTjZST+/TKvtdA258NXJH+/gIx/xwyZxKrAezNFvk= +git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5/go.mod h1:NLdpaBoMQNFqncwP8OVRNWUDw1Kt9XWm3snfT7cXu24= +github.com/lxn/walk v0.0.0-20191128110447-55ccb3a9f5c1 h1:/QwQcwWVOQXcoNuV9tHx30gQ3q7jCE/rKcGjwzsa5tg= +github.com/lxn/walk v0.0.0-20191128110447-55ccb3a9f5c1/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ= +github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4 h1:5BmtGkQbch91lglMHQ9JIDGiYCL3kBRBA0ItZTvOcEI= +github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4/go.mod h1:ouWl4wViUNh8tPSIwxTVMuS014WakR1hqvBc2I0bMoA= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f h1:mOhmO9WsBaJCNmaZHPtHs9wOcdqdKCjF6OPJlmDM3KI= +golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/Knetic/govaluate.v3 v3.0.0 h1:18mUyIt4ZlRlFZAAfVetz4/rzlJs9yhN+U02F4u1AOc= +gopkg.in/Knetic/govaluate.v3 v3.0.0/go.mod h1:csKLBORsPbafmSCGTEh3U7Ozmsuq8ZSIlKk1bcqph0E=