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

28
main.go
View File

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