Implement unique clipboard clearing behavior for interactive GUI/TUI implementations

This commit is contained in:
2024-12-20 20:29:16 -05:00
parent f2f9c523f4
commit 619e4220a6
7 changed files with 56 additions and 56 deletions
+4 -4
View File
@@ -12,7 +12,7 @@ import (
) )
// CopyArgument copies a field from an entry to the clipboard. // CopyArgument copies a field from an entry to the clipboard.
func CopyArgument(executableName, targetLocation string, field int) { func CopyArgument(targetLocation string, field int) {
if isFile, _ := TargetIsFile(targetLocation, true, 2); isFile { if isFile, _ := TargetIsFile(targetLocation, true, 2); isFile {
decryptedEntry := DecryptGPG(targetLocation) decryptedEntry := DecryptGPG(targetLocation)
@@ -42,9 +42,9 @@ func CopyArgument(executableName, targetLocation string, field int) {
fmt.Println("Clipboard will be kept up to date with the current TOTP code until this process is closed") fmt.Println("Clipboard will be kept up to date with the current TOTP code until this process is closed")
for { // keep field copied to clipboard, refresh on 30-second intervals for { // keep token copied to clipboard, refresh on 30-second intervals
currentTime := time.Now() currentTime := time.Now()
copyField("", GenTOTP(secret, currentTime, forSteam)) copyString(true, GenTOTP(secret, currentTime, forSteam))
// sleep until next 30-second interval // sleep until next 30-second interval
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second) time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
} }
@@ -55,7 +55,7 @@ func CopyArgument(executableName, targetLocation string, field int) {
} }
// copy field to clipboard, launch clipboard clearing process // copy field to clipboard, launch clipboard clearing process
copyField(executableName, copySubject) copyString(false, copySubject)
} }
} }
+5 -13
View File
@@ -10,8 +10,8 @@ import (
"time" "time"
) )
// copyField copies a field from an entry to the clipboard. // copyString copies a string to the clipboard.
func copyField(executableName, copySubject string) { func copyString(continuous bool, copySubject string) {
cmd := exec.Command("pbcopy") cmd := exec.Command("pbcopy")
writeToStdin(cmd, copySubject) writeToStdin(cmd, copySubject)
err := cmd.Run() err := cmd.Run()
@@ -20,16 +20,8 @@ func copyField(executableName, copySubject string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
// launch clipboard clearing process if executableName is provided if !continuous {
if executableName != "" { launchClipClear(copySubject)
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(ErrorClipboard)
}
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
} }
} }
@@ -53,5 +45,5 @@ func clipClear(oldContents string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
} }
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) Exit(0)
} }
+5 -13
View File
@@ -10,8 +10,8 @@ import (
"time" "time"
) )
// copyField copies a field from an entry to the clipboard. // copyString copies a string to the clipboard.
func copyField(executableName, copySubject string) { func copyString(continuous bool, copySubject string) {
cmd := exec.Command("termux-clipboard-set") cmd := exec.Command("termux-clipboard-set")
writeToStdin(cmd, copySubject) writeToStdin(cmd, copySubject)
err := cmd.Run() err := cmd.Run()
@@ -20,16 +20,8 @@ func copyField(executableName, copySubject string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
// launch clipboard clearing process if executableName is provided if !continuous {
if executableName != "" { launchClipClear(copySubject)
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(ErrorClipboard)
}
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
} }
} }
@@ -53,5 +45,5 @@ func clipClear(oldContents string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
} }
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) Exit(0)
} }
+5 -13
View File
@@ -10,8 +10,8 @@ import (
"time" "time"
) )
// copyField copies a field from an entry to the clipboard. // copyString copies a string to the clipboard.
func copyField(executableName, copySubject string) { func copyString(continuous bool, copySubject string) {
var envSet bool // track whether environment variables are set var envSet bool // track whether environment variables are set
var cmd *exec.Cmd var cmd *exec.Cmd
// determine whether to use wl-copy (Wayland) or xclip (X11) // determine whether to use wl-copy (Wayland) or xclip (X11)
@@ -31,16 +31,8 @@ func copyField(executableName, copySubject string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
// launch clipboard clearing process if executableName is provided if !continuous {
if executableName != "" { launchClipClear(copySubject)
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(ErrorClipboard)
}
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
} }
} }
@@ -77,5 +69,5 @@ func clipClear(oldContents string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
} }
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) Exit(0)
} }
+5 -13
View File
@@ -10,8 +10,8 @@ import (
"time" "time"
) )
// copyField copies a field from an entry to the clipboard. // copyString copies a string to the clipboard.
func copyField(executableName, copySubject string) { func copyString(continuous bool, copySubject string) {
cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''"))) cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''")))
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
@@ -19,16 +19,8 @@ func copyField(executableName, copySubject string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
// launch clipboard clearing process if executableName is provided if !continuous {
if executableName != "" { launchClipClear(copySubject)
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(ErrorClipboard)
}
Exit(0) // only exit if clipboard clearing process is launched, otherwise assume continuous clipboard refresh
} }
} }
@@ -51,5 +43,5 @@ func clipClear(oldContents string) {
os.Exit(ErrorClipboard) os.Exit(ErrorClipboard)
} }
} }
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment) Exit(0)
} }
+23
View File
@@ -0,0 +1,23 @@
//go:build !returnOnExit
package core
import (
"fmt"
"os"
"os/exec"
)
// launchClipClear launches the automated clipboard clearing process.
// For non-interactive CLI implementations, an entirely separate process is created for this purpose.
func launchClipClear(copySubject string) {
executableName := os.Args[0]
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(ErrorClipboard)
}
os.Exit(0) // use os.Exit directly since this version of this function is only meant for non-interactive CLI implementations
}
+9
View File
@@ -0,0 +1,9 @@
//go:build returnOnExit
package core
// launchClipClear launches the automated clipboard clearing process.
// For interactive GUI/TUI implementations, the clipboard clearing process is launched as a goroutine.
func launchClipClear(copySubject string) {
go clipClear(copySubject)
}