Implement MIME aliases for clipboard

This commit is contained in:
Kovid Goyal
2022-12-03 12:20:03 +05:30
parent a622a149f6
commit 6422b323c6
4 changed files with 62 additions and 7 deletions

View File

@@ -139,14 +139,22 @@ func (self *Output) commit() {
self.dest = nil
}
func (self *Output) assign_mime_type(available_mimes []string) (err error) {
func (self *Output) assign_mime_type(available_mimes []string, aliases map[string][]string) (err error) {
if self.mime_type == "." {
self.remote_mime_type = "."
return
}
if utils.Contains(available_mimes, self.mime_type) {
self.remote_mime_type = self.mime_type
return
}
if self.mime_type == "." {
self.remote_mime_type = "."
return
if len(aliases[self.mime_type]) > 0 {
for _, alias := range aliases[self.mime_type] {
if utils.Contains(available_mimes, alias) {
self.remote_mime_type = alias
return
}
}
}
if images.EncodableImageTypes[self.mime_type] {
for _, mt := range available_mimes {
@@ -242,6 +250,19 @@ func parse_escape_code(etype loop.EscapeCodeType, data []byte) (metadata map[str
return
}
func parse_aliases(raw []string) (map[string][]string, error) {
ans := make(map[string][]string, len(raw))
for _, x := range raw {
k, v, found := utils.Cut(x, "=")
if !found {
return nil, fmt.Errorf("%s is not valid MIME alias specification", x)
}
ans[k] = append(ans[k], v)
ans[v] = append(ans[v], k)
}
return ans, nil
}
func run_get_loop(opts *Options, args []string) (err error) {
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking)
if err != nil {
@@ -253,6 +274,10 @@ func run_get_loop(opts *Options, args []string) (err error) {
requested_mimes := make(map[string]*Output)
reading_available_mimes := true
outputs := make([]*Output, len(args))
aliases, merr := parse_aliases(opts.Alias)
if merr != nil {
return merr
}
for i, arg := range args {
outputs[i] = &Output{arg: arg, arg_is_stream: arg == "/dev/stdout" || arg == "/dev/stderr", ext: filepath.Ext(arg)}
@@ -301,7 +326,7 @@ func run_get_loop(opts *Options, args []string) (err error) {
return fmt.Errorf("The clipboard is empty")
}
for _, o := range outputs {
err = o.assign_mime_type(available_mimes)
err = o.assign_mime_type(available_mimes, aliases)
if err != nil {
return err
}

View File

@@ -8,6 +8,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"kitty/tools/tui/loop"
"kitty/tools/utils"
@@ -23,13 +24,17 @@ type Input struct {
mime_type string
}
func write_loop(inputs []*Input) (err error) {
func write_loop(inputs []*Input, opts *Options) (err error) {
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking)
if err != nil {
return err
}
var waiting_for_write loop.IdType
var buf [4096]byte
aliases, aerr := parse_aliases(opts.Alias)
if aerr != nil {
return aerr
}
lp.OnInitialize = func() (string, error) {
waiting_for_write = lp.QueueWriteString(encode(map[string]string{"type": "write"}, ""))
@@ -47,6 +52,9 @@ func write_loop(inputs []*Input) (err error) {
}
if err != nil {
if errors.Is(err, io.EOF) {
if len(aliases[i.mime_type]) > 0 {
lp.QueueWriteString(encode(map[string]string{"type": "walias", "mime": i.mime_type}, strings.Join(aliases[i.mime_type], " ")))
}
inputs = inputs[1:]
if len(inputs) == 0 {
lp.QueueWriteString(encode(map[string]string{"type": "wdata"}, ""))
@@ -151,5 +159,5 @@ func run_set_loop(opts *Options, args []string) (err error) {
}
to_process[i] = inputs[i]
}
return write_loop(to_process)
return write_loop(to_process, opts)
}