mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Improve versatility of getClipCommands()
This commit is contained in:
@@ -13,7 +13,10 @@ import (
|
|||||||
// ClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed.
|
// ClearProcess clears the clipboard after 30 seconds if the clipboard contents have not changed.
|
||||||
// assignedContents can be omitted to clear the clipboard immediately and unconditionally.
|
// assignedContents can be omitted to clear the clipboard immediately and unconditionally.
|
||||||
func ClearProcess(assignedContents string) error {
|
func ClearProcess(assignedContents string) error {
|
||||||
cmdPaste, cmdClear := getClipCommands()
|
cmdPaste, cmdClear, err := getClipCommands()
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("unable to determine clipboard platform: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
clearClipboard := func() error {
|
clearClipboard := func() error {
|
||||||
err := cmdClear.Run()
|
err := cmdClear.Run()
|
||||||
|
|||||||
+28
-12
@@ -13,32 +13,48 @@ import (
|
|||||||
// 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 {
|
||||||
// determine whether to use wl-copy (Wayland) or xclip (X11)
|
// determine whether to use wl-copy (Wayland) or xclip (X11)
|
||||||
var envSet, isWayland bool // track whether environment variables are set
|
sessionIsWayland, err := isWayland()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var cmdCopy *exec.Cmd
|
var cmdCopy *exec.Cmd
|
||||||
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
if sessionIsWayland {
|
||||||
cmdCopy = exec.Command("wl-copy", "-t", "text/plain")
|
cmdCopy = exec.Command("wl-copy", "-t", "text/plain")
|
||||||
isWayland = true
|
|
||||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
|
||||||
cmdCopy = exec.Command("xclip", "-sel", "c", "-t", "text/plain")
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("clipboard platform could not be determined")
|
cmdCopy = exec.Command("xclip", "-sel", "c", "-t", "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = back.WriteToStdin(cmdCopy, copySubject)
|
_ = back.WriteToStdin(cmdCopy, copySubject)
|
||||||
err := cmdCopy.Run()
|
err = cmdCopy.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||||
}
|
}
|
||||||
if clearClipboardAutomatically {
|
if clearClipboardAutomatically {
|
||||||
LaunchClipClearProcess(copySubject, isWayland)
|
LaunchClipClearProcess(copySubject, sessionIsWayland)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
func getClipCommands() (*exec.Cmd, *exec.Cmd, error) {
|
||||||
if os.Args[2] == "true" { // wayland
|
sessionIsWayland, err := isWayland()
|
||||||
return exec.Command("wl-paste"), exec.Command("wl-copy", "-c")
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if sessionIsWayland {
|
||||||
|
return exec.Command("wl-paste"), exec.Command("wl-copy", "-c"), nil
|
||||||
|
}
|
||||||
|
return exec.Command("xclip", "-o", "-sel", "c"), exec.Command("xclip", "-i", "/dev/null", "-sel", "c"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isWayland returns a boolean indicating whether the current session is Wayland.
|
||||||
|
func isWayland() (bool, error) {
|
||||||
|
var envSet bool // track whether environment variables are set
|
||||||
|
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
||||||
|
return true, nil
|
||||||
|
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||||
|
return false, nil
|
||||||
|
} else {
|
||||||
|
return false, errors.New("unable to detect Wayland or X11 session")
|
||||||
}
|
}
|
||||||
return exec.Command("xclip", "-o", "-sel", "c"), exec.Command("xclip", "-i", "/dev/null", "-sel", "c")
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user