mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-31 22:36:41 -04:00
Improve generic UNIX clipboard clearing logic
This commit is contained in:
@@ -48,26 +48,30 @@ func copyField(executableName, copySubject string) {
|
|||||||
func clipClear(oldContents string) {
|
func clipClear(oldContents string) {
|
||||||
time.Sleep(30 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
|
|
||||||
var envSet bool // track whether environment variables are set
|
// determine clipboard tool to use (wl-clipboard VS xclip)
|
||||||
var platform bool // track clipboard platform, false for Wayland, true for X11
|
var envSet bool
|
||||||
var cmd *exec.Cmd
|
var cmdClear, cmdPaste *exec.Cmd
|
||||||
// determine whether to use wl-copy (Wayland) or xclip (X11)
|
|
||||||
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
if _, envSet = os.LookupEnv("WAYLAND_DISPLAY"); envSet {
|
||||||
cmd = exec.Command("wl-paste")
|
cmdClear = exec.Command("wl-copy", "-c")
|
||||||
|
cmdPaste = exec.Command("wl-paste")
|
||||||
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
} else if _, envSet = os.LookupEnv("DISPLAY"); envSet {
|
||||||
cmd = exec.Command("xclip", "-o", "-sel", "c")
|
cmdClear = exec.Command("xclip", "-i", "/dev/null", "-sel", "c")
|
||||||
platform = true
|
cmdPaste = exec.Command("xclip", "-o", "-sel", "c")
|
||||||
|
} else {
|
||||||
|
fmt.Println(AnsiError + "Clipboard platform could not be determined - neither $WAYLAND_DISPLAY nor $DISPLAY are set" + AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
newContents, _ := cmd.Output()
|
|
||||||
|
|
||||||
|
// read current clipboard contents
|
||||||
|
newContents, err := cmdPaste.Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(AnsiError + "Failed to read clipboard contents: " + err.Error() + AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear clipboard if contents have not been modified
|
||||||
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
if oldContents == strings.TrimRight(string(newContents), "\r\n") {
|
||||||
switch platform {
|
err = cmdClear.Run()
|
||||||
case false:
|
|
||||||
cmd = exec.Command("wl-copy", "-c")
|
|
||||||
case true:
|
|
||||||
cmd = exec.Command("xclip", "-i", "/dev/null", "-sel", "c")
|
|
||||||
}
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset)
|
fmt.Println(AnsiError + "Failed to clear clipboard: " + err.Error() + AnsiReset)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user