Implement untested MacOS clipboard support via pbcopy

This commit is contained in:
2024-03-03 22:36:20 -05:00
parent 207a4762b6
commit 0e6210f1af
4 changed files with 79 additions and 6 deletions
+75
View File
@@ -0,0 +1,75 @@
//go:build darwin
package offline
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
// TODO MacOS support is entirely untested - I would appreciate feedback on this implementation
// CopyField copies a field from an entry to the clipboard
func CopyField(targetLocation string, field int, executableName string) {
if isFile, _ := TargetIsFile(targetLocation, true); isFile {
decryptedEntry := DecryptGPG(targetLocation)
var copySubject string // will store data to be copied
// ensure field exists in entry
if len(decryptedEntry) > field {
copySubject = decryptedEntry[field]
} else {
fmt.Println(AnsiError + "Field does not exist in entry" + AnsiReset)
os.Exit(1)
}
// ensure field is not blank
if copySubject == "" {
fmt.Println(AnsiError + "Field is empty" + AnsiReset)
os.Exit(1)
}
cmd := exec.Command("pbcopy")
writeToStdin(cmd, copySubject)
err := cmd.Run()
if err != nil {
fmt.Println(AnsiError + "Failed to copy to clipboard: " + err.Error() + AnsiReset)
os.Exit(1)
}
cmd = exec.Command(executableName, "clipclear")
writeToStdin(cmd, copySubject)
err = cmd.Start()
if err != nil {
fmt.Println(AnsiError + "Failed to launch automated clipboard clearing process - does this libmutton implementation support the \"clipclear\" argument?" + AnsiReset)
os.Exit(1)
}
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
os.Exit(1)
}
os.Exit(0)
}
// ClipClear is called in a separate process to clear the clipboard after 30 seconds
func ClipClear(oldContents string) {
time.Sleep(30 * time.Second)
cmd := exec.Command("pbpaste")
newContents, _ := cmd.Output()
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
cmd = exec.Command("pbcopy")
writeToStdin(cmd, "")
err := cmd.Run()
if err != nil {
fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset)
os.Exit(1)
}
}
os.Exit(0)
}
+2 -2
View File
@@ -10,7 +10,7 @@ import (
"time"
)
// TODO Implement support for MacOS via pbcopy, Termux via termux-clipboard-set (in separate files)
// TODO Implement support for Termux via termux-clipboard-set (in separate file)
// CopyField copies a field from an entry to the clipboard
func CopyField(targetLocation string, field int, executableName string) {
@@ -61,12 +61,12 @@ func CopyField(targetLocation string, field int, executableName string) {
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
os.Exit(1)
}
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)
+1 -3
View File
@@ -10,8 +10,6 @@ import (
"time"
)
// TODO Avoid 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 int, executableName string) {
if isFile, _ := TargetIsFile(targetLocation, true); isFile {
@@ -49,12 +47,12 @@ func CopyField(targetLocation string, field int, executableName string) {
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
os.Exit(1)
}
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)
+1 -1
View File
@@ -13,7 +13,7 @@ import (
// DecryptGPG decrypts a GPG-encrypted file and returns the contents as a slice of (trimmed) strings
func DecryptGPG(targetLocation string) []string {
cmd := exec.Command("gpg", "--pinentry-mode", "loopback", "-q", "-d", targetLocation)
output, err := cmd.CombinedOutput()
output, err := cmd.Output()
if err != nil {
fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset)
os.Exit(1)