You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.2 KiB
Go
113 lines
2.2 KiB
Go
package clipboard
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type FileType string
|
|
|
|
const (
|
|
Text FileType = "text"
|
|
File FileType = "file"
|
|
Image FileType = "image"
|
|
HTML FileType = "html"
|
|
)
|
|
|
|
type Clipboard struct {
|
|
winOriginTypes []string
|
|
plateform string
|
|
date time.Time
|
|
secondaryOriType string
|
|
secondaryType FileType
|
|
secondaryData []byte
|
|
|
|
primaryOriType string
|
|
primaryType FileType
|
|
primaryData []byte
|
|
hash string
|
|
}
|
|
|
|
func (c *Clipboard) PrimaryType() FileType {
|
|
return c.primaryType
|
|
}
|
|
|
|
func (c *Clipboard) AvailableTypes() []FileType {
|
|
var res = make([]FileType, 0, 2)
|
|
if c.primaryType != "" {
|
|
res = append(res, c.primaryType)
|
|
}
|
|
if c.secondaryType != "" {
|
|
res = append(res, c.secondaryType)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (c *Clipboard) IsText() bool {
|
|
return c.primaryType == Text || c.secondaryType == Text
|
|
}
|
|
|
|
func (c *Clipboard) Text() string {
|
|
if c.primaryType == Text {
|
|
return string(c.primaryData)
|
|
}
|
|
if c.secondaryType == Text {
|
|
return string(c.secondaryData)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c *Clipboard) IsHTML() bool {
|
|
return (c.primaryType == HTML || c.secondaryType == HTML) || c.IsText()
|
|
}
|
|
|
|
func (c *Clipboard) HTML() string {
|
|
var htmlBytes []byte
|
|
if c.primaryType == HTML {
|
|
htmlBytes = c.primaryData
|
|
} else if c.secondaryType == HTML {
|
|
htmlBytes = c.secondaryData
|
|
} else {
|
|
return c.Text()
|
|
}
|
|
formats := strings.SplitN(string(htmlBytes), "\n", 7)
|
|
if len(formats) < 7 {
|
|
return string(htmlBytes)
|
|
}
|
|
return formats[6]
|
|
}
|
|
|
|
func (c *Clipboard) FilePaths() []string {
|
|
if c.primaryType == File {
|
|
return strings.Split(string(c.primaryData), "|")
|
|
}
|
|
if c.secondaryType == File {
|
|
return strings.Split(string(c.secondaryData), "|")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Clipboard) FirstFilePath() string {
|
|
if c.primaryType == File {
|
|
return strings.Split(string(c.primaryData), "|")[0]
|
|
}
|
|
if c.secondaryType == File {
|
|
return strings.Split(string(c.secondaryData), "|")[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c *Clipboard) Image() []byte {
|
|
if c.primaryType == Image {
|
|
return c.primaryData
|
|
}
|
|
if c.secondaryType == Image {
|
|
return c.secondaryData
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Clipboard) IsImage() bool {
|
|
return c.primaryType == Image || c.secondaryType == Image
|
|
}
|