-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/go_modules/golang.org/x/net-0.23.0
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package clipboard | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
// writes usinng the xclip command, might also | ||
// work on freebsd and netbsd | ||
func writeAll(text string) error { | ||
path, err := exec.LookPath("xclip") | ||
if err != nil { | ||
return fmt.Errorf("failed to find xclip: %w", err) | ||
} | ||
|
||
r, w, err := os.Pipe() | ||
if err != nil { | ||
return fmt.Errorf("failed to create xclip pipe: %w", err) | ||
} | ||
var perr error | ||
go func() { | ||
_, err := w.WriteString(text) | ||
if err != nil { | ||
perr = fmt.Errorf("failed to write to xclip: %w", err) | ||
} | ||
w.Close() // ignore err | ||
}() | ||
|
||
c := exec.Cmd{ | ||
Path: path, | ||
Args: []string{ | ||
"-i", | ||
"-selection", | ||
"clipboard", | ||
}, | ||
Stdin: r, | ||
Stdout: nil, | ||
Stderr: nil, | ||
} | ||
err = c.Run() | ||
if err != nil { | ||
return fmt.Errorf("failed to run xclip: %w", err) | ||
} | ||
if perr != nil { | ||
return fmt.Errorf("failed to write to xclip: %w", err) | ||
} | ||
|
||
return nil | ||
} |