Implement X11 clipboard support via xclip

This commit is contained in:
2024-03-03 20:58:51 -05:00
parent 00e0a70ee6
commit 9802b7498b
4 changed files with 30 additions and 10 deletions
@@ -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)
@@ -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()