mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-23 18:54:21 +01:00
paste images with ctrl+v (#26)
This commit is contained in:
49
internal/tui/image/clipboard_unix.go
Normal file
49
internal/tui/image/clipboard_unix.go
Normal file
@@ -0,0 +1,49 @@
|
||||
//go:build !windows
|
||||
|
||||
package image
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"github.com/atotto/clipboard"
|
||||
)
|
||||
|
||||
func GetImageFromClipboard() ([]byte, string, error) {
|
||||
text, err := clipboard.ReadAll()
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("Error reading clipboard")
|
||||
}
|
||||
|
||||
if text == "" {
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
binaryData := []byte(text)
|
||||
imageBytes, err := binaryToImage(binaryData)
|
||||
if err != nil {
|
||||
return nil, text, nil
|
||||
}
|
||||
return imageBytes, "", nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
func binaryToImage(data []byte) ([]byte, error) {
|
||||
reader := bytes.NewReader(data)
|
||||
img, _, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to covert bytes to image")
|
||||
}
|
||||
|
||||
return ImageToBytes(img)
|
||||
}
|
||||
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user