diff --git a/src/offline/copy.go b/src/offline/UNIXcopy.go similarity index 68% rename from src/offline/copy.go rename to src/offline/UNIXcopy.go index af88cec..7087c0b 100644 --- a/src/offline/copy.go +++ b/src/offline/UNIXcopy.go @@ -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) - }() -} diff --git a/src/offline/WINcopy.go b/src/offline/WINcopy.go new file mode 100644 index 0000000..b6e2592 --- /dev/null +++ b/src/offline/WINcopy.go @@ -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) +} diff --git a/src/offline/targetChecks.go b/src/offline/utility.go similarity index 73% rename from src/offline/targetChecks.go rename to src/offline/utility.go index 9590a5c..448d723 100644 --- a/src/offline/targetChecks.go +++ b/src/offline/utility.go @@ -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) + }() +}