From b97dd6bc3f7c8bfc7725e1cf50b26ff27bb9b403 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 15 Oct 2024 15:28:33 +0200 Subject: [PATCH] Remove utils.Clamp, use lo.Clamp instead --- pkg/commands/patch/patch.go | 7 +++---- pkg/gui/context/list_renderer.go | 2 +- pkg/gui/context/traits/list_cursor.go | 3 ++- pkg/gui/mergeconflicts/state.go | 5 +++-- pkg/utils/utils.go | 9 --------- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/pkg/commands/patch/patch.go b/pkg/commands/patch/patch.go index d785fe49e..5e4f9a846 100644 --- a/pkg/commands/patch/patch.go +++ b/pkg/commands/patch/patch.go @@ -1,7 +1,6 @@ package patch import ( - "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" ) @@ -54,7 +53,7 @@ func (self *Patch) Lines() []*PatchLine { // Returns the patch line index of the first line in the given hunk func (self *Patch) HunkStartIdx(hunkIndex int) int { - hunkIndex = utils.Clamp(hunkIndex, 0, len(self.hunks)-1) + hunkIndex = lo.Clamp(hunkIndex, 0, len(self.hunks)-1) result := len(self.header) for i := 0; i < hunkIndex; i++ { @@ -65,7 +64,7 @@ func (self *Patch) HunkStartIdx(hunkIndex int) int { // Returns the patch line index of the last line in the given hunk func (self *Patch) HunkEndIdx(hunkIndex int) int { - hunkIndex = utils.Clamp(hunkIndex, 0, len(self.hunks)-1) + hunkIndex = lo.Clamp(hunkIndex, 0, len(self.hunks)-1) return self.HunkStartIdx(hunkIndex) + self.hunks[hunkIndex].lineCount() - 1 } @@ -118,7 +117,7 @@ func (self *Patch) HunkContainingLine(idx int) int { // Returns the patch line index of the next change (i.e. addition or deletion). func (self *Patch) GetNextChangeIdx(idx int) int { - idx = utils.Clamp(idx, 0, self.LineCount()-1) + idx = lo.Clamp(idx, 0, self.LineCount()-1) lines := self.Lines() diff --git a/pkg/gui/context/list_renderer.go b/pkg/gui/context/list_renderer.go index 06fc69b0c..9bde5fb4b 100644 --- a/pkg/gui/context/list_renderer.go +++ b/pkg/gui/context/list_renderer.go @@ -52,7 +52,7 @@ func (self *ListRenderer) ModelIndexToViewIndex(modelIndex int) int { } func (self *ListRenderer) ViewIndexToModelIndex(viewIndex int) int { - viewIndex = utils.Clamp(viewIndex, 0, self.list.Len()+self.numNonModelItems) + viewIndex = lo.Clamp(viewIndex, 0, self.list.Len()+self.numNonModelItems) if self.modelIndicesByViewIndex != nil { return self.modelIndicesByViewIndex[viewIndex] } diff --git a/pkg/gui/context/traits/list_cursor.go b/pkg/gui/context/traits/list_cursor.go index 31f3de7a4..a8ff89716 100644 --- a/pkg/gui/context/traits/list_cursor.go +++ b/pkg/gui/context/traits/list_cursor.go @@ -3,6 +3,7 @@ package traits import ( "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type RangeSelectMode int @@ -85,7 +86,7 @@ func (self *ListCursor) clampValue(value int) int { clampedValue := -1 length := self.getLength() if length > 0 { - clampedValue = utils.Clamp(value, 0, length-1) + clampedValue = lo.Clamp(value, 0, length-1) } return clampedValue diff --git a/pkg/gui/mergeconflicts/state.go b/pkg/gui/mergeconflicts/state.go index a3dbcff3a..047241353 100644 --- a/pkg/gui/mergeconflicts/state.go +++ b/pkg/gui/mergeconflicts/state.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) // State represents the selection state of the merge conflict context. @@ -37,14 +38,14 @@ func (s *State) setConflictIndex(index int) { if len(s.conflicts) == 0 { s.conflictIndex = 0 } else { - s.conflictIndex = utils.Clamp(index, 0, len(s.conflicts)-1) + s.conflictIndex = lo.Clamp(index, 0, len(s.conflicts)-1) } s.setSelectionIndex(s.selectionIndex) } func (s *State) setSelectionIndex(index int) { if selections := s.availableSelections(); len(selections) != 0 { - s.selectionIndex = utils.Clamp(index, 0, len(selections)-1) + s.selectionIndex = lo.Clamp(index, 0, len(selections)-1) } } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index ceb8f0dac..c7118ff5c 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -39,15 +39,6 @@ func SortRange(x int, y int) (int, int) { return y, x } -func Clamp(x int, min int, max int) int { - if x < min { - return min - } else if x > max { - return max - } - return x -} - func AsJson(i interface{}) string { bytes, _ := json.MarshalIndent(i, "", " ") return string(bytes)