mirror of
https://github.com/Jguer/yay.git
synced 2025-12-27 10:01:53 -05:00
refactor(upgrade): optimize code and improve maintainability (#2600)
* refactor(upgrade): reduce code duplication in Print methods and fix typo in Filter documentation * refactor(upgrade): optimize UserExcludeUpgrades with early return and simplify GraphUpgrades function
This commit is contained in:
@@ -245,10 +245,6 @@ func (u *UpgradeService) GraphUpgrades(ctx context.Context,
|
||||
return graph, err
|
||||
}
|
||||
|
||||
if graph.Len() == 0 {
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
@@ -306,6 +302,11 @@ func (u *UpgradeService) UserExcludeUpgrades(graph *topo.Graph[string, *dep.Inst
|
||||
// true if user doesn't want to include specific repositories/packages
|
||||
noIncludes := len(include) == 0 && otherInclude.Cardinality() == 0
|
||||
|
||||
// No exclusions or inclusions specified, return early
|
||||
if noIncludes && len(exclude) == 0 && otherExclude.Cardinality() == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
excluded := make([]string, 0)
|
||||
for i := range allUp.Up {
|
||||
up := &allUp.Up[i]
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/Jguer/yay/v12/pkg/text"
|
||||
)
|
||||
|
||||
// Filter decides if specific package should be included in theincluded in the results.
|
||||
// Filter decides if specific package should be included in the results.
|
||||
type Filter func(*Upgrade) bool
|
||||
|
||||
// Upgrade type describes a system upgrade.
|
||||
@@ -51,12 +51,12 @@ func (u UpSlice) Less(i, j int) bool {
|
||||
return text.LessRunes(iRunes, jRunes)
|
||||
}
|
||||
|
||||
// Print prints the details of the packages to upgrade.
|
||||
func (u UpSlice) Print(logger *text.Logger) {
|
||||
// calculateFormatting calculates formatting parameters for printing upgrades
|
||||
func calculateFormatting(upgrades []Upgrade) (int, int, int) {
|
||||
longestName, longestVersion := 0, 0
|
||||
|
||||
for k := range u.Up {
|
||||
upgrade := &u.Up[k]
|
||||
for i := range upgrades {
|
||||
upgrade := &upgrades[i]
|
||||
packNameLen := len(StylizedNameWithRepository(upgrade))
|
||||
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
|
||||
packVersionLen := len(packVersion)
|
||||
@@ -64,8 +64,16 @@ func (u UpSlice) Print(logger *text.Logger) {
|
||||
longestVersion = max(packVersionLen, longestVersion)
|
||||
}
|
||||
|
||||
lenUp := len(u.Up)
|
||||
lenUp := len(upgrades)
|
||||
longestNumber := len(fmt.Sprintf("%v", lenUp))
|
||||
|
||||
return longestName, longestVersion, longestNumber
|
||||
}
|
||||
|
||||
// Print prints the details of the packages to upgrade.
|
||||
func (u UpSlice) Print(logger *text.Logger) {
|
||||
longestName, longestVersion, longestNumber := calculateFormatting(u.Up)
|
||||
|
||||
namePadding := fmt.Sprintf("%%-%ds ", longestName)
|
||||
versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
|
||||
numberPadding := fmt.Sprintf("%%%dd ", longestNumber)
|
||||
@@ -74,10 +82,8 @@ func (u UpSlice) Print(logger *text.Logger) {
|
||||
upgrade := &u.Up[k]
|
||||
left, right := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
|
||||
|
||||
logger.Printf(text.Magenta(fmt.Sprintf(numberPadding, lenUp-k)))
|
||||
|
||||
logger.Printf(text.Magenta(fmt.Sprintf(numberPadding, len(u.Up)-k)))
|
||||
logger.Printf(namePadding, StylizedNameWithRepository(upgrade))
|
||||
|
||||
logger.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
|
||||
if upgrade.Extra != "" {
|
||||
logger.Println(strings.Repeat(" ", longestNumber), upgrade.Extra)
|
||||
@@ -86,19 +92,8 @@ func (u UpSlice) Print(logger *text.Logger) {
|
||||
}
|
||||
|
||||
func (u UpSlice) PrintDeps(logger *text.Logger) {
|
||||
longestName, longestVersion := 0, 0
|
||||
longestName, longestVersion, longestNumber := calculateFormatting(u.PulledDeps)
|
||||
|
||||
for k := range u.PulledDeps {
|
||||
upgrade := &u.PulledDeps[k]
|
||||
packNameLen := len(StylizedNameWithRepository(upgrade))
|
||||
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
|
||||
packVersionLen := len(packVersion)
|
||||
longestName = max(packNameLen, longestName)
|
||||
longestVersion = max(packVersionLen, longestVersion)
|
||||
}
|
||||
|
||||
lenUp := len(u.PulledDeps)
|
||||
longestNumber := len(fmt.Sprintf("%v", lenUp))
|
||||
namePadding := fmt.Sprintf(" %s%%-%ds ", strings.Repeat(" ", longestNumber), longestName)
|
||||
versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user