Skip to content

Commit

Permalink
Use Go 1.21's min/max built-ins (#2405)
Browse files Browse the repository at this point in the history
This simplifies the code compared to either rolling our own or awkwardly
using math's float functions with integers.
  • Loading branch information
JRaspass authored Mar 23, 2024
1 parent a1d530c commit 6c23305
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
22 changes: 2 additions & 20 deletions pkg/intrange/intrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ func (rs IntRanges) Get(n int) bool {
return false
}

// Min returns min value between a and b.
func Min(a, b int) int {
if a < b {
return a
}

return b
}

// Max returns max value between a and b.
func Max(a, b int) int {
if a < b {
return b
}

return a
}

// ParseNumberMenu parses input for number menus split by spaces or commas
// supports individual selection: 1 2 3 4
// supports range selections: 1-4 10-20
Expand Down Expand Up @@ -116,8 +98,8 @@ func ParseNumberMenu(input string) (include, exclude IntRanges,
num2 = num1
}

mi := Min(num1, num2)
ma := Max(num1, num2)
mi := min(num1, num2)
ma := max(num1, num2)

if !invert {
include = append(include, makeIntRange(mi, ma))
Expand Down
3 changes: 1 addition & 2 deletions pkg/upgrade/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package upgrade
import (
"context"
"fmt"
"math"
"sort"
"strings"

Expand Down Expand Up @@ -195,7 +194,7 @@ func (u *UpgradeService) graphToUpSlice(graph *topo.Graph[string, *dep.InstallIn
parents := graph.ImmediateDependencies(name)
extra := ""
if len(parents) > 0 && !info.Upgrade && info.Reason == dep.MakeDep {
reducedParents := parents.Slice()[:int(math.Min(cutOffExtra, float64(len(parents))))]
reducedParents := parents.Slice()[:min(cutOffExtra, len(parents))]
if len(parents) > cutOffExtra {
reducedParents = append(reducedParents, "...")
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/intrange"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/text"
)
Expand Down Expand Up @@ -61,8 +60,8 @@ func (u UpSlice) Print(logger *text.Logger) {
packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion)
longestName = max(packNameLen, longestName)
longestVersion = max(packVersionLen, longestVersion)
}

lenUp := len(u.Up)
Expand Down Expand Up @@ -94,8 +93,8 @@ func (u UpSlice) PrintDeps(logger *text.Logger) {
packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion)
longestName = max(packNameLen, longestName)
longestVersion = max(packVersionLen, longestVersion)
}

lenUp := len(u.PulledDeps)
Expand Down

0 comments on commit 6c23305

Please sign in to comment.