1
0
Fork 0

Fix bug, wrap debug logs

This commit is contained in:
Gregory Eremin 2020-08-28 17:37:44 +02:00
parent 75c6d31127
commit b0cb3723f9
1 changed files with 23 additions and 11 deletions

34
main.go
View File

@ -6,11 +6,14 @@ import (
"time"
"os/exec"
"bytes"
"log"
)
func main() {
interval := flag.Duration("i", 100*time.Millisecond, "Clipboard polling interval")
debug := flag.Bool("d", false, "Debug output")
lotsofDebug := flag.Bool("dd", false, "Full debug")
flag.Parse()
fmt.Println("Starting clipboard sync")
@ -19,34 +22,43 @@ func main() {
var unified string
for range time.NewTicker(*interval).C {
primary := getVal("-p")
if primary != unified {
if *lotsofDebug {
log.Println("Primary:", primary)
}
if primary != "" && primary != unified {
unified = primary
setVal(unified)
fmt.Println("Setting clip to", unified)
if *debug {
fmt.Println("Setting clip to", unified)
}
continue
}
selection := getVal()
if selection != unified {
if *lotsofDebug {
log.Println("Selection:", primary)
}
if selection != "" && selection != unified {
unified = selection
setVal(unified, "-p")
fmt.Println("Setting primary to", unified)
if *debug {
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)
}
out, _ := exec.Command("wl-paste", args...).Output()
return string(out)
}
func setVal(val string, args... string) {
cmd := exec.Command("wl-copy")
cmd := exec.Command("wl-copy", args...)
cmd.Stdin = bytes.NewBufferString(val)
if err := cmd.Run(); err != nil {
panic(err)
log.Printf("Failed to get paste buffer value (%v): %v", args, err)
log.Printf("Value in question: %q", val)
}
}
}