From 41fb3c79c5462b18b6b118aeee2dd60d50c45928 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 24 Jan 2023 18:26:01 +0530 Subject: [PATCH] DRYer --- tools/cmd/clipboard/read.go | 2 +- tools/cmd/clipboard/write.go | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tools/cmd/clipboard/read.go b/tools/cmd/clipboard/read.go index a79e99351..2014b5c85 100644 --- a/tools/cmd/clipboard/read.go +++ b/tools/cmd/clipboard/read.go @@ -171,7 +171,7 @@ func (self *Output) assign_mime_type(available_mimes []string, aliases map[strin } } } - if strings.HasPrefix(self.mime_type, "text/") { + if is_textual_mime(self.mime_type) { for _, mt := range available_mimes { if mt == "text/plain" { self.remote_mime_type = mt diff --git a/tools/cmd/clipboard/write.go b/tools/cmd/clipboard/write.go index 72f0b1473..a3354bd62 100644 --- a/tools/cmd/clipboard/write.go +++ b/tools/cmd/clipboard/write.go @@ -8,7 +8,6 @@ import ( "io" "os" "path/filepath" - "regexp" "strings" "kitty/tools/tui/loop" @@ -26,12 +25,20 @@ type Input struct { extra_mime_types []string } -func (self *Input) has_mime_matching(pat *regexp.Regexp) bool { - if pat.MatchString(self.mime_type) { +func is_textual_mime(x string) bool { + return strings.HasPrefix(x, "text/") +} + +func is_text_plain_mime(x string) bool { + return x == "text/plain" +} + +func (self *Input) has_mime_matching(predicate func(string) bool) bool { + if predicate(self.mime_type) { return true } for _, i := range self.extra_mime_types { - if pat.MatchString(i) { + if predicate(i) { return true } } @@ -51,20 +58,18 @@ func write_loop(inputs []*Input, opts *Options) (err error) { } 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) { + if i.has_mime_matching(is_textual_mime) { num_text_mimes++ - if !has_text_plain && i.has_mime_matching(text_plain_pat) { + if !has_text_plain && i.has_mime_matching(is_text_plain_mime) { has_text_plain = true } } } if num_text_mimes > 0 && !has_text_plain { for _, i := range inputs { - if i.has_mime_matching(text_pat) { + if i.has_mime_matching(is_textual_mime) { i.extra_mime_types = append(i.extra_mime_types, "text/plain") break }