diff --git a/core/copy.go b/core/copy.go index 05743a3..869f9b1 100644 --- a/core/copy.go +++ b/core/copy.go @@ -12,7 +12,7 @@ import ( ) // 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 { 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") - 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() - copyField("", GenTOTP(secret, currentTime, forSteam)) + copyString(true, GenTOTP(secret, currentTime, forSteam)) // sleep until next 30-second interval 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 - copyField(executableName, copySubject) + copyString(false, copySubject) } } diff --git a/core/copyDARWIN.go b/core/copyDARWIN.go index d239070..300d27d 100644 --- a/core/copyDARWIN.go +++ b/core/copyDARWIN.go @@ -10,8 +10,8 @@ import ( "time" ) -// copyField copies a field from an entry to the clipboard. -func copyField(executableName, copySubject string) { +// copyString copies a string to the clipboard. +func copyString(continuous bool, copySubject string) { cmd := exec.Command("pbcopy") writeToStdin(cmd, copySubject) err := cmd.Run() @@ -20,16 +20,8 @@ func copyField(executableName, copySubject string) { os.Exit(ErrorClipboard) } - // launch clipboard clearing process if executableName is provided - if executableName != "" { - 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 + if !continuous { + launchClipClear(copySubject) } } @@ -53,5 +45,5 @@ func clipClear(oldContents string) { 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) } diff --git a/core/copyTERMUX.go b/core/copyTERMUX.go index e513817..ade3c35 100644 --- a/core/copyTERMUX.go +++ b/core/copyTERMUX.go @@ -10,8 +10,8 @@ import ( "time" ) -// copyField copies a field from an entry to the clipboard. -func copyField(executableName, copySubject string) { +// copyString copies a string to the clipboard. +func copyString(continuous bool, copySubject string) { cmd := exec.Command("termux-clipboard-set") writeToStdin(cmd, copySubject) err := cmd.Run() @@ -20,16 +20,8 @@ func copyField(executableName, copySubject string) { os.Exit(ErrorClipboard) } - // launch clipboard clearing process if executableName is provided - if executableName != "" { - 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 + if !continuous { + launchClipClear(copySubject) } } @@ -53,5 +45,5 @@ func clipClear(oldContents string) { 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) } diff --git a/core/copyUNIXGeneric.go b/core/copyUNIXGeneric.go index 947ff13..28dac11 100644 --- a/core/copyUNIXGeneric.go +++ b/core/copyUNIXGeneric.go @@ -10,8 +10,8 @@ import ( "time" ) -// copyField copies a field from an entry to the clipboard. -func copyField(executableName, copySubject string) { +// copyString copies a string to the clipboard. +func copyString(continuous bool, copySubject string) { var envSet bool // track whether environment variables are set var cmd *exec.Cmd // determine whether to use wl-copy (Wayland) or xclip (X11) @@ -31,16 +31,8 @@ func copyField(executableName, copySubject string) { os.Exit(ErrorClipboard) } - // launch clipboard clearing process if executableName is provided - if executableName != "" { - 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 + if !continuous { + launchClipClear(copySubject) } } @@ -77,5 +69,5 @@ func clipClear(oldContents string) { 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) } diff --git a/core/copyWIN.go b/core/copyWIN.go index f2f69d1..67fa84d 100644 --- a/core/copyWIN.go +++ b/core/copyWIN.go @@ -10,8 +10,8 @@ import ( "time" ) -// copyField copies a field from an entry to the clipboard. -func copyField(executableName, copySubject string) { +// copyString copies a string to the clipboard. +func copyString(continuous bool, copySubject string) { cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''"))) err := cmd.Run() if err != nil { @@ -19,16 +19,8 @@ func copyField(executableName, copySubject string) { os.Exit(ErrorClipboard) } - // launch clipboard clearing process if executableName is provided - if executableName != "" { - 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 + if !continuous { + launchClipClear(copySubject) } } @@ -51,5 +43,5 @@ func clipClear(oldContents string) { 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) } diff --git a/core/launchClipClearProcessCLI.go b/core/launchClipClearProcessCLI.go new file mode 100644 index 0000000..eb397fe --- /dev/null +++ b/core/launchClipClearProcessCLI.go @@ -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 +} diff --git a/core/launchClipClearProcessGUI.go b/core/launchClipClearProcessGUI.go new file mode 100644 index 0000000..4d0a029 --- /dev/null +++ b/core/launchClipClearProcessGUI.go @@ -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) +}