Implement clipboard support for Windows

This commit is contained in:
2024-03-03 19:09:25 -05:00
parent 3d621f5e38
commit 7a17df4aa3
3 changed files with 66 additions and 16 deletions
@@ -1,8 +1,9 @@
//go:build !windows && !darwin
package offline
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
@@ -10,16 +11,14 @@ 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
// 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
// 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]
//hash := getSha512Sum(copySubject)
cmd := exec.Command("wl-copy")
writeToStdin(cmd, copySubject)
cmd.Run()
@@ -27,7 +26,6 @@ func CopyField(targetLocation string, field uint8, executableName string) {
// 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()
@@ -39,7 +37,7 @@ 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 Windows via powershell, MacOS via pbcopy, Termux via termux-clipboard-set, X11 via xclip
// 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)
@@ -53,12 +51,3 @@ func ClipClear(oldContents string) {
}
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)
}()
}
+50
View File
@@ -0,0 +1,50 @@
//go:build windows
package offline
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
// 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
// CopyField copies a field from an entry to the clipboard
func CopyField(targetLocation string, field uint8, executableName string) {
if isFile, _ := TargetIsFile(targetLocation, true); isFile {
copySubject := DecryptGPG(targetLocation)[field]
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()
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
// TODO Make clear time period configurable
func ClipClear(oldContents string) {
time.Sleep(30 * time.Second)
cmd := exec.Command("powershell.exe", "-c", "Get-Clipboard")
newContents, _ := cmd.Output()
if oldContents == strings.TrimRight(string(newContents), "\n") {
cmd := exec.Command("powershell.exe", "-c", "Set-Clipboard")
cmd.Run()
}
os.Exit(0)
}
@@ -2,7 +2,9 @@ package offline
import (
"fmt"
"io"
"os"
"os/exec"
)
// TargetIsFile TargetStatusCheck checks if the targetLocation is a file, directory, or is inaccessible
@@ -23,3 +25,12 @@ func TargetIsFile(targetLocation string, errorOnFail bool) (bool, bool) {
return true, true
}
}
// 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)
}()
}