Allow clearing the clipboard instantly; improve code sharing between platforms; facilitate GUI/TUI client development regarding the clipboard

This commit is contained in:
2024-12-21 15:31:14 -05:00
parent 9ed7b8ad9b
commit 3b57d59ddc
10 changed files with 117 additions and 128 deletions
+7 -25
View File
@@ -1,4 +1,4 @@
//go:build linux && termux
//go:build android && termux
package core
@@ -6,8 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
// copyString copies a string to the clipboard.
@@ -21,29 +19,13 @@ func copyString(continuous bool, copySubject string) {
}
if !continuous {
launchClipClear(copySubject)
launchClipClearProcess(copySubject)
}
}
// 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("termux-clipboard-get")
newContents, err := cmd.Output()
if err != nil {
fmt.Println(AnsiError+"Failed to read clipboard contents:", err.Error()+AnsiReset)
os.Exit(ErrorClipboard)
}
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
cmd = exec.Command("termux-clipboard-set")
writeToStdin(cmd, "")
err = cmd.Run()
if err != nil {
fmt.Println(AnsiError+"Failed to clear clipboard:", err.Error()+AnsiReset)
os.Exit(ErrorClipboard)
}
}
Exit(0)
// getClipCommands returns the commands for pasting and clearing the clipboard contents.
func getClipCommands() (*exec.Cmd, *exec.Cmd) {
cmdClear := exec.Command("termux-clipboard-set")
writeToStdin(cmdClear, "")
return exec.Command("termux-clipboard-get"), cmdClear
}