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

This commit is contained in:
jguer 2020-06-26 09:03:45 +02:00
parent 9d93a1f1bd
commit d0450179f9
No known key found for this signature in database
GPG Key ID: 6D6CC9BEA8556B35
6 changed files with 36 additions and 45 deletions

View File

@ -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

View File

@ -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")

View File

@ -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
pkg/text/convert.go Normal file
View File

@ -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")
}

View File

@ -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()

View File

@ -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