From 341d845b9a051a0aa8f95315160a4db7ab753f58 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Aug 2023 22:50:13 +0530 Subject: [PATCH] Port calls to slices.Sort functions since they now need a cmp() function rather than a less() function Also rename os.SEEK_* to io.Seek* as the former has been deprecated --- kittens/clipboard/legacy.go | 2 +- kittens/clipboard/read.go | 3 ++- kittens/diff/collect_test.go | 5 +++-- kittens/diff/search.go | 2 +- kittens/transfer/receive.go | 6 +++--- kittens/transfer/send.go | 2 +- tools/cli/command.go | 6 +++--- tools/rsync/algorithm.go | 3 +-- tools/themes/collection.go | 10 +++++++++- tools/tui/loop/timers.go | 2 +- tools/tui/readline/history.go | 4 ++-- tools/tui/sgr/insert-formatting.go | 2 +- tools/tui/subseq/score_test.go | 10 +++++++++- tools/unicode_names/query_test.go | 4 ++-- tools/utils/images/loading.go | 4 ++-- tools/utils/misc.go | 21 +++++++++++++++------ tools/utils/shm/shm_fs.go | 4 ++-- tools/utils/shm/shm_syscall.go | 4 ++-- 18 files changed, 60 insertions(+), 34 deletions(-) diff --git a/kittens/clipboard/legacy.go b/kittens/clipboard/legacy.go index 439b96456..74fec7dc0 100644 --- a/kittens/clipboard/legacy.go +++ b/kittens/clipboard/legacy.go @@ -87,7 +87,7 @@ func preread_stdin() (data_src io.Reader, tempfile *os.File, err error) { if err != nil { return nil, nil, fmt.Errorf("Failed to copy data from STDIN pipe to temp file with error: %w", err) } - tempfile.Seek(0, os.SEEK_SET) + tempfile.Seek(0, io.SeekStart) data_src = tempfile } else if stdin_data != nil { data_src = bytes.NewBuffer(stdin_data) diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index 19a707b7a..a78dce321 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -7,6 +7,7 @@ import ( "encoding/base64" "fmt" "image" + "io" "os" "path/filepath" "strings" @@ -110,7 +111,7 @@ func (self *Output) commit() { return } if self.image_needs_conversion { - self.dest.Seek(0, os.SEEK_SET) + self.dest.Seek(0, io.SeekStart) img, _, err := image.Decode(self.dest) self.dest.Close() os.Remove(self.dest.Name()) diff --git a/kittens/diff/collect_test.go b/kittens/diff/collect_test.go index e9bbb448d..d3eeb0c71 100644 --- a/kittens/diff/collect_test.go +++ b/kittens/diff/collect_test.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "kitty/tools/utils" @@ -42,8 +43,8 @@ func TestDiffCollectWalk(t *testing.T) { t.Fatal(err) } if diff := cmp.Diff( - utils.Sort(expected_names.AsSlice(), func(a, b string) bool { return a < b }), - utils.Sort(names.AsSlice(), func(a, b string) bool { return a < b }), + utils.Sort(expected_names.AsSlice(), strings.Compare), + utils.Sort(names.AsSlice(), strings.Compare), ); diff != "" { t.Fatal(diff) } diff --git a/kittens/diff/search.go b/kittens/diff/search.go index 7eb8286be..cf7e4f3d7 100644 --- a/kittens/diff/search.go +++ b/kittens/diff/search.go @@ -124,7 +124,7 @@ func (self *Search) search(logical_lines *LogicalLines) { } }) for _, spans := range self.matches { - slices.SortFunc(spans, func(a, b Span) bool { return a.start < b.start }) + slices.SortFunc(spans, func(a, b Span) int { return a.start - b.start }) } } diff --git a/kittens/transfer/receive.go b/kittens/transfer/receive.go index 8599bea92..f0ee3d95d 100644 --- a/kittens/transfer/receive.go +++ b/kittens/transfer/receive.go @@ -52,7 +52,7 @@ type filesystem_file struct { } func (ff *filesystem_file) tell() (int64, error) { - return ff.f.Seek(0, os.SEEK_CUR) + return ff.f.Seek(0, io.SeekCurrent) } func (ff *filesystem_file) close() error { @@ -78,7 +78,7 @@ func (pf *patch_file) tell() (int64, error) { s, err := os.Stat(pf.path) return s.Size(), err } - return pf.temp.Seek(0, os.SEEK_CUR) + return pf.temp.Seek(0, io.SeekCurrent) } func (pf *patch_file) close() (err error) { @@ -685,7 +685,7 @@ func files_for_receive(opts *Options, dest string, files []*remote_file, remote_ spec_paths := make([]string, len(specs)) for i := range specs { // use the shortest path as the path for the spec - slices.SortStableFunc(spec_map[i], func(a, b *remote_file) bool { return len(a.remote_path) < len(b.remote_path) }) + slices.SortStableFunc(spec_map[i], func(a, b *remote_file) int { return len(a.remote_path) - len(b.remote_path) }) spec_paths[i] = spec_map[i][0].remote_path } if opts.Mode == "mirror" { diff --git a/kittens/transfer/send.go b/kittens/transfer/send.go index afe6dba85..3af46b442 100644 --- a/kittens/transfer/send.go +++ b/kittens/transfer/send.go @@ -951,7 +951,7 @@ func (self *File) next_chunk() (ans string, asz int, err error) { } if n <= 0 { is_last = true - } else if pos, _ := self.actual_file.Seek(0, os.SEEK_CUR); pos >= self.file_size { + } else if pos, _ := self.actual_file.Seek(0, io.SeekCurrent); pos >= self.file_size { is_last = true } chunk = chunk[:n] diff --git a/tools/cli/command.go b/tools/cli/command.go index 585516ce9..37f15ec71 100644 --- a/tools/cli/command.go +++ b/tools/cli/command.go @@ -308,12 +308,12 @@ func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option) { } func sort_levenshtein_matches(q string, matches []string) { - utils.StableSort(matches, func(a, b string) bool { + utils.StableSort(matches, func(a, b string) int { la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true) if la != lb { - return la < lb + return la - lb } - return a < b + return strings.Compare(a, b) }) } diff --git a/tools/rsync/algorithm.go b/tools/rsync/algorithm.go index 8f5d00c27..fd2dd882f 100644 --- a/tools/rsync/algorithm.go +++ b/tools/rsync/algorithm.go @@ -15,7 +15,6 @@ import ( "fmt" "hash" "io" - "os" "strconv" "github.com/zeebo/xxh3" @@ -291,7 +290,7 @@ func (r *rsync) ApplyDelta(output io.Writer, target io.ReadSeeker, op Operation) return err } write_block := func(op Operation) (err error) { - if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), os.SEEK_SET); err != nil { + if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), io.SeekStart); err != nil { return err } n, err = io.ReadAtLeast(target, buffer, r.BlockSize) diff --git a/tools/themes/collection.go b/tools/themes/collection.go index dc16ed28c..7b0b54341 100644 --- a/tools/themes/collection.go +++ b/tools/themes/collection.go @@ -909,7 +909,15 @@ func (self *Themes) ThemeByName(name string) *Theme { func match(expression string, items []string) []*subseq.Match { matches := subseq.ScoreItems(expression, items, subseq.Options{Level1: " "}) - matches = utils.StableSort(matches, func(a, b *subseq.Match) bool { return a.Score > b.Score }) + matches = utils.StableSort(matches, func(a, b *subseq.Match) int { + if b.Score < a.Score { + return -1 + } + if b.Score > a.Score { + return 1 + } + return 0 + }) return matches } diff --git a/tools/tui/loop/timers.go b/tools/tui/loop/timers.go index 38d211039..6be7a8ebe 100644 --- a/tools/tui/loop/timers.go +++ b/tools/tui/loop/timers.go @@ -81,5 +81,5 @@ func (self *Loop) dispatch_timers(now time.Time) error { } func (self *Loop) sort_timers() { - slices.SortStableFunc(self.timers, func(a, b *timer) bool { return a.deadline.Before(b.deadline) }) + slices.SortStableFunc(self.timers, func(a, b *timer) int { return a.deadline.Compare(b.deadline) }) } diff --git a/tools/tui/readline/history.go b/tools/tui/readline/history.go index c46fffc8a..c2da774fd 100644 --- a/tools/tui/readline/history.go +++ b/tools/tui/readline/history.go @@ -90,8 +90,8 @@ func (self *History) merge_items(items ...HistoryItem) { if !changed { return } - self.items = utils.StableSort(self.items, func(a, b HistoryItem) bool { - return a.Timestamp.Before(b.Timestamp) + self.items = utils.StableSort(self.items, func(a, b HistoryItem) int { + return a.Timestamp.Compare(b.Timestamp) }) if len(self.items) > self.max_items { self.items = self.items[len(self.items)-self.max_items:] diff --git a/tools/tui/sgr/insert-formatting.go b/tools/tui/sgr/insert-formatting.go index ea0d57f72..c9c00c09a 100644 --- a/tools/tui/sgr/insert-formatting.go +++ b/tools/tui/sgr/insert-formatting.go @@ -447,7 +447,7 @@ func InsertFormatting(text string, spans ...*Span) string { var in_span *Span ans := make([]byte, 0, 2*len(text)) var overall_sgr_state SGR - slices.SortFunc(spans, func(a, b *Span) bool { return a.Offset < b.Offset }) + slices.SortFunc(spans, func(a, b *Span) int { return a.Offset - b.Offset }) text_len := 0 var ep *wcswidth.EscapeCodeParser diff --git a/tools/tui/subseq/score_test.go b/tools/tui/subseq/score_test.go index 0be713d7b..d1ddd329b 100644 --- a/tools/tui/subseq/score_test.go +++ b/tools/tui/subseq/score_test.go @@ -21,7 +21,15 @@ func TestSubseq(t *testing.T) { simple := func(items, query string, expected ...string) { matches := ScoreItems(query, utils.Splitlines(items), Options{}) if sort_by_score { - matches = utils.StableSort(matches, func(a, b *Match) bool { return a.Score > b.Score }) + matches = utils.StableSort(matches, func(a, b *Match) int { + if b.Score < a.Score { + return -1 + } + if b.Score > a.Score { + return 1 + } + return 0 + }) } actual := make([]string, 0, len(matches)) actual_positions := make([][]int, 0, len(matches)) diff --git a/tools/unicode_names/query_test.go b/tools/unicode_names/query_test.go index 543e3330e..78b836c6a 100644 --- a/tools/unicode_names/query_test.go +++ b/tools/unicode_names/query_test.go @@ -18,9 +18,9 @@ func TestUnicodeInputQueries(t *testing.T) { if expected == nil { expected = make([]rune, 0) } - expected = utils.Sort(expected, func(a, b rune) bool { return a < b }) + expected = utils.Sort(expected, func(a, b rune) int { return int(a) - int(b) }) actual := CodePointsForQuery(query) - actual = utils.Sort(actual, func(a, b rune) bool { return a < b }) + actual = utils.Sort(actual, func(a, b rune) int { return int(a) - int(b) }) diff := cmp.Diff(expected, actual) if diff != "" { t.Fatalf("Failed query: %#v\n%s", query, diff) diff --git a/tools/utils/images/loading.go b/tools/utils/images/loading.go index 9289abc67..7bb2add92 100644 --- a/tools/utils/images/loading.go +++ b/tools/utils/images/loading.go @@ -244,7 +244,7 @@ func OpenNativeImageFromReader(f io.ReadSeeker) (ans *ImageData, err error) { if err != nil { return nil, err } - f.Seek(0, os.SEEK_SET) + f.Seek(0, io.SeekStart) ans = &ImageData{Width: c.Width, Height: c.Height, Format_uppercase: strings.ToUpper(fmt)} if ans.Format_uppercase == "GIF" { @@ -563,7 +563,7 @@ func RenderWithMagick(path string, ro *RenderOptions, frames []IdentifyRecord) ( err = fmt.Errorf("Failed to render %d out of %d frames", len(frames)-len(ans), len(frames)) return } - slices.SortFunc(ans, func(a, b *ImageFrame) bool { return a.Number < b.Number }) + slices.SortFunc(ans, func(a, b *ImageFrame) int { return a.Number - b.Number }) anchor_frame := 1 for i, frame := range ans { anchor_frame, frame.Compose_onto = SetGIFFrameDisposal(frame.Number, anchor_frame, byte(frames[i].Disposal)) diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 24b46a46c..0100cb895 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -79,13 +79,13 @@ func Repeat[T any](x T, n int) []T { return ans } -func Sort[T any](s []T, less func(a, b T) bool) []T { - slices.SortFunc(s, less) +func Sort[T any](s []T, cmp func(a, b T) int) []T { + slices.SortFunc(s, cmp) return s } -func StableSort[T any](s []T, less func(a, b T) bool) []T { - slices.SortStableFunc(s, less) +func StableSort[T any](s []T, cmp func(a, b T) int) []T { + slices.SortStableFunc(s, cmp) return s } @@ -98,10 +98,19 @@ func sort_with_key[T any, C constraints.Ordered](stable bool, s []T, key func(a for i, x := range s { temp[i].val, temp[i].key = x, key(x) } + cmp := func(a, b t) int { + if a.key < b.key { + return -1 + } + if a.key > b.key { + return 1 + } + return 0 + } if stable { - slices.SortStableFunc(temp, func(a, b t) bool { return a.key < b.key }) + slices.SortStableFunc(temp, cmp) } else { - slices.SortFunc(temp, func(a, b t) bool { return a.key < b.key }) + slices.SortFunc(temp, cmp) } for i, x := range temp { s[i] = x.val diff --git a/tools/utils/shm/shm_fs.go b/tools/utils/shm/shm_fs.go index bf0a817a1..358e3a781 100644 --- a/tools/utils/shm/shm_fs.go +++ b/tools/utils/shm/shm_fs.go @@ -48,9 +48,9 @@ func (self *file_based_mmap) Seek(offset int64, whence int) (ret int64, err erro switch whence { case io.SeekStart: self.pos = offset - case os.SEEK_END: + case io.SeekEnd: self.pos = int64(len(self.region)) + offset - case os.SEEK_CUR: + case io.SeekCurrent: self.pos += offset } return self.pos, nil diff --git a/tools/utils/shm/shm_syscall.go b/tools/utils/shm/shm_syscall.go index 14203d2bd..7922a7b1b 100644 --- a/tools/utils/shm/shm_syscall.go +++ b/tools/utils/shm/shm_syscall.go @@ -123,9 +123,9 @@ func (self *syscall_based_mmap) Seek(offset int64, whence int) (ret int64, err e switch whence { case io.SeekStart: self.pos = offset - case os.SEEK_END: + case io.SeekEnd: self.pos = int64(len(self.region)) + offset - case os.SEEK_CUR: + case io.SeekCurrent: self.pos += offset } return self.pos, nil