1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-27 12:32:37 +02:00

Replace min/max helpers with built-in min/max (#3482)

- **PR Description**

We upgraded our minimum Go version to 1.21 in commit
57ac9c2189458a7f0e63c2e9cac8334694a3d545. We can now replace our
`utils.Min` and `utils.Max` functions with the built-in `min` and `max`.

Reference: https://go.dev/ref/spec#Min_and_max

- **Please check if the PR fulfills these requirements**

* [x] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [ ] Tests have been added/updated (see
[here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md)
for the integration test guide)
* [ ] Text is internationalised (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
* [ ] Docs (specifically `docs/Config.md`) have been updated if
necessary
* [x] You've read through your own file changes for silly mistakes etc

<!--
Be sure to name your PR with an imperative e.g. 'Add worktrees view'
see https://github.com/jesseduffield/lazygit/releases/tag/v0.40.0 for
examples
-->
This commit is contained in:
Jesse Duffield 2024-04-08 09:31:14 +10:00 committed by GitHub
commit 426375b7cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 20 additions and 73 deletions

View File

@ -457,9 +457,9 @@ func (self *LocalCommitsController) drop(selectedCommits []*models.Commit, start
} }
if selectedIdx > rangeStartIdx { if selectedIdx > rangeStartIdx {
selectedIdx = utils.Max(selectedIdx-len(updateRefTodos), rangeStartIdx) selectedIdx = max(selectedIdx-len(updateRefTodos), rangeStartIdx)
} else { } else {
rangeStartIdx = utils.Max(rangeStartIdx-len(updateRefTodos), selectedIdx) rangeStartIdx = max(rangeStartIdx-len(updateRefTodos), selectedIdx)
} }
self.context().SetSelectionRangeAndMode(selectedIdx, rangeStartIdx, rangeSelectMode) self.context().SetSelectionRangeAndMode(selectedIdx, rangeStartIdx, rangeSelectMode)

View File

@ -3,7 +3,6 @@ package controllers
import ( import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
// To be called after pressing up-arrow; checks whether the cursor entered the // To be called after pressing up-arrow; checks whether the cursor entered the
@ -39,7 +38,7 @@ func calculateLinesToScrollUp(viewPortStart int, viewPortHeight int, scrollOffMa
// a very large value to keep the cursor always in the middle of the screen. // a very large value to keep the cursor always in the middle of the screen.
// Use +.5 so that if the height is even, the top margin is one line higher // Use +.5 so that if the height is even, the top margin is one line higher
// than the bottom margin. // than the bottom margin.
scrollOffMargin = utils.Min(scrollOffMargin, int((float64(viewPortHeight)+.5)/2)) scrollOffMargin = min(scrollOffMargin, int((float64(viewPortHeight)+.5)/2))
// Scroll only if the "before" position was visible (this could be false if // Scroll only if the "before" position was visible (this could be false if
// the scroll wheel was used to scroll the selected line out of view) ... // the scroll wheel was used to scroll the selected line out of view) ...
@ -59,7 +58,7 @@ func calculateLinesToScrollDown(viewPortStart int, viewPortHeight int, scrollOff
// a very large value to keep the cursor always in the middle of the screen. // a very large value to keep the cursor always in the middle of the screen.
// Use -.5 so that if the height is even, the bottom margin is one line lower // Use -.5 so that if the height is even, the bottom margin is one line lower
// than the top margin. // than the top margin.
scrollOffMargin = utils.Min(scrollOffMargin, int((float64(viewPortHeight)-.5)/2)) scrollOffMargin = min(scrollOffMargin, int((float64(viewPortHeight)-.5)/2))
// Scroll only if the "before" position was visible (this could be false if // Scroll only if the "before" position was visible (this could be false if
// the scroll wheel was used to scroll the selected line out of view) ... // the scroll wheel was used to scroll the selected line out of view) ...

View File

@ -5,7 +5,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -140,7 +139,7 @@ func (self *FileTree) GetAllItems() []*FileNode {
func (self *FileTree) Len() int { func (self *FileTree) Len() int {
// -1 because we're ignoring the root // -1 because we're ignoring the root
return utils.Max(self.tree.Size(self.collapsedPaths)-1, 0) return max(self.tree.Size(self.collapsedPaths)-1, 0)
} }
func (self *FileTree) GetItem(index int) types.HasUrn { func (self *FileTree) GetItem(index int) types.HasUrn {

View File

@ -145,7 +145,7 @@ func (gui *Gui) handleCopySelectedSideContextItemToClipboardWithTruncation(maxWi
} }
if maxWidth > 0 { if maxWidth > 0 {
itemId = itemId[:utils.Min(len(itemId), maxWidth)] itemId = itemId[:min(len(itemId), maxWidth)]
} }
gui.c.LogAction(gui.c.Tr.Actions.CopyToClipboard) gui.c.LogAction(gui.c.Tr.Actions.CopyToClipboard)

View File

@ -5,7 +5,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
// note: items option is mutated by this function // note: items option is mutated by this function
@ -31,7 +30,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
item.LabelColumns[0] = fmt.Sprintf("%s...", item.LabelColumns[0]) item.LabelColumns[0] = fmt.Sprintf("%s...", item.LabelColumns[0])
} }
maxColumnSize = utils.Max(maxColumnSize, len(item.LabelColumns)) maxColumnSize = max(maxColumnSize, len(item.LabelColumns))
} }
for _, item := range opts.Items { for _, item := range opts.Items {

View File

@ -1,7 +1,5 @@
package patch_exploring package patch_exploring
import "github.com/jesseduffield/lazygit/pkg/utils"
func calculateOrigin(currentOrigin int, bufferHeight int, numLines int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int { func calculateOrigin(currentOrigin int, bufferHeight int, numLines int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode) needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode)
@ -14,7 +12,7 @@ func calculateOrigin(currentOrigin int, bufferHeight int, numLines int, firstLin
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, numLines int, needToSeeIdx int, wantToSeeIdx int) int { func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, numLines int, needToSeeIdx int, wantToSeeIdx int) int {
origin := currentOrigin origin := currentOrigin
if needToSeeIdx < currentOrigin || needToSeeIdx > currentOrigin+bufferHeight { if needToSeeIdx < currentOrigin || needToSeeIdx > currentOrigin+bufferHeight {
origin = utils.Max(utils.Min(needToSeeIdx-bufferHeight/2, numLines-bufferHeight-1), 0) origin = max(min(needToSeeIdx-bufferHeight/2, numLines-bufferHeight-1), 0)
} }
bottom := origin + bufferHeight bottom := origin + bufferHeight
@ -22,11 +20,11 @@ func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight in
if wantToSeeIdx < origin { if wantToSeeIdx < origin {
requiredChange := origin - wantToSeeIdx requiredChange := origin - wantToSeeIdx
allowedChange := bottom - needToSeeIdx allowedChange := bottom - needToSeeIdx
return origin - utils.Min(requiredChange, allowedChange) return origin - min(requiredChange, allowedChange)
} else if wantToSeeIdx > origin+bufferHeight { } else if wantToSeeIdx > origin+bufferHeight {
requiredChange := wantToSeeIdx - bottom requiredChange := wantToSeeIdx - bottom
allowedChange := needToSeeIdx - origin allowedChange := needToSeeIdx - origin
return origin + utils.Min(requiredChange, allowedChange) return origin + min(requiredChange, allowedChange)
} else { } else {
return origin return origin
} }

View File

@ -79,9 +79,9 @@ func getBranchDisplayStrings(
} }
// Don't bother shortening branch names that are already 3 characters or less // Don't bother shortening branch names that are already 3 characters or less
if len(displayName) > utils.Max(availableWidth, 3) { if len(displayName) > max(availableWidth, 3) {
// Never shorten the branch name to less then 3 characters // Never shorten the branch name to less then 3 characters
len := utils.Max(availableWidth, 4) len := max(availableWidth, 4)
displayName = displayName[:len-1] + "…" displayName = displayName[:len-1] + "…"
} }
coloredName := nameTextStyle.Sprint(displayName) coloredName := nameTextStyle.Sprint(displayName)

View File

@ -69,7 +69,7 @@ func GetCommitListDisplayStrings(
} }
// this is where my non-TODO commits begin // this is where my non-TODO commits begin
rebaseOffset := utils.Min(indexOfFirstNonTODOCommit(commits), endIdx) rebaseOffset := min(indexOfFirstNonTODOCommit(commits), endIdx)
filteredCommits := commits[startIdx:endIdx] filteredCommits := commits[startIdx:endIdx]
@ -80,11 +80,11 @@ func GetCommitListDisplayStrings(
if showGraph { if showGraph {
// this is where the graph begins (may be beyond the TODO commits depending on startIdx, // this is where the graph begins (may be beyond the TODO commits depending on startIdx,
// but we'll never include TODO commits as part of the graph because it'll be messy) // but we'll never include TODO commits as part of the graph because it'll be messy)
graphOffset := utils.Max(startIdx, rebaseOffset) graphOffset := max(startIdx, rebaseOffset)
pipeSets := loadPipesets(commits[rebaseOffset:]) pipeSets := loadPipesets(commits[rebaseOffset:])
pipeSetOffset := utils.Max(startIdx-rebaseOffset, 0) pipeSetOffset := max(startIdx-rebaseOffset, 0)
graphPipeSets := pipeSets[pipeSetOffset:utils.Max(endIdx-rebaseOffset, 0)] graphPipeSets := pipeSets[pipeSetOffset:max(endIdx-rebaseOffset, 0)]
graphCommits := commits[graphOffset:endIdx] graphCommits := commits[graphOffset:endIdx]
graphLines := graph.RenderAux( graphLines := graph.RenderAux(
graphPipeSets, graphPipeSets,

View File

@ -42,11 +42,11 @@ func ContainsCommitSha(pipes []*Pipe, sha string) bool {
} }
func (self Pipe) left() int { func (self Pipe) left() int {
return utils.Min(self.fromPos, self.toPos) return min(self.fromPos, self.toPos)
} }
func (self Pipe) right() int { func (self Pipe) right() int {
return utils.Max(self.fromPos, self.toPos) return max(self.fromPos, self.toPos)
} }
func RenderCommitGraph(commits []*models.Commit, selectedCommitSha string, getStyle func(c *models.Commit) style.TextStyle) []string { func RenderCommitGraph(commits []*models.Commit, selectedCommitSha string, getStyle func(c *models.Commit) style.TextStyle) []string {
@ -390,7 +390,7 @@ func equalHashes(a, b string) bool {
return false return false
} }
length := utils.Min(len(a), len(b)) length := min(len(a), len(b))
// parent hashes are only stored up to 20 characters for some reason so we'll truncate to that for comparison // parent hashes are only stored up to 20 characters for some reason so we'll truncate to that for comparison
return a[:length] == b[:length] return a[:length] == b[:length]
} }

View File

@ -9,8 +9,6 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"time" "time"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
// this is for running shell commands, mostly for the sake of setting up the repo // this is for running shell commands, mostly for the sake of setting up the repo
@ -289,7 +287,7 @@ func (self *Shell) CreateRepoHistory() *Shell {
// Choose a random commit within the last 20 commits on the master branch // Choose a random commit within the last 20 commits on the master branch
lastMasterCommit := totalCommits - 1 lastMasterCommit := totalCommits - 1
commitOffset := rand.Intn(utils.Min(lastMasterCommit, 5)) + 1 commitOffset := rand.Intn(min(lastMasterCommit, 5)) + 1
// Create the feature branch and checkout the chosen commit // Create the feature branch and checkout the chosen commit
self.NewBranchFrom(branchName, fmt.Sprintf("master~%d", commitOffset)) self.NewBranchFrom(branchName, fmt.Sprintf("master~%d", commitOffset))

View File

@ -32,21 +32,6 @@ func Loader(now time.Time, config config.SpinnerConfig) string {
return config.Frames[index] return config.Frames[index]
} }
// Min returns the minimum of two integers
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}
func SortRange(x int, y int) (int, int) { func SortRange(x int, y int) (int, int) {
if x < y { if x < y {
return x, y return x, y

View File

@ -6,37 +6,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestMin is a function.
func TestMin(t *testing.T) {
type scenario struct {
a int
b int
expected int
}
scenarios := []scenario{
{
1,
1,
1,
},
{
1,
2,
1,
},
{
2,
1,
1,
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, Min(s.a, s.b))
}
}
func TestAsJson(t *testing.T) { func TestAsJson(t *testing.T) {
type myStruct struct { type myStruct struct {
a string a string