xgbsel/data.go

79 lines
2.4 KiB
Go
Raw Normal View History

2023-06-06 11:41:30 -06:00
package xgbsel
import "io"
import "strings"
// Data represents a polymorphic data structure
type Data interface {
Convert (Target) (reader io.ReadSeekCloser, ok bool)
Supported () []Target
}
// Target represents an X selection target. It defines the type of data stored
// within an X selection. This data may be a mime type, or a more specific name
// that is unique to X. A list of these names can be found here:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2
type Target string
type Confidence int; const (
ConfidenceNone Confidence = iota
ConfidencePartial
ConfidenceFull
)
// ToMime converts the specified target to a MIME type. Because a single MIME
// type may correspond to several targets, a confidence value is returned
// representing how good of a match it is. If data is represented by multiple
// targets, they can be checked one after the other and the one with the highest
// confidence value chosen.
func (target Target) ToMime () (string, Confidence) {
// TODO: add other stuff. reference this table:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2
// perhaps we should also have parameters for mime types so we can
// return an encoding here for things like STRING?
switch target {
case "ADOBE_PORTABLE_DOCUMENT_FORMAT":
return "application/pdf", ConfidenceFull
case "APPLE_PICT":
return "image/pict", ConfidenceFull
case
"POSTSCRIPT",
"ENCAPSULATED_POSTSCRIPT",
"ENCAPSULATED_POSTSCRIPT_INTERCHANGE":
return "application/postscript", ConfidenceFull
case "FILE_NAME":
return "text/uri-list", ConfidenceFull
case "UTF8_STRING":
return "text/plain", ConfidenceFull
case "TEXT":
return "text/plain", ConfidencePartial
case "STRING":
return "text/plain", ConfidencePartial
default:
if strings.Count(string(target), "/") == 1 {
return string(target), ConfidenceFull
} else {
return "", ConfidenceNone
}
}
}
// MimeToTargets returns a list of targets that correspond to a specified MIME
// type.
func MimeToTargets (mime string) []Target {
targets := []Target { Target(mime) }
switch mime {
case "application/pdf":
targets = append(targets, "ADOBE_PORTABLE_DOCUMENT_FORMAT")
case "image/pict":
targets = append(targets, "APPLE_PICT")
case "application/postscript":
targets = append(targets, "POSTSCRIPT")
case "text/uri-list":
targets = append(targets, "FILE_NAME")
case "text/plain":
targets = append(targets, "UTF8_STRING", "TEXT", "STRING")
}
return targets
}