diff --git a/src/backend/copyUNIXGeneric.go b/src/backend/copyUNIXGeneric.go index 50db16e..f88c48f 100644 --- a/src/backend/copyUNIXGeneric.go +++ b/src/backend/copyUNIXGeneric.go @@ -48,26 +48,30 @@ func copyField(executableName, copySubject string) { func clipClear(oldContents string) { time.Sleep(30 * time.Second) - var envSet bool // track whether environment variables are set - var platform bool // track clipboard platform, false for Wayland, true for X11 - var cmd *exec.Cmd - // determine whether to use wl-copy (Wayland) or xclip (X11) + // determine clipboard tool to use (wl-clipboard VS xclip) + var envSet bool + var cmdClear, cmdPaste *exec.Cmd if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet { - cmd = exec.Command("wl-paste") + cmdClear = exec.Command("wl-copy", "-c") + cmdPaste = exec.Command("wl-paste") } else if _, envSet = os.LookupEnv("DISPLAY"); envSet { - cmd = exec.Command("xclip", "-o", "-sel", "c") - platform = true + cmdClear = exec.Command("xclip", "-i", "/dev/null", "-sel", "c") + cmdPaste = exec.Command("xclip", "-o", "-sel", "c") + } else { + fmt.Println(AnsiError + "Clipboard platform could not be determined - neither $WAYLAND_DISPLAY nor $DISPLAY are set" + AnsiReset) + os.Exit(1) } - newContents, _ := cmd.Output() + // read current clipboard contents + newContents, err := cmdPaste.Output() + if err != nil { + fmt.Println(AnsiError + "Failed to read clipboard contents: " + err.Error() + AnsiReset) + os.Exit(1) + } + + // clear clipboard if contents have not been modified if oldContents == strings.TrimRight(string(newContents), "\r\n") { - switch platform { - case false: - cmd = exec.Command("wl-copy", "-c") - case true: - cmd = exec.Command("xclip", "-i", "/dev/null", "-sel", "c") - } - err := cmd.Run() + err = cmdClear.Run() if err != nil { fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset) os.Exit(1) diff --git a/src/cli/edit.go b/src/cli/edit.go index 26ca406..326bd85 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -91,13 +91,13 @@ func editNote(baseNote []string) ([]string, bool) { cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { - panic(backend.AnsiError + "Failed to write note with " + editor + backend.AnsiReset) + panic(backend.AnsiError + "Failed to write note with " + editor + backend.AnsiReset) // panic is used to ensure the tempFile is removed, as per the defer statement } // open the tempFile for reading tempFile, err = os.Open(tempFile.Name()) if err != nil { - panic(backend.AnsiError + "Failed to write note with " + editor + backend.AnsiReset) + panic(backend.AnsiError + "Failed to write note with " + editor + backend.AnsiReset) // panic is used to ensure the tempFile is removed, as per the defer statement } // read the edited note from the tempFile