From 405f5ce148a3f6adea0ffbf0af87824813cb5e34 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 4 May 2024 14:34:40 +0530 Subject: [PATCH] Use stdlib maps/slices --- kittens/clipboard/read.go | 6 ++---- kittens/diff/search.go | 3 +-- kittens/hints/marks.go | 2 +- kittens/ssh/main.go | 6 +++--- kittens/themes/ui.go | 5 ++--- kittens/transfer/receive.go | 2 +- kittens/transfer/send.go | 2 +- kittens/unicode_input/main.go | 3 +-- kittens/unicode_input/table.go | 3 +-- tools/cli/fish.go | 4 +--- tools/cli/help.go | 2 +- tools/cli/option.go | 3 +-- tools/cmd/benchmark/main.go | 2 +- tools/cmd/list_fonts/ui.go | 4 +--- tools/config/utils.go | 6 ++---- tools/rsync/algorithm.go | 2 +- tools/rsync/api_test.go | 2 +- tools/themes/collection.go | 10 +++++----- tools/tui/graphics/collection.go | 6 ++---- tools/tui/loop/timers.go | 3 +-- tools/tui/sgr/insert-formatting.go | 3 +-- tools/tui/shell_integration/api.go | 5 ++--- tools/tui/subseq/score.go | 3 +-- tools/unicode_names/query_test.go | 2 +- tools/utils/images/loading.go | 2 +- tools/utils/misc.go | 2 +- tools/utils/set.go | 22 ++++++++++++++++++---- 27 files changed, 55 insertions(+), 60 deletions(-) diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index a78dce321..d5f8a5956 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -10,6 +10,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "sync" @@ -17,9 +18,6 @@ import ( "kitty/tools/tui/loop" "kitty/tools/utils" "kitty/tools/utils/images" - - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" ) var _ = fmt.Print @@ -365,7 +363,7 @@ func run_get_loop(opts *Options, args []string) (err error) { } } if len(requested_mimes) > 0 { - lp.QueueWriteString(encode(basic_metadata, strings.Join(maps.Keys(requested_mimes), " "))) + lp.QueueWriteString(encode(basic_metadata, strings.Join(utils.Keys(requested_mimes), " "))) } else { lp.Quit(0) } diff --git a/kittens/diff/search.go b/kittens/diff/search.go index cf7e4f3d7..49a8128ce 100644 --- a/kittens/diff/search.go +++ b/kittens/diff/search.go @@ -5,6 +5,7 @@ package diff import ( "fmt" "regexp" + "slices" "strings" "sync" @@ -12,8 +13,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/images" "kitty/tools/wcswidth" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/kittens/hints/marks.go b/kittens/hints/marks.go index 4d187817a..e302bef6c 100644 --- a/kittens/hints/marks.go +++ b/kittens/hints/marks.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" "regexp" + "slices" "strconv" "strings" "sync" @@ -18,7 +19,6 @@ import ( "github.com/dlclark/regexp2" "github.com/seancfoley/ipaddress-go/ipaddr" - "golang.org/x/exp/slices" "kitty" "kitty/tools/config" diff --git a/kittens/ssh/main.go b/kittens/ssh/main.go index edbb42af2..d0a485625 100644 --- a/kittens/ssh/main.go +++ b/kittens/ssh/main.go @@ -13,6 +13,7 @@ import ( "io" "io/fs" "kitty" + "maps" "net/url" "os" "os/exec" @@ -21,6 +22,7 @@ import ( "path" "path/filepath" "regexp" + "slices" "strconv" "strings" "time" @@ -36,8 +38,6 @@ import ( "kitty/tools/utils/shlex" "kitty/tools/utils/shm" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" "golang.org/x/sys/unix" ) @@ -411,7 +411,7 @@ func prepare_script(script string, replacements map[string]string) string { if _, found := replacements["EXPORT_HOME_CMD"]; !found { replacements["EXPORT_HOME_CMD"] = "" } - keys := maps.Keys(replacements) + keys := utils.Keys(replacements) for i, key := range keys { keys[i] = "\\b" + key + "\\b" } diff --git a/kittens/themes/ui.go b/kittens/themes/ui.go index ac740c8b0..f552e9862 100644 --- a/kittens/themes/ui.go +++ b/kittens/themes/ui.go @@ -5,8 +5,10 @@ package themes import ( "fmt" "io" + "maps" "path/filepath" "regexp" + "slices" "strings" "time" @@ -16,9 +18,6 @@ import ( "kitty/tools/tui/readline" "kitty/tools/utils" "kitty/tools/wcswidth" - - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/kittens/transfer/receive.go b/kittens/transfer/receive.go index cb0c7b279..2ccdc718b 100644 --- a/kittens/transfer/receive.go +++ b/kittens/transfer/receive.go @@ -11,6 +11,7 @@ import ( "io/fs" "os" "path/filepath" + "slices" "strconv" "strings" "time" @@ -25,7 +26,6 @@ import ( "kitty/tools/utils/humanize" "kitty/tools/wcswidth" - "golang.org/x/exp/slices" "golang.org/x/sys/unix" ) diff --git a/kittens/transfer/send.go b/kittens/transfer/send.go index b2373bd65..c9257599b 100644 --- a/kittens/transfer/send.go +++ b/kittens/transfer/send.go @@ -11,6 +11,7 @@ import ( "io/fs" "os" "path/filepath" + "slices" "strconv" "strings" "syscall" @@ -18,7 +19,6 @@ import ( "unicode/utf8" "golang.org/x/exp/constraints" - "golang.org/x/exp/slices" "kitty" "kitty/tools/cli/markup" diff --git a/kittens/unicode_input/main.go b/kittens/unicode_input/main.go index e864d510d..42cfe8f92 100644 --- a/kittens/unicode_input/main.go +++ b/kittens/unicode_input/main.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" "unicode" @@ -21,8 +22,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/style" "kitty/tools/wcswidth" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/kittens/unicode_input/table.go b/kittens/unicode_input/table.go index 904bc08f1..597e7db72 100644 --- a/kittens/unicode_input/table.go +++ b/kittens/unicode_input/table.go @@ -4,6 +4,7 @@ package unicode_input import ( "fmt" + "slices" "strconv" "strings" @@ -11,8 +12,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/style" "kitty/tools/wcswidth" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/cli/fish.go b/tools/cli/fish.go index 1c944f55f..c9068fcb8 100644 --- a/tools/cli/fish.go +++ b/tools/cli/fish.go @@ -8,8 +8,6 @@ import ( "kitty/tools/cli/markup" "kitty/tools/utils" - - "golang.org/x/exp/maps" ) var _ = fmt.Print @@ -23,7 +21,7 @@ func fish_completion_script(commands []string) (string, error) { "kitten": true, } if len(commands) == 0 { - commands = append(commands, maps.Keys(all_commands)...) + commands = append(commands, utils.Keys(all_commands)...) } script := strings.Builder{} script.WriteString(`function __ksi_completions diff --git a/tools/cli/help.go b/tools/cli/help.go index 26571ce64..4ecc43743 100644 --- a/tools/cli/help.go +++ b/tools/cli/help.go @@ -7,10 +7,10 @@ import ( "io" "os" "os/exec" + "slices" "strings" "time" - "golang.org/x/exp/slices" "golang.org/x/sys/unix" "kitty" diff --git a/tools/cli/option.go b/tools/cli/option.go index d30b45d97..848d00a75 100644 --- a/tools/cli/option.go +++ b/tools/cli/option.go @@ -4,10 +4,9 @@ package cli import ( "fmt" + "slices" "strconv" "strings" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/cmd/benchmark/main.go b/tools/cmd/benchmark/main.go index e135b53a5..8b7651680 100644 --- a/tools/cmd/benchmark/main.go +++ b/tools/cmd/benchmark/main.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "math/rand/v2" + "slices" "strings" "time" @@ -16,7 +17,6 @@ import ( "kitty/tools/tui/loop" "kitty/tools/utils" - "golang.org/x/exp/slices" "golang.org/x/sys/unix" ) diff --git a/tools/cmd/list_fonts/ui.go b/tools/cmd/list_fonts/ui.go index 5c014ccf0..15fa572ef 100644 --- a/tools/cmd/list_fonts/ui.go +++ b/tools/cmd/list_fonts/ui.go @@ -11,8 +11,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/style" "kitty/tools/wcswidth" - - "golang.org/x/exp/maps" ) var _ = fmt.Print @@ -277,7 +275,7 @@ func (h *handler) on_wakeup() (err error) { switch h.state { case SCANNING_FAMILIES: h.state = LISTING_FAMILIES - h.family_list.UpdateFamilies(utils.StableSortWithKey(maps.Keys(h.fonts), strings.ToLower)) + h.family_list.UpdateFamilies(utils.StableSortWithKey(utils.Keys(h.fonts), strings.ToLower)) case LISTING_FAMILIES: } return h.draw_screen() diff --git a/tools/config/utils.go b/tools/config/utils.go index 426aa583d..79d5ae350 100644 --- a/tools/config/utils.go +++ b/tools/config/utils.go @@ -7,12 +7,10 @@ import ( "kitty/tools/tui/loop" "kitty/tools/utils" "regexp" + "slices" "strconv" "strings" "sync" - - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" ) var _ = fmt.Print @@ -309,5 +307,5 @@ func ResolveShortcuts(actions []*KeyAction) []*KeyAction { action_map[key] = ac } } - return maps.Values(action_map) + return utils.Values(action_map) } diff --git a/tools/rsync/algorithm.go b/tools/rsync/algorithm.go index 48d52500e..e78308bb2 100644 --- a/tools/rsync/algorithm.go +++ b/tools/rsync/algorithm.go @@ -15,10 +15,10 @@ import ( "fmt" "hash" "io" + "slices" "strconv" "github.com/zeebo/xxh3" - "golang.org/x/exp/slices" ) // If no BlockSize is specified in the rsync instance, this value is used. diff --git a/tools/rsync/api_test.go b/tools/rsync/api_test.go index 2eae4a2c8..575092de7 100644 --- a/tools/rsync/api_test.go +++ b/tools/rsync/api_test.go @@ -7,12 +7,12 @@ import ( "encoding/hex" "fmt" "io" + "slices" "strconv" "strings" "testing" "github.com/google/go-cmp/cmp" - "golang.org/x/exp/slices" "kitty/tools/utils" ) diff --git a/tools/themes/collection.go b/tools/themes/collection.go index fb9dc4615..0bb5a88d9 100644 --- a/tools/themes/collection.go +++ b/tools/themes/collection.go @@ -10,11 +10,13 @@ import ( "fmt" "io" "io/fs" + "maps" "net/http" "os" "path" "path/filepath" "regexp" + "slices" "strconv" "strings" "sync" @@ -28,8 +30,6 @@ import ( "kitty/tools/utils/style" "github.com/shirou/gopsutil/v3/process" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" "golang.org/x/sys/unix" ) @@ -594,7 +594,7 @@ func patch_conf(text, theme_name string) string { } ntext = text + addition } - pat = utils.MustCompile(fmt.Sprintf(`(?m)^\s*(%s)\b`, strings.Join(maps.Keys(AllColorSettingNames), "|"))) + pat = utils.MustCompile(fmt.Sprintf(`(?m)^\s*(%s)\b`, strings.Join(utils.Keys(AllColorSettingNames), "|"))) return pat.ReplaceAllString(ntext, `# $1`) } func is_kitty_gui_cmdline(cmd ...string) bool { @@ -809,12 +809,12 @@ func (self *Themes) At(x int) *Theme { func (self *Themes) Names() []string { return self.index_map } func (self *Themes) create_index_map() { - self.index_map = maps.Keys(self.name_map) + self.index_map = utils.Keys(self.name_map) self.index_map = utils.StableSortWithKey(self.index_map, strings.ToLower) } func (self *Themes) Filtered(is_ok func(*Theme) bool) *Themes { - themes := utils.Filter(maps.Values(self.name_map), is_ok) + themes := utils.Filter(utils.Values(self.name_map), is_ok) ans := Themes{name_map: make(map[string]*Theme, len(themes))} for _, theme := range themes { ans.name_map[theme.metadata.Name] = theme diff --git a/tools/tui/graphics/collection.go b/tools/tui/graphics/collection.go index ce2e66759..825aaa3f9 100644 --- a/tools/tui/graphics/collection.go +++ b/tools/tui/graphics/collection.go @@ -15,8 +15,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/images" "kitty/tools/utils/shm" - - "golang.org/x/exp/maps" ) var _ = fmt.Print @@ -137,7 +135,7 @@ func (self *ImageCollection) ResizeForPageSize(width, height int) { defer self.mutex.Unlock() ctx := images.Context{} - keys := maps.Keys(self.images) + keys := utils.Keys(self.images) ctx.Parallel(0, len(keys), func(nums <-chan int) { for i := range nums { img := self.images[keys[i]] @@ -295,7 +293,7 @@ func (self *ImageCollection) LoadAll() { self.mutex.Lock() defer self.mutex.Unlock() ctx := images.Context{} - all := maps.Values(self.images) + all := utils.Values(self.images) ctx.Parallel(0, len(self.images), func(nums <-chan int) { for i := range nums { img := all[i] diff --git a/tools/tui/loop/timers.go b/tools/tui/loop/timers.go index 3ade482cf..d8a0cd2f1 100644 --- a/tools/tui/loop/timers.go +++ b/tools/tui/loop/timers.go @@ -4,10 +4,9 @@ package loop import ( "fmt" + "slices" "time" - "golang.org/x/exp/slices" - "kitty/tools/tty" "kitty/tools/utils" ) diff --git a/tools/tui/sgr/insert-formatting.go b/tools/tui/sgr/insert-formatting.go index c9c00c09a..a92c7c3e8 100644 --- a/tools/tui/sgr/insert-formatting.go +++ b/tools/tui/sgr/insert-formatting.go @@ -4,6 +4,7 @@ package sgr import ( "fmt" + "slices" "strconv" "strings" "unicode/utf8" @@ -11,8 +12,6 @@ import ( "kitty/tools/utils" "kitty/tools/utils/style" "kitty/tools/wcswidth" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/tui/shell_integration/api.go b/tools/tui/shell_integration/api.go index bbf07f136..ad57a7b06 100644 --- a/tools/tui/shell_integration/api.go +++ b/tools/tui/shell_integration/api.go @@ -6,14 +6,13 @@ import ( "archive/tar" "bytes" "fmt" + "maps" "os" "os/exec" "path/filepath" + "slices" "strings" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" - "kitty" "kitty/tools/tty" "kitty/tools/utils" diff --git a/tools/tui/subseq/score.go b/tools/tui/subseq/score.go index 4b10f22e2..57341a06d 100644 --- a/tools/tui/subseq/score.go +++ b/tools/tui/subseq/score.go @@ -4,12 +4,11 @@ package subseq import ( "fmt" + "slices" "strings" "kitty/tools/utils" "kitty/tools/utils/images" - - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/unicode_names/query_test.go b/tools/unicode_names/query_test.go index 78b836c6a..17bbde471 100644 --- a/tools/unicode_names/query_test.go +++ b/tools/unicode_names/query_test.go @@ -5,10 +5,10 @@ package unicode_names import ( "fmt" "kitty/tools/utils" + "slices" "testing" "github.com/google/go-cmp/cmp" - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/utils/images/loading.go b/tools/utils/images/loading.go index ab59a3fce..3ea306595 100644 --- a/tools/utils/images/loading.go +++ b/tools/utils/images/loading.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" "sync" @@ -23,7 +24,6 @@ import ( "github.com/edwvee/exiffix" "github.com/kovidgoyal/imaging" - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 60c75935c..779112bd8 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -9,10 +9,10 @@ import ( "path/filepath" "reflect" "runtime" + "slices" "strconv" "golang.org/x/exp/constraints" - "golang.org/x/exp/slices" ) var _ = fmt.Print diff --git a/tools/utils/set.go b/tools/utils/set.go index 892329abc..6618e22da 100644 --- a/tools/utils/set.go +++ b/tools/utils/set.go @@ -4,12 +4,26 @@ package utils import ( "fmt" - - "golang.org/x/exp/maps" ) var _ = fmt.Print +func Keys[M ~map[K]V, K comparable, V any](m M) []K { + r := make([]K, 0, len(m)) + for k := range m { + r = append(r, k) + } + return r +} + +func Values[M ~map[K]V, K comparable, V any](m M) []V { + r := make([]V, 0, len(m)) + for _, k := range m { + r = append(r, k) + } + return r +} + type Set[T comparable] struct { items map[T]struct{} } @@ -25,7 +39,7 @@ func (self *Set[T]) AddItems(val ...T) { } func (self *Set[T]) String() string { - return fmt.Sprintf("%#v", maps.Keys(self.items)) + return fmt.Sprintf("%#v", Keys(self.items)) } func (self *Set[T]) Remove(val T) { @@ -60,7 +74,7 @@ func (self *Set[T]) Iterable() map[T]struct{} { } func (self *Set[T]) AsSlice() []T { - return maps.Keys(self.items) + return Keys(self.items) } func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {