Refactor f() to a dedicated function colourHash()

Also enable colourHash() for yogurt mode.
This commit is contained in:
morganamilo 2018-03-02 01:02:56 +00:00
parent 3109ae759e
commit 181663b7cb
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
2 changed files with 15 additions and 14 deletions

View file

@ -41,7 +41,7 @@ func (q aurQuery) printSearch(start int) {
fmt.Println(res.Name)
continue
}
toprint += boldWhiteFg("aur/") + boldYellowFg(res.Name) +
toprint += colourHash("aur") + "/" + boldYellowFg(res.Name) +
" " + boldCyanFg(res.Version) +
" (" + strconv.Itoa(res.NumVotes) + ") "
@ -75,7 +75,7 @@ func (s repoQuery) printSearch() {
fmt.Println(res.Name())
continue
}
toprint += boldWhiteFg(res.DB().Name()+"/") + boldYellowFg(res.Name()) +
toprint += colourHash(res.DB().Name()) + "/" + boldYellowFg(res.Name()) +
" " + boldCyanFg(res.Version()) + " "
if len(res.Groups().Slice()) != 0 {
@ -85,7 +85,7 @@ func (s repoQuery) printSearch() {
localDb, err := alpmHandle.LocalDb()
if err == nil {
if _, err = localDb.PkgByName(res.Name()); err == nil {
toprint += greenFgBlackBg("Installed")
toprint += greenFgBlueBg("Installed")
}
}
@ -417,3 +417,14 @@ func boldYellowFgBlackBg(in string) string {
return in
}
func colourHash(name string) (output string) {
if alpmConf.Options&alpm.ConfColor == 0 {
return name
}
var hash = 5381
for i := 0; i < len(name); i++ {
hash = int(name[i]) + ((hash << 5) + (hash))
}
return fmt.Sprintf("\x1b[1;%dm%s\x1b[0m", hash%6+31, name)
}

View file

@ -85,18 +85,8 @@ func (u upSlice) Print(start int) {
for k, i := range u {
left, right := getVersionDiff(i.LocalVersion, i.RemoteVersion)
f := func(name string) (output string) {
if alpmConf.Options&alpm.ConfColor == 0 {
return name
}
var hash = 5381
for i := 0; i < len(name); i++ {
hash = int(name[i]) + ((hash << 5) + (hash))
}
return fmt.Sprintf("\x1b[1;%dm%s\x1b[0m", hash%6+31, name)
}
fmt.Print(yellowFg(fmt.Sprintf("%2d ", len(u)+start-k-1)))
fmt.Print(f(i.Repository), "/", boldWhiteFg(i.Name))
fmt.Print(colourHash(i.Repository), "/", boldWhiteFg(i.Name))
w := 70 - len(i.Repository) - len(i.Name) + len(left)
fmt.Printf(fmt.Sprintf("%%%ds", w),