Use stdlib maps/slices

This commit is contained in:
Kovid Goyal 2024-05-04 14:34:40 +05:30
parent e7ed5716a9
commit 405f5ce148
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
27 changed files with 55 additions and 60 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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"

View File

@ -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"
}

View File

@ -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

View File

@ -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"
)

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -7,10 +7,10 @@ import (
"io"
"os"
"os/exec"
"slices"
"strings"
"time"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
"kitty"

View File

@ -4,10 +4,9 @@ package cli
import (
"fmt"
"slices"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
var _ = fmt.Print

View File

@ -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"
)

View File

@ -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()

View File

@ -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)
}

View File

@ -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.

View File

@ -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"
)

View File

@ -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

View File

@ -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]

View File

@ -4,10 +4,9 @@ package loop
import (
"fmt"
"slices"
"time"
"golang.org/x/exp/slices"
"kitty/tools/tty"
"kitty/tools/utils"
)

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -9,10 +9,10 @@ import (
"path/filepath"
"reflect"
"runtime"
"slices"
"strconv"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)
var _ = fmt.Print

View File

@ -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]) {