Implement clipboard clearing functionality on Wayland

This commit is contained in:
2024-03-02 00:12:55 -05:00
parent 4bd44e7b44
commit 3d621f5e38
2 changed files with 68 additions and 23 deletions
+43 -7
View File
@@ -5,24 +5,60 @@ import (
"io"
"os"
"os/exec"
"strings"
"time"
)
func CopyField(targetLocation string, field uint8) {
// TODO Handle cmd.Run errors, especially those due to missing clipboard utilities
// CopyField copies a field from an entry to the clipboard
// TODO Implement support for Windows via powershell, MacOS via pbcopy, Termux via termux-clipboard-set, X11 via xclip
// TODO Monitor Go support for Haiku, if possible implement support via clipboard command
func CopyField(targetLocation string, field uint8, executableName string) {
if isFile, _ := TargetIsFile(targetLocation, true); isFile {
copySubject := DecryptGPG(targetLocation)[field]
//hash := getSha512Sum(copySubject)
cmd := exec.Command("wl-copy")
stdin, _ := cmd.StdinPipe()
go func() {
defer stdin.Close()
io.WriteString(stdin, copySubject)
}()
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 If an implementation opts out of doing this, this may error out
//cmd = exec.Command(executableName, "clipclear", hash)
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
cmd.Start()
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
}
os.Exit(0)
}
func clipClear() {
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// TODO Implement support for Windows via powershell, 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")
newContents, _ := cmd.Output()
if oldContents == strings.TrimRight(string(newContents), "\n") {
cmd := exec.Command("wl-copy", "-c")
cmd.Run()
}
os.Exit(0)
}
// writeToStdin writes a string to a command's stdin
func writeToStdin(cmd *exec.Cmd, input string) {
stdin, _ := cmd.StdinPipe()
go func() {
defer stdin.Close()
io.WriteString(stdin, input)
}()
}