From d0450179f9e11cb38aaa9e6cc6624e8b197f726b Mon Sep 17 00:00:00 2001 From: jguer Date: Fri, 26 Jun 2020 09:03:45 +0200 Subject: [PATCH] fix(usecolor): usecolor belongs to text. remove deprecated --- config.go | 3 --- install.go | 2 +- main.go | 8 ++++---- pkg/text/convert.go | 16 ++++++++++++++++ print.go | 44 +++++++++----------------------------------- upgrade_test.go | 8 ++++++-- 6 files changed, 36 insertions(+), 45 deletions(-) create mode 100644 pkg/text/convert.go diff --git a/config.go b/config.go index aa8a5cd0..88161ab9 100644 --- a/config.go +++ b/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 diff --git a/install.go b/install.go index 8a5ce71f..246e1a23 100644 --- a/install.go +++ b/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") diff --git a/main.go b/main.go index 9b8112fc..d9b78258 100644 --- a/main.go +++ b/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 diff --git a/pkg/text/convert.go b/pkg/text/convert.go new file mode 100644 index 00000000..92fae2f9 --- /dev/null +++ b/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") +} diff --git a/print.go b/print.go index 3ace8db0..9a16487c 100644 --- a/print.go +++ b/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() diff --git a/upgrade_test.go b/upgrade_test.go index d5d97285..df9253eb 100644 --- a/upgrade_test.go +++ b/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