瀏覽代碼

fix(usecolor): usecolor belongs to text. remove deprecated

jguer 4 年之前
父節點
當前提交
d0450179f9
共有 6 個文件被更改,包括 36 次插入45 次删除
  1. 0 3
      config.go
  2. 1 1
      install.go
  3. 4 4
      main.go
  4. 16 0
      pkg/text/convert.go
  5. 9 35
      print.go
  6. 6 2
      upgrade_test.go

+ 0 - 3
config.go

@@ -94,9 +94,6 @@ const configFileName string = "config.json"
 // vcsFileName holds the name of the vcs file.
 const vcsFileName string = "vcs.json"
 
-// useColor enables/disables colored printing
-var useColor bool
-
 // configHome handles config directory home
 var configHome string
 

+ 1 - 1
install.go

@@ -755,7 +755,7 @@ func showPkgbuildDiffs(bases []Base, cloned stringset.StringSet) error {
 		args := []string{"diff",
 			start + "..HEAD@{upstream}", "--src-prefix",
 			dir + "/", "--dst-prefix", dir + "/", "--", ".", ":(exclude).SRCINFO"}
-		if useColor {
+		if text.UseColor {
 			args = append(args, "--color=always")
 		} else {
 			args = append(args, "--color=never")

+ 4 - 4
main.go

@@ -165,13 +165,13 @@ func initAlpm() error {
 
 	switch value, _, _ := cmdArgs.getArg("color"); value {
 	case "always":
-		useColor = true
+		text.UseColor = true
 	case "auto":
-		useColor = isTty()
+		text.UseColor = isTty()
 	case "never":
-		useColor = false
+		text.UseColor = false
 	default:
-		useColor = pacmanConf.Color && isTty()
+		text.UseColor = pacmanConf.Color && isTty()
 	}
 
 	return nil

+ 16 - 0
pkg/text/convert.go

@@ -0,0 +1,16 @@
+package text
+
+import "fmt"
+
+// Human method returns results in human readable format.
+func Human(size int64) string {
+	floatsize := float32(size)
+	units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
+	for _, unit := range units {
+		if floatsize < 1024 {
+			return fmt.Sprintf("%.1f %sB", floatsize, unit)
+		}
+		floatsize /= 1024
+	}
+	return fmt.Sprintf("%d%s", size, "B")
+}

+ 9 - 35
print.go

@@ -49,19 +49,6 @@ func (warnings *aurWarnings) print() {
 	}
 }
 
-// human method returns results in human readable format.
-func human(size int64) string {
-	floatsize := float32(size)
-	units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
-	for _, unit := range units {
-		if floatsize < 1024 {
-			return fmt.Sprintf("%.1f %sB", floatsize, unit)
-		}
-		floatsize /= 1024
-	}
-	return fmt.Sprintf("%d%s", size, "B")
-}
-
 // PrintSearch handles printing search results in a given format
 func (q aurQuery) printSearch(start int) {
 	localDB, _ := alpmHandle.LocalDB()
@@ -82,7 +69,7 @@ func (q aurQuery) printSearch(start int) {
 			continue
 		}
 
-		toprint += bold(colorHash("aur")) + "/" + bold(q[i].Name) +
+		toprint += bold(text.ColorHash("aur")) + "/" + bold(q[i].Name) +
 			" " + cyan(q[i].Version) +
 			bold(" (+"+strconv.Itoa(q[i].NumVotes)) +
 			" " + bold(strconv.FormatFloat(q[i].Popularity, 'f', 2, 64)+") ")
@@ -125,10 +112,10 @@ func (s repoQuery) printSearch() {
 			continue
 		}
 
-		toprint += bold(colorHash(res.DB().Name())) + "/" + bold(res.Name()) +
+		toprint += bold(text.ColorHash(res.DB().Name())) + "/" + bold(res.Name()) +
 			" " + cyan(res.Version()) +
-			bold(" ("+human(res.Size())+
-				" "+human(res.ISize())+") ")
+			bold(" ("+text.Human(res.Size())+
+				" "+text.Human(res.ISize())+") ")
 
 		if len(res.Groups().Slice()) != 0 {
 			toprint += fmt.Sprint(res.Groups().Slice(), " ")
@@ -169,8 +156,8 @@ func (b Base) String() string {
 	return str
 }
 
-func (u upgrade) StylizedNameWithRepository() string {
-	return bold(colorHash(u.Repository)) + "/" + bold(u.Name)
+func (u *upgrade) StylizedNameWithRepository() string {
+	return bold(text.ColorHash(u.Repository)) + "/" + bold(u.Name)
 }
 
 // Print prints the details of the packages to upgrade.
@@ -334,7 +321,7 @@ func biggestPackages() {
 	}
 
 	for i := 0; i < 10; i++ {
-		fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(human(pkgS[i].ISize())))
+		fmt.Printf("%s: %s\n", bold(pkgS[i].Name()), cyan(text.Human(pkgS[i].ISize())))
 	}
 	// Could implement size here as well, but we just want the general idea
 }
@@ -356,7 +343,7 @@ func localStatistics() error {
 	text.Infoln(gotext.Get("Total installed packages: %s", cyan(strconv.Itoa(info.Totaln))))
 	text.Infoln(gotext.Get("Total foreign installed packages: %s", cyan(strconv.Itoa(len(remoteNames)))))
 	text.Infoln(gotext.Get("Explicitly installed packages: %s", cyan(strconv.Itoa(info.Expln))))
-	text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(human(info.TotalSize))))
+	text.Infoln(gotext.Get("Total Size occupied by packages: %s", cyan(text.Human(info.TotalSize))))
 	fmt.Println(bold(cyan("===========================================")))
 	text.Infoln(gotext.Get("Ten biggest packages:"))
 	biggestPackages()
@@ -561,7 +548,7 @@ const (
 )
 
 func stylize(startCode, in string) string {
-	if useColor {
+	if text.UseColor {
 		return startCode + in + resetCode
 	}
 
@@ -596,19 +583,6 @@ func bold(in string) string {
 	return stylize(boldCode, in)
 }
 
-// Colors text using a hashing algorithm. The same text will always produce the
-// same color while different text will produce a different color.
-func colorHash(name string) (output string) {
-	if !useColor {
-		return name
-	}
-	var hash uint = 5381
-	for i := 0; i < len(name); i++ {
-		hash = uint(name[i]) + ((hash << 5) + (hash))
-	}
-	return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
-}
-
 func providerMenu(dep string, providers providers) *rpc.Pkg {
 	size := providers.Len()
 

+ 6 - 2
upgrade_test.go

@@ -1,9 +1,13 @@
 package main
 
-import "testing"
+import (
+	"testing"
+
+	"github.com/Jguer/yay/v10/pkg/text"
+)
 
 func TestGetVersionDiff(t *testing.T) {
-	useColor = true
+	text.UseColor = true
 
 	type versionPair struct {
 		Old string