Browse Source

fix(clean_menu): optimize any base installed

jguer 3 years ago
parent
commit
f3c3e2e4d4
3 changed files with 17 additions and 17 deletions
  1. 1 5
      clean_menu.go
  2. 2 11
      install.go
  3. 14 1
      pkg/dep/base.go

+ 1 - 5
clean_menu.go

@@ -36,11 +36,7 @@ func cleanNumberMenu(bases []dep.Base, installed stringset.StringSet) ([]dep.Bas
 	if !cOtherInclude.Get("n") && !cOtherInclude.Get("none") {
 		for i, base := range bases {
 			pkg := base.Pkgbase()
-			anyInstalled := false
-
-			for _, b := range base {
-				anyInstalled = anyInstalled || installed.Get(b.Name)
-			}
+			anyInstalled := base.AnyIsInSet(installed)
 
 			dir := filepath.Join(config.BuildDir, pkg)
 			if _, err := os.Stat(dir); os.IsNotExist(err) {

+ 2 - 11
install.go

@@ -564,12 +564,7 @@ func pkgbuildNumberMenu(bases []dep.Base, installed stringset.StringSet) {
 		toPrint += fmt.Sprintf(text.Magenta("%3d")+" %-40s", len(bases)-n,
 			text.Bold(base.String()))
 
-		anyInstalled := false
-		for _, b := range base {
-			anyInstalled = anyInstalled || installed.Get(b.Name)
-		}
-
-		if anyInstalled {
+		if base.AnyIsInSet(installed) {
 			toPrint += text.Bold(text.Green(gotext.Get(" (Installed)")))
 		}
 
@@ -625,11 +620,7 @@ func editDiffNumberMenu(bases []dep.Base, installed stringset.StringSet, diff bo
 	if !eOtherInclude.Get("n") && !eOtherInclude.Get("none") {
 		for i, base := range bases {
 			pkg := base.Pkgbase()
-			anyInstalled := false
-
-			for _, b := range base {
-				anyInstalled = anyInstalled || installed.Get(b.Name)
-			}
+			anyInstalled := base.AnyIsInSet(installed)
 
 			if !eIsInclude && eExclude.Get(len(bases)-i) {
 				continue

+ 14 - 1
pkg/dep/base.go

@@ -1,6 +1,9 @@
 package dep
 
-import aur "github.com/Jguer/yay/v11/pkg/query"
+import (
+	aur "github.com/Jguer/yay/v11/pkg/query"
+	"github.com/Jguer/yay/v11/pkg/stringset"
+)
 
 // Base is an AUR base package.
 type Base []*aur.Pkg
@@ -20,6 +23,16 @@ func (b Base) URLPath() string {
 	return b[0].URLPath
 }
 
+func (b Base) AnyIsInSet(set stringset.StringSet) bool {
+	for _, pkg := range b {
+		if set.Get(pkg.Name) {
+			return true
+		}
+	}
+
+	return false
+}
+
 // Packages foo and bar from a pkgbase named base would print like so:
 // base (foo bar).
 func (b Base) String() string {