clipboard/def.go

159 lines
3.0 KiB
Go
Raw Permalink Normal View History

2025-11-10 10:17:06 +08:00
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
platform string
date time.Time
secondaryOriType string
secondaryType FileType
secondaryData []byte
secondarySize int
primaryOriType string
primaryType FileType
primaryData []byte
primarySize int
hash string
}
// format represents the format of clipboard data.
type format int
// All sorts of supported clipboard data
const (
// FmtText indicates plain text clipboard format
fmtText format = iota
// FmtImage indicates image/png clipboard format
fmtImage
)
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) TextSize() int {
if c.primaryType == Text {
return c.primarySize
}
if c.secondaryType == Text {
return c.secondarySize
}
return 0
}
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) IsFile() bool {
return c.primaryType == File || c.secondaryType == File
}
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) ImageSize() int {
if c.primaryType == Image {
return c.primarySize
}
if c.secondaryType == Image {
return c.secondarySize
}
return 0
}
func (c *Clipboard) IsImage() bool {
return c.primaryType == Image || c.secondaryType == Image
}
func (c *Clipboard) PrimaryTypeSize() int {
return c.primarySize
}
func (c *Clipboard) SecondaryTypeSize() int {
return c.secondarySize
}