From 4ad3827511a0ad1391288717ebf33ccc029cc0a6 Mon Sep 17 00:00:00 2001 From: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com> Date: Tue, 24 Mar 2026 23:57:13 +0100 Subject: [PATCH] refactor: simplify listing functions (#2465) --- ui.go | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/ui.go b/ui.go index 8ec5eff5..d755acfd 100644 --- a/ui.go +++ b/ui.go @@ -2,13 +2,14 @@ package main import ( "bytes" + "cmp" "fmt" "log" + "maps" "os" "path/filepath" "reflect" "slices" - "sort" "strconv" "strings" "text/tabwriter" @@ -1218,11 +1219,11 @@ func listBinds(binds map[string]map[string]expr) string { } } - sort.Slice(entries, func(i, j int) bool { - if entries[i].key != entries[j].key { - return entries[i].key < entries[j].key + slices.SortFunc(entries, func(a, b entry) int { + if c := cmp.Compare(a.key, b.key); c != 0 { + return c } - return entries[i].mode < entries[j].mode + return cmp.Compare(a.mode, b.mode) }) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) @@ -1239,15 +1240,9 @@ func listMatchingBinds(binds map[string]expr, prefix string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) - keys := make([]string, 0, len(binds)) - for k := range binds { - keys = append(keys, k) - } - sort.Strings(keys) - t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "key\tcommand") - for _, k := range keys { + for _, k := range slices.Sorted(maps.Keys(binds)) { remain, _ := strings.CutPrefix(k, prefix) fmt.Fprintf(t, "%s\t%v\n", remain, binds[k]) } @@ -1260,15 +1255,9 @@ func listCmds(cmds map[string]expr) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) - keys := make([]string, 0, len(cmds)) - for k := range cmds { - keys = append(keys, k) - } - sort.Strings(keys) - t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "name\tcommand") - for _, k := range keys { + for _, k := range slices.Sorted(maps.Keys(cmds)) { fmt.Fprintf(t, "%s\t%v\n", k, cmds[k]) } t.Flush() @@ -1320,15 +1309,9 @@ func listMarks(marks map[string]string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) - keys := make([]string, 0, len(marks)) - for k := range marks { - keys = append(keys, k) - } - sort.Strings(keys) - t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "mark\tpath") - for _, k := range keys { + for _, k := range slices.Sorted(maps.Keys(marks)) { fmt.Fprintf(t, "%s\t%s\n", k, marks[k]) } t.Flush()