diff --git a/go.mod b/go.mod index 0d7372daf..d3250c2dc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module kitty -go 1.20 +go 1.21 require ( github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 diff --git a/kittens/ask/choices.go b/kittens/ask/choices.go index 370d4aae9..7cca7abb7 100644 --- a/kittens/ask/choices.go +++ b/kittens/ask/choices.go @@ -57,7 +57,7 @@ func truncate_at_space(text string, width int) (string, string) { } func extra_for(width, screen_width int) int { - return utils.Max(0, screen_width-width)/2 + 1 + return max(0, screen_width-width)/2 + 1 } func GetChoices(o *Options) (response string, err error) { @@ -71,7 +71,7 @@ func GetChoices(o *Options) (response string, err error) { prefix_style_pat := regexp.MustCompile("^(?:\x1b\\[[^m]*?m)+") choice_order := make([]Choice, 0, len(o.Choices)) clickable_ranges := make(map[string][]Range, 16) - allowed := utils.NewSet[string](utils.Max(2, len(o.Choices))) + allowed := utils.NewSet[string](max(2, len(o.Choices))) response_on_accept := o.Default switch o.Type { case "yesno": @@ -328,7 +328,7 @@ func GetChoices(o *Options) (response string, err error) { } } y := int(sz.HeightCells) - len(msg_lines) - y = utils.Max(0, (y/2)-2) + y = max(0, (y/2)-2) lp.QueueWriteString(strings.Repeat("\r\n", y)) for _, line := range msg_lines { if replacement_text != "" { diff --git a/kittens/hints/marks.go b/kittens/hints/marks.go index 9620206c9..b410b4b96 100644 --- a/kittens/hints/marks.go +++ b/kittens/hints/marks.go @@ -397,8 +397,8 @@ func mark(r *regexp2.Regexp, post_processors []PostProcessorFunc, group_processo if idx > 0 && g.IsNamed { c := g.LastCapture() if s, e := c.Byte_Offsets.Start, c.Byte_Offsets.End; s > -1 && e > -1 { - s = utils.Max(s, match_start) - e = utils.Min(e, match_end) + s = max(s, match_start) + e = min(e, match_end) gd[g.Name] = sanitize_pat.ReplaceAllLiteralString(text[s:e], "") } } @@ -413,8 +413,8 @@ func mark(r *regexp2.Regexp, post_processors []PostProcessorFunc, group_processo if opts.Type == "regex" && len(m.Groups) > 1 && !m.HasNamedGroups() { cp := m.Groups[1].LastCapture() ms, me := cp.Byte_Offsets.Start, cp.Byte_Offsets.End - match_start = utils.Max(match_start, ms) - match_end = utils.Min(match_end, me) + match_start = max(match_start, ms) + match_end = min(match_end, me) full_match = sanitize_pat.ReplaceAllLiteralString(text[match_start:match_end], "") } if full_match != "" { @@ -576,7 +576,7 @@ process_answer: return "", nil, nil, &ErrNoMatches{Type: opts.Type} } largest_index := ans[len(ans)-1].Index - offset := utils.Max(0, opts.HintsOffset) + offset := max(0, opts.HintsOffset) index_map = make(map[int]*Mark, len(ans)) for i := range ans { m := &ans[i] diff --git a/tools/cmd/edit_in_kitty/main.go b/tools/cmd/edit_in_kitty/main.go index d0edaca8c..fb4929d4d 100644 --- a/tools/cmd/edit_in_kitty/main.go +++ b/tools/cmd/edit_in_kitty/main.go @@ -105,7 +105,7 @@ func edit_loop(data_to_send string, kill_if_signaled bool, on_data OnDataCallbac lp.OnInitialize = func() (string, error) { pos, chunk_num := 0, 0 for { - limit := utils.Min(pos+2048, len(data_to_send)) + limit := min(pos+2048, len(data_to_send)) if limit <= pos { break } diff --git a/tools/config/utils.go b/tools/config/utils.go index dd1fc937f..bd43a5817 100644 --- a/tools/config/utils.go +++ b/tools/config/utils.go @@ -30,7 +30,7 @@ func ParseStrDict(val, record_sep, field_sep string) (map[string]string, error) func PositiveFloat(val string) (ans float64, err error) { ans, err = strconv.ParseFloat(val, 64) if err == nil { - ans = utils.Max(0, ans) + ans = max(0, ans) } return } @@ -38,7 +38,7 @@ func PositiveFloat(val string) (ans float64, err error) { func UnitFloat(val string) (ans float64, err error) { ans, err = strconv.ParseFloat(val, 64) if err == nil { - ans = utils.Max(0, utils.Min(ans, 1)) + ans = max(0, min(ans, 1)) } return } diff --git a/tools/rsync/api.go b/tools/rsync/api.go index 4df743e12..b18744d28 100644 --- a/tools/rsync/api.go +++ b/tools/rsync/api.go @@ -269,12 +269,12 @@ func NewDiffer() *Differ { // Use to create a signature and possibly apply a delta func NewPatcher(expected_input_size int64) (ans *Patcher) { bs := DefaultBlockSize - sz := utils.Max(0, expected_input_size) + sz := max(0, expected_input_size) if sz > 0 { bs = int(math.Round(math.Sqrt(float64(sz)))) } ans = &Patcher{} - ans.rsync.BlockSize = utils.Min(bs, MaxBlockSize) + ans.rsync.BlockSize = min(bs, MaxBlockSize) ans.rsync.SetHasher(new_xxh3_64) ans.rsync.SetChecksummer(new_xxh3_128) diff --git a/tools/rsync/api_test.go b/tools/rsync/api_test.go index 302405f21..2eae4a2c8 100644 --- a/tools/rsync/api_test.go +++ b/tools/rsync/api_test.go @@ -31,7 +31,7 @@ func run_roundtrip_test(t *testing.T, src_data, changed []byte, num_of_patches, test_equal := func(src_data, output []byte) { if !bytes.Equal(src_data, output) { - first_diff := utils.Min(len(src_data), len(output)) + first_diff := min(len(src_data), len(output)) for i := 0; i < first_diff; i++ { if src_data[i] != output[i] { first_diff = i @@ -115,7 +115,7 @@ func run_roundtrip_test(t *testing.T, src_data, changed []byte, num_of_patches, outputbuf := bytes.Buffer{} p.StartDelta(&outputbuf, bytes.NewReader(changed)) for len(deltabuf) > 0 { - n := utils.Min(123, len(deltabuf)) + n := min(123, len(deltabuf)) if err := p.UpdateDelta(deltabuf[:n]); err != nil { t.Fatal(err) } diff --git a/tools/tui/loop/mouse.go b/tools/tui/loop/mouse.go index d0b327c08..775928973 100644 --- a/tools/tui/loop/mouse.go +++ b/tools/tui/loop/mouse.go @@ -6,8 +6,6 @@ import ( "fmt" "strconv" "strings" - - "kitty/tools/utils" ) var _ = fmt.Print @@ -107,7 +105,7 @@ func (e MouseEvent) String() string { } func pixel_to_cell(px, length, cell_length int) int { - px = utils.Max(0, utils.Min(px, length-1)) + px = max(0, min(px, length-1)) return px / cell_length } diff --git a/tools/tui/mouse.go b/tools/tui/mouse.go index 1de4b3fcf..41b894af3 100644 --- a/tools/tui/mouse.go +++ b/tools/tui/mouse.go @@ -7,7 +7,6 @@ import ( "time" "kitty/tools/tui/loop" - "kitty/tools/utils" ) var _ = fmt.Print @@ -68,7 +67,7 @@ func (self *MouseSelection) Clear() { *self = MouseSelection{} } func (ms *MouseSelection) StartNewSelection(ev *loop.MouseEvent, line LinePos, min_y, max_y, cell_width, cell_height int) { *ms = MouseSelection{cell_width: cell_width, cell_height: cell_height, min_y: min_y, max_y: max_y} ms.start.line = line - ms.start.x = utils.Max(line.MinX(), utils.Min(ev.Cell.X, line.MaxX())) + ms.start.x = max(line.MinX(), min(ev.Cell.X, line.MaxX())) cell_start := cell_width * ev.Cell.X ms.start.in_first_half_of_cell = ev.Pixel.X <= cell_start+cell_width/2 ms.end = ms.start @@ -78,7 +77,7 @@ func (ms *MouseSelection) StartNewSelection(ev *loop.MouseEvent, line LinePos, m func (ms *MouseSelection) Update(ev *loop.MouseEvent, line LinePos) { ms.drag_scroll.timer_id = 0 if ms.is_active { - ms.end.x = utils.Max(line.MinX(), utils.Min(ev.Cell.X, line.MaxX())) + ms.end.x = max(line.MinX(), min(ev.Cell.X, line.MaxX())) cell_start := ms.cell_width * ms.end.x ms.end.in_first_half_of_cell = ev.Pixel.X <= cell_start+ms.cell_width/2 ms.end.line = line diff --git a/tools/tui/readline/actions.go b/tools/tui/readline/actions.go index 75b268742..dcc38c316 100644 --- a/tools/tui/readline/actions.go +++ b/tools/tui/readline/actions.go @@ -19,7 +19,7 @@ func (self *Readline) text_upto_cursor_pos() string { buf.Grow(1024) for i, line := range self.input_state.lines { if i == self.input_state.cursor.Y { - buf.WriteString(line[:utils.Min(len(line), self.input_state.cursor.X)]) + buf.WriteString(line[:min(len(line), self.input_state.cursor.X)]) break } else { buf.WriteString(line) @@ -34,7 +34,7 @@ func (self *Readline) text_after_cursor_pos() string { buf.Grow(1024) for i, line := range self.input_state.lines { if i == self.input_state.cursor.Y { - buf.WriteString(line[utils.Min(len(line), self.input_state.cursor.X):]) + buf.WriteString(line[min(len(line), self.input_state.cursor.X):]) buf.WriteString("\n") } else if i > self.input_state.cursor.Y { buf.WriteString(line) @@ -165,7 +165,7 @@ func (self *Readline) move_cursor_vertically(amt int) (ans int) { break } } - target_line_num := utils.Min(utils.Max(0, cursor_line_num+amt), len(screen_lines)-1) + target_line_num := min(max(0, cursor_line_num+amt), len(screen_lines)-1) ans = target_line_num - cursor_line_num if ans != 0 { self.move_cursor_to_target_line(screen_lines[cursor_line_num], screen_lines[target_line_num]) @@ -441,9 +441,9 @@ func (self *Readline) kill_previous_space_delimited_word(amt uint, traverse_line } func (self *Readline) ensure_position_in_bounds(pos *Position) *Position { - pos.Y = utils.Max(0, utils.Min(pos.Y, len(self.input_state.lines)-1)) + pos.Y = max(0, min(pos.Y, len(self.input_state.lines)-1)) line := self.input_state.lines[pos.Y] - pos.X = utils.Max(0, utils.Min(pos.X, len(line))) + pos.X = max(0, min(pos.X, len(line))) return pos } diff --git a/tools/tui/readline/completion.go b/tools/tui/readline/completion.go index 7b2babd7f..eda0475b1 100644 --- a/tools/tui/readline/completion.go +++ b/tools/tui/readline/completion.go @@ -151,7 +151,7 @@ type cell struct { } func (self cell) whitespace(desired_length int) string { - return strings.Repeat(" ", utils.Max(0, desired_length-self.length)) + return strings.Repeat(" ", max(0, desired_length-self.length)) } type column struct { @@ -212,7 +212,7 @@ func (self *Readline) screen_lines_for_match_group_without_descriptions(g *cli.M } } var ans []column - ncols := utils.Max(1, self.screen_width/(max_length+1)) + ncols := max(1, self.screen_width/(max_length+1)) for { cols, total_length := layout_words_in_table(words, lengths, ncols) if total_length > self.screen_width { diff --git a/tools/tui/readline/draw.go b/tools/tui/readline/draw.go index 47accc89d..8c6d775dc 100644 --- a/tools/tui/readline/draw.go +++ b/tools/tui/readline/draw.go @@ -25,8 +25,8 @@ func (self *Readline) update_current_screen_size() { screen_size.WidthCells = 80 screen_size.HeightCells = 24 } - self.screen_width = utils.Max(1, int(screen_size.WidthCells)) - self.screen_height = utils.Max(1, int(screen_size.HeightCells)) + self.screen_width = max(1, int(screen_size.WidthCells)) + self.screen_height = max(1, int(screen_size.HeightCells)) } type ScreenLine struct { diff --git a/tools/tui/readline/history.go b/tools/tui/readline/history.go index c2da774fd..e2665f46e 100644 --- a/tools/tui/readline/history.go +++ b/tools/tui/readline/history.go @@ -227,13 +227,13 @@ func (self *HistoryMatches) first(rl *Readline) bool { } func (self *HistoryMatches) last(rl *Readline) bool { - self.current_idx = utils.Max(0, len(self.items)-1) + self.current_idx = max(0, len(self.items)-1) return self.apply(rl) } func (self *HistoryMatches) previous(num uint, rl *Readline) bool { if self.current_idx > 0 { - self.current_idx = utils.Max(0, self.current_idx-int(num)) + self.current_idx = max(0, self.current_idx-int(num)) return self.apply(rl) } return false @@ -241,7 +241,7 @@ func (self *HistoryMatches) previous(num uint, rl *Readline) bool { func (self *HistoryMatches) next(num uint, rl *Readline) bool { if self.current_idx+1 < len(self.items) { - self.current_idx = utils.Min(len(self.items)-1, self.current_idx+int(num)) + self.current_idx = min(len(self.items)-1, self.current_idx+int(num)) return self.apply(rl) } return false @@ -295,7 +295,7 @@ func (self *Readline) markup_history_search() { func (self *Readline) remove_text_from_history_search(num uint) uint { l := len(self.history_search.query) - nl := utils.Max(0, l-int(num)) + nl := max(0, l-int(num)) self.history_search.query = self.history_search.query[:nl] num_removed := uint(l - nl) self.add_text_to_history_search("") // update the search results @@ -361,7 +361,7 @@ func (self *Readline) add_text_to_history_search(text string) { idx = 0 } } - self.history_search.current_idx = utils.Max(0, idx) + self.history_search.current_idx = max(0, idx) self.markup_history_search() } @@ -372,9 +372,9 @@ func (self *Readline) next_history_search(backwards bool, num uint) bool { return false } if backwards { - ni = utils.Max(0, ni-int(num)) + ni = max(0, ni-int(num)) } else { - ni = utils.Min(ni+int(num), len(self.history_search.items)-1) + ni = min(ni+int(num), len(self.history_search.items)-1) } if ni == self.history_search.current_idx { return false diff --git a/tools/utils/images/loading.go b/tools/utils/images/loading.go index 7bb2add92..f145644ff 100644 --- a/tools/utils/images/loading.go +++ b/tools/utils/images/loading.go @@ -216,7 +216,7 @@ func check_resize(frame *ImageFrame, filename string) error { } func (frame *ImageFrame) set_delay(min_gap, delay int) { - frame.Delay_ms = int32(utils.Max(min_gap, delay) * 10) + frame.Delay_ms = int32(max(min_gap, delay) * 10) if frame.Delay_ms == 0 { frame.Delay_ms = -1 // gapless frame in the graphics protocol } @@ -308,7 +308,7 @@ func parse_identify_record(ans *IdentifyRecord, raw *IdentifyOutput) (err error) if err != nil { return fmt.Errorf("Invalid gap value in identify output: %s", raw.Gap) } - ans.Gap = utils.Max(0, ans.Gap) + ans.Gap = max(0, ans.Gap) } area, pos, found := strings.Cut(raw.Canvas, "+") ok := false diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 0100cb895..60c75935c 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -3,6 +3,7 @@ package utils import ( + "cmp" "fmt" "os" "path/filepath" @@ -98,19 +99,13 @@ 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 + key_cmp := func(a, b t) int { + return cmp.Compare(a.key, b.key) } if stable { - slices.SortStableFunc(temp, cmp) + slices.SortStableFunc(temp, key_cmp) } else { - slices.SortFunc(temp, cmp) + slices.SortFunc(temp, key_cmp) } for i, x := range temp { s[i] = x.val diff --git a/tools/utils/style/indent-and-wrap.go b/tools/utils/style/indent-and-wrap.go index 27c40eb10..9adf36a97 100644 --- a/tools/utils/style/indent-and-wrap.go +++ b/tools/utils/style/indent-and-wrap.go @@ -507,7 +507,7 @@ func (self *wrapper) wrap_text(text string) []string { } func new_wrapper(opts WrapOptions, width int) *wrapper { - width = utils.Max(2, width) + width = max(2, width) ans := wrapper{indent: opts.Indent, width: width, trim_whitespace: opts.Trim_whitespace, indent_width: wcswidth.Stringwidth(opts.Indent)} if opts.Ignore_lines_containing != "" { ans.ignore_lines_containing = utils.Splitlines(opts.Ignore_lines_containing) diff --git a/tools/wcswidth/iter.go b/tools/wcswidth/iter.go index f3c57904f..4b1962d2d 100644 --- a/tools/wcswidth/iter.go +++ b/tools/wcswidth/iter.go @@ -4,7 +4,6 @@ package wcswidth import ( "fmt" - "kitty/tools/utils" ) var _ = fmt.Print @@ -124,7 +123,7 @@ func (self *CellIterator) Backward() (has_more bool) { for self.Forward() { cells = append(cells, self.current) } - ri.pos = utils.Min(utils.Max(-1, current_cell_num-1), len(cells)) + ri.pos = min(max(-1, current_cell_num-1), len(cells)) ri.cells = cells } if ri.pos > -1 {