From 75c6d311273ea222fb0554ae106454d074d235fa Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 20 Aug 2020 15:18:05 +0200 Subject: [PATCH] Initial commit --- LICENSE | 8 ++++++++ README.md | 16 ++++++++++++++++ main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf45fa9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2020 Gregory Eremin + +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/README.md b/README.md new file mode 100644 index 0000000..7b37dc3 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# wl-clipsync + +Synchronizes clipboard and primary paste buffers. + +Depends on [wl-clipboard](https://github.com/bugaevc/wl-clipboard). + +## Installation + +``` +go get -u github.com/localhots/wl-clipwync +wl-clipsync & +``` + +# License + +MIT diff --git a/main.go b/main.go new file mode 100644 index 0000000..63a038c --- /dev/null +++ b/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "flag" + "time" + "os/exec" + "bytes" +) + + +func main() { + interval := flag.Duration("i", 100*time.Millisecond, "Clipboard polling interval") + flag.Parse() + + fmt.Println("Starting clipboard sync") + fmt.Println("Polling interval:", *interval) + + var unified string + for range time.NewTicker(*interval).C { + primary := getVal("-p") + if primary != unified { + unified = primary + setVal(unified) + fmt.Println("Setting clip to", unified) + } + + selection := getVal() + if selection != unified { + unified = selection + setVal(unified, "-p") + fmt.Println("Setting primary to", unified) + } + } +} + +func getVal(args ...string) string { + args = append(args, "-n") + out, err := exec.Command("wl-paste", args...).Output() + if err != nil { + panic(err) + } + return string(out) +} + +func setVal(val string, args... string) { + cmd := exec.Command("wl-copy") + cmd.Stdin = bytes.NewBufferString(val) + if err := cmd.Run(); err != nil { + panic(err) + } +} \ No newline at end of file