v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-21 01:50:36 -07:00

refactor: text output directly to clipboard command

This commit is contained in:
eeeXun 2023-04-08 14:43:22 +08:00
parent 8ddd42b431
commit fa95a43f01

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strings"
) )
func IndexOf(candidate string, arr []string) int { func IndexOf(candidate string, arr []string) int {
@ -21,25 +22,22 @@ func SetTermTitle(name string) {
} }
func CopyToClipboard(text string) { func CopyToClipboard(text string) {
var cmd *exec.Cmd
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
switch os.Getenv("XDG_SESSION_TYPE") { switch os.Getenv("XDG_SESSION_TYPE") {
case "x11": case "x11":
exec.Command("sh", "-c", cmd = exec.Command("xclip", "-selection", "clipboard")
fmt.Sprintf("echo -n '%s' | xclip -selection clipboard", text)).
Start()
case "wayland": case "wayland":
exec.Command("sh", "-c", cmd = exec.Command("wl-copy")
fmt.Sprintf("echo -n '%s' | wl-copy", text)).
Start()
} }
case "darwin": case "darwin":
exec.Command("sh", "-c", cmd = exec.Command("pbcopy")
fmt.Sprintf("echo -n '%s' | pbcopy", text)).
Start()
case "windows": case "windows":
exec.Command("cmd", "/c", cmd = exec.Command("clip")
fmt.Sprintf("echo %s | clip", text)).
Start()
} }
cmd.Stdin = strings.NewReader(text)
cmd.Start()
} }