mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
join plain text and rich text to one struct.
This commit is contained in:
@ -5,8 +5,37 @@ import (
|
||||
"bytes"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardGetText() (*types.ClipboardText, error) {
|
||||
text, err := manager.ClipboardGetBinary("STRING")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Rich text must not always be available, can fail silently.
|
||||
html, _ := manager.ClipboardGetBinary("text/html")
|
||||
|
||||
return &types.ClipboardText{
|
||||
Text: string(text),
|
||||
HTML: string(html),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardSetText(data types.ClipboardText) error {
|
||||
// TODO: Refactor.
|
||||
// Current implementation is unable to set multiple targets. HTML
|
||||
// is set, if available. Otherwise plain text.
|
||||
|
||||
if data.HTML != "" {
|
||||
return manager.ClipboardSetBinary("text/html", []byte(data.HTML))
|
||||
}
|
||||
|
||||
return manager.ClipboardSetBinary("STRING", []byte(data.Text))
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", mime)
|
||||
|
||||
@ -77,21 +106,3 @@ func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) {
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardGetPlainText() (string, error) {
|
||||
bytes, err := manager.ClipboardGetBinary("STRING")
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardSetPlainText(data string) error {
|
||||
return manager.ClipboardSetBinary("STRING", []byte(data))
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardGetRichText() (string, error) {
|
||||
bytes, err := manager.ClipboardGetBinary("text/html")
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardSetRichText(data string) error {
|
||||
return manager.ClipboardSetBinary("text/html", []byte(data))
|
||||
}
|
||||
|
Reference in New Issue
Block a user