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:
parent
5b4e4f032d
commit
7a526d9588
@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"kitty/tools/tui/loop"
|
"kitty/tools/tui/loop"
|
||||||
@ -17,11 +18,24 @@ import (
|
|||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
|
||||||
type Input struct {
|
type Input struct {
|
||||||
src *os.File
|
src *os.File
|
||||||
arg string
|
arg string
|
||||||
ext string
|
ext string
|
||||||
is_stream bool
|
is_stream bool
|
||||||
mime_type string
|
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) {
|
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 {
|
if aerr != nil {
|
||||||
return aerr
|
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 {
|
make_metadata := func(ptype, mime string) map[string]string {
|
||||||
ans := map[string]string{"type": ptype}
|
ans := map[string]string{"type": ptype}
|
||||||
@ -63,8 +98,8 @@ func write_loop(inputs []*Input, opts *Options) (err error) {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
if len(aliases[i.mime_type]) > 0 {
|
if len(i.extra_mime_types) > 0 {
|
||||||
lp.QueueWriteString(encode(make_metadata("walias", i.mime_type), strings.Join(aliases[i.mime_type], " ")))
|
lp.QueueWriteString(encode(make_metadata("walias", i.mime_type), strings.Join(i.extra_mime_types, " ")))
|
||||||
}
|
}
|
||||||
inputs = inputs[1:]
|
inputs = inputs[1:]
|
||||||
if len(inputs) == 0 {
|
if len(inputs) == 0 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user