Skip to content

Commit a3ae1c8

Browse files
committed
Refactor: extract function clampLineIdx
And reimplement using lo.Clamp instead of comparing manually.
1 parent 61636d8 commit a3ae1c8

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Diff for: pkg/gui/patch_exploring/state.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/jesseduffield/gocui"
88
"github.com/jesseduffield/lazygit/pkg/commands/patch"
99
"github.com/jesseduffield/lazygit/pkg/utils"
10+
"github.com/samber/lo"
1011
)
1112

1213
// State represents the current state of the patch explorer context i.e. when
@@ -177,15 +178,13 @@ func (s *State) SelectLine(newSelectedLineIdx int) {
177178
s.selectLineWithoutRangeCheck(newSelectedLineIdx)
178179
}
179180

181+
func (s *State) clampLineIdx(lineIdx int) int {
182+
return lo.Clamp(lineIdx, 0, len(s.patchLineIndices)-1)
183+
}
184+
180185
// This just moves the cursor without caring about range select
181186
func (s *State) selectLineWithoutRangeCheck(newSelectedLineIdx int) {
182-
if newSelectedLineIdx < 0 {
183-
newSelectedLineIdx = 0
184-
} else if newSelectedLineIdx > len(s.patchLineIndices)-1 {
185-
newSelectedLineIdx = len(s.patchLineIndices) - 1
186-
}
187-
188-
s.selectedLineIdx = newSelectedLineIdx
187+
s.selectedLineIdx = s.clampLineIdx(newSelectedLineIdx)
189188
}
190189

191190
func (s *State) SelectNewLineForRange(newSelectedLineIdx int) {

0 commit comments

Comments
 (0)