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.

167 lines
3.2 KiB
Go

package clipboard
import (
"errors"
"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
secondarySize int
primaryOriType string
primaryType FileType
primaryData []byte
primarySize int
hash string
}
func Set(types FileType, data []byte) error {
switch types {
case Text:
return AutoSetter("CF_UNICODETEXT", data)
case File:
return AutoSetter("CF_HDROP", data)
case Image:
return AutoSetter("PNG", data)
case HTML:
return AutoSetter("HTML Format", data)
}
return errors.New("not support type:" + string(types))
}
func SetOrigin(types string, data []byte) error {
return AutoSetter(types, data)
}
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
}