Use clip.exe for setting/clearing clipboard on Windows to avoid Powershell startup latency

This commit is contained in:
2026-01-10 19:08:43 -05:00
parent 07ad0a1bd9
commit 2054f5b3cf
+7 -4
View File
@@ -4,14 +4,15 @@ package clip
import ( import (
"errors" "errors"
"fmt"
"os/exec" "os/exec"
"strings"
"github.com/rwinkhart/go-boilerplate/back"
) )
// CopyString copies a string to the clipboard. // CopyString copies a string to the clipboard.
func CopyString(clearClipboardAutomatically bool, copySubject string) error { func CopyString(clearClipboardAutomatically bool, copySubject string) error {
cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''"))) cmd := exec.Command("clip.exe")
_ = back.WriteToStdin(cmd, copySubject)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return errors.New("unable to copy to clipboard: " + err.Error()) return errors.New("unable to copy to clipboard: " + err.Error())
} }
@@ -23,5 +24,7 @@ func CopyString(clearClipboardAutomatically bool, copySubject string) error {
// getClipCommands returns the commands for pasting and clearing the clipboard contents. // getClipCommands returns the commands for pasting and clearing the clipboard contents.
func getClipCommands() (*exec.Cmd, *exec.Cmd, error) { func getClipCommands() (*exec.Cmd, *exec.Cmd, error) {
return exec.Command("powershell.exe", "-c", "Get-Clipboard"), exec.Command("powershell.exe", "-c", "Set-Clipboard"), nil clearCMD := exec.Command("clip.exe")
_ = back.WriteToStdin(clearCMD, "")
return exec.Command("powershell.exe", "-c", "Get-Clipboard"), clearCMD, nil
} }