clipboard kitten: When copying, automatically add a text/plain alias if there is at least one text/* MIME and no actual text/plain MIME

This commit is contained in:
Kovid Goyal 2023-01-22 20:41:37 +05:30
parent 5b4e4f032d
commit 7a526d9588
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -8,6 +8,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
"kitty/tools/tui/loop"
@ -17,11 +18,24 @@ import (
var _ = fmt.Print
type Input struct {
src *os.File
arg string
ext string
is_stream bool
mime_type string
src *os.File
arg string
ext string
is_stream bool
mime_type string
extra_mime_types []string
}
func (self *Input) has_mime_matching(pat *regexp.Regexp) bool {
if pat.MatchString(self.mime_type) {
return true
}
for _, i := range self.extra_mime_types {
if pat.MatchString(i) {
return true
}
}
return false
}
func write_loop(inputs []*Input, opts *Options) (err error) {
@ -35,6 +49,27 @@ func write_loop(inputs []*Input, opts *Options) (err error) {
if aerr != nil {
return aerr
}
num_text_mimes := 0
has_text_plain := false
text_pat := regexp.MustCompile("text/.+")
text_plain_pat := regexp.MustCompile("text/plain")
for _, i := range inputs {
i.extra_mime_types = aliases[i.mime_type]
if i.has_mime_matching(text_pat) {
num_text_mimes++
if !has_text_plain && i.has_mime_matching(text_plain_pat) {
has_text_plain = true
}
}
}
if num_text_mimes > 0 && !has_text_plain {
for _, i := range inputs {
if i.has_mime_matching(text_pat) {
i.extra_mime_types = append(i.extra_mime_types, "text/plain")
break
}
}
}
make_metadata := func(ptype, mime string) map[string]string {
ans := map[string]string{"type": ptype}
@ -63,8 +98,8 @@ func write_loop(inputs []*Input, opts *Options) (err error) {
}
if err != nil {
if errors.Is(err, io.EOF) {
if len(aliases[i.mime_type]) > 0 {
lp.QueueWriteString(encode(make_metadata("walias", i.mime_type), strings.Join(aliases[i.mime_type], " ")))
if len(i.extra_mime_types) > 0 {
lp.QueueWriteString(encode(make_metadata("walias", i.mime_type), strings.Join(i.extra_mime_types, " ")))
}
inputs = inputs[1:]
if len(inputs) == 0 {