diff --git a/src/offline/2UNIXglobals.go b/src/offline/2globalsUNIX.go similarity index 100% rename from src/offline/2UNIXglobals.go rename to src/offline/2globalsUNIX.go diff --git a/src/offline/3WINglobals.go b/src/offline/2globalsWIN.go similarity index 100% rename from src/offline/3WINglobals.go rename to src/offline/2globalsWIN.go diff --git a/src/offline/UNIXcopy.go b/src/offline/copyUNIXGeneric.go similarity index 50% rename from src/offline/UNIXcopy.go rename to src/offline/copyUNIXGeneric.go index cd28b13..320488c 100644 --- a/src/offline/UNIXcopy.go +++ b/src/offline/copyUNIXGeneric.go @@ -12,19 +12,29 @@ import ( // TODO Handle cmd.Run errors, especially those due to missing clipboard utilities // TODO Handle index out of range errors when copying fields that do not exist +// TODO Implement support for MacOS via pbcopy, Termux via termux-clipboard-set (in separate files) // CopyField copies a field from an entry to the clipboard -// TODO Implement support for MacOS via pbcopy, Termux via termux-clipboard-set, X11 via xclip func CopyField(targetLocation string, field uint8, executableName string) { if isFile, _ := TargetIsFile(targetLocation, true); isFile { copySubject := DecryptGPG(targetLocation)[field] - cmd := exec.Command("wl-copy") + var envSet bool // track whether environment variables are set + var cmd *exec.Cmd + // determine whether to use wl-copy (Wayland) or xclip (X11) + if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet { + cmd = exec.Command("wl-copy") + } else if _, envSet = os.LookupEnv("DISPLAY"); envSet { + cmd = exec.Command("xclip", "-sel", "c") + } else { + fmt.Println(AnsiError + "Clipboard platform could not be determined - note that the clipboard does not function in a raw TTY" + AnsiReset) + os.Exit(1) + } + writeToStdin(cmd, copySubject) cmd.Run() - // TODO Unpair from mutn, as this breaks library functionality - // TODO Maybe use this method for mutn and strongly encourage other implementations to support a clipclear argument + // TODO Strongly encourage other implementations to support a clipclear argument // TODO If an implementation opts out of doing this, this may error out cmd = exec.Command(executableName, "clipclear") writeToStdin(cmd, copySubject) @@ -37,16 +47,29 @@ func CopyField(targetLocation string, field uint8, executableName string) { } // ClipClear is called in a separate process to clear the clipboard after 30 seconds -// TODO Implement support for MacOS via pbcopy, Termux via termux-clipboard-set, X11 via xclip // TODO Make clear time period configurable func ClipClear(oldContents string) { time.Sleep(30 * time.Second) - cmd := exec.Command("wl-paste") + 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) + if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet { + cmd = exec.Command("wl-paste") + } else if _, envSet = os.LookupEnv("DISPLAY"); envSet { + cmd = exec.Command("xclip", "-o", "-sel", "c") + platform = true + } newContents, _ := cmd.Output() if oldContents == strings.TrimRight(string(newContents), "\r\n") { - cmd = exec.Command("wl-copy", "-c") + switch platform { + case false: + cmd = exec.Command("wl-copy", "-c") + case true: + cmd = exec.Command("xclip", "-i", "/dev/null", "-sel", "c") + } cmd.Run() } os.Exit(0) diff --git a/src/offline/WINcopy.go b/src/offline/copyWIN.go similarity index 83% rename from src/offline/WINcopy.go rename to src/offline/copyWIN.go index 3f6398c..aebc1ef 100644 --- a/src/offline/WINcopy.go +++ b/src/offline/copyWIN.go @@ -21,9 +21,6 @@ func CopyField(targetLocation string, field uint8, executableName string) { cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", copySubject)) cmd.Run() - // TODO Unpair from mutn, as this breaks library functionality - // TODO Maybe use this method for mutn and strongly encourage other implementations to support a clipclear argument - // TODO If an implementation opts out of doing this, this may error out cmd = exec.Command(executableName, "clipclear") writeToStdin(cmd, copySubject) cmd.Start()