feat: native clipboard image sync (Linux, opt-in sync_images)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -70,3 +71,37 @@ func (c *linuxClipboard) Write(text string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadImage returns the PNG image from the clipboard, if any.
|
||||
func (c *linuxClipboard) ReadImage() (string, []byte, error) {
|
||||
var args []string
|
||||
if c.pasteCmd == "wl-paste" {
|
||||
args = []string{"--type", "image/png"}
|
||||
} else {
|
||||
args = []string{"-selection", "clipboard", "-t", "image/png", "-o"}
|
||||
}
|
||||
|
||||
cmd := exec.Command(c.pasteCmd, args...)
|
||||
out, err := cmd.Output()
|
||||
if err != nil || len(out) == 0 {
|
||||
return "", nil, ErrNoImage
|
||||
}
|
||||
return "image/png", out, nil
|
||||
}
|
||||
|
||||
// WriteImage writes image bytes to the clipboard using the given MIME type.
|
||||
func (c *linuxClipboard) WriteImage(mime string, data []byte) error {
|
||||
var args []string
|
||||
if c.copyCmd == "wl-copy" {
|
||||
args = []string{"--type", mime}
|
||||
} else {
|
||||
args = []string{"-selection", "clipboard", "-t", mime}
|
||||
}
|
||||
|
||||
cmd := exec.Command(c.copyCmd, args...)
|
||||
cmd.Stdin = bytes.NewReader(data)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("clipboard write image (%s): %w", c.copyCmd, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user