Use Go 1.21's min/max built-ins (#2405)

This simplifies the code compared to either rolling our own or awkwardly
using math's float functions with integers.
This commit is contained in:
James Raspass 2024-03-23 22:27:30 +00:00 committed by GitHub
parent a1d530cbf4
commit 6c2330528f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 27 deletions

View File

@ -42,24 +42,6 @@ func (rs IntRanges) Get(n int) bool {
return false 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 // ParseNumberMenu parses input for number menus split by spaces or commas
// supports individual selection: 1 2 3 4 // supports individual selection: 1 2 3 4
// supports range selections: 1-4 10-20 // supports range selections: 1-4 10-20
@ -116,8 +98,8 @@ func ParseNumberMenu(input string) (include, exclude IntRanges,
num2 = num1 num2 = num1
} }
mi := Min(num1, num2) mi := min(num1, num2)
ma := Max(num1, num2) ma := max(num1, num2)
if !invert { if !invert {
include = append(include, makeIntRange(mi, ma)) include = append(include, makeIntRange(mi, ma))

View File

@ -3,7 +3,6 @@ package upgrade
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"sort" "sort"
"strings" "strings"
@ -195,7 +194,7 @@ func (u *UpgradeService) graphToUpSlice(graph *topo.Graph[string, *dep.InstallIn
parents := graph.ImmediateDependencies(name) parents := graph.ImmediateDependencies(name)
extra := "" extra := ""
if len(parents) > 0 && !info.Upgrade && info.Reason == dep.MakeDep { 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 { if len(parents) > cutOffExtra {
reducedParents = append(reducedParents, "...") reducedParents = append(reducedParents, "...")
} }

View File

@ -5,7 +5,6 @@ import (
"strings" "strings"
"github.com/Jguer/yay/v12/pkg/db" "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/query"
"github.com/Jguer/yay/v12/pkg/text" "github.com/Jguer/yay/v12/pkg/text"
) )
@ -61,8 +60,8 @@ func (u UpSlice) Print(logger *text.Logger) {
packNameLen := len(StylizedNameWithRepository(upgrade)) packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion) packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion) packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName) longestName = max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion) longestVersion = max(packVersionLen, longestVersion)
} }
lenUp := len(u.Up) lenUp := len(u.Up)
@ -94,8 +93,8 @@ func (u UpSlice) PrintDeps(logger *text.Logger) {
packNameLen := len(StylizedNameWithRepository(upgrade)) packNameLen := len(StylizedNameWithRepository(upgrade))
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion) packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
packVersionLen := len(packVersion) packVersionLen := len(packVersion)
longestName = intrange.Max(packNameLen, longestName) longestName = max(packNameLen, longestName)
longestVersion = intrange.Max(packVersionLen, longestVersion) longestVersion = max(packVersionLen, longestVersion)
} }
lenUp := len(u.PulledDeps) lenUp := len(u.PulledDeps)