Highlight diff between old and new versions better

Split by dots, pluses, dashes, tilds, etc., not only `Version` and `Pkgrel`.

Don't make new version different bold.

Inspired by [`pikaur`](https://github.com/actionless/pikaur).
This commit is contained in:
Alexander Popov 2018-04-05 15:33:31 +03:00
parent 922a591b64
commit 46cffa6ba6
No known key found for this signature in database
GPG key ID: 9CF7623B5A63EDAA

View file

@ -5,6 +5,7 @@ import (
"sort"
"strings"
"sync"
"unicode"
alpm "github.com/jguer/go-alpm"
rpc "github.com/mikkeloscar/aur"
@ -74,13 +75,50 @@ func getVersionDiff(oldVersion, newversion string) (left, right string) {
}
if errOld == nil && errNew == nil {
if old.Version == new.Version {
left = string(old.Version) + "-" + red(string(old.Pkgrel))
right = string(new.Version) + "-" + green(string(new.Pkgrel))
} else {
left = red(string(old.Version)) + "-" + string(old.Pkgrel)
right = bold(green(string(new.Version))) + "-" + string(new.Pkgrel)
oldVersion := old.String()
newVersion := new.String()
if oldVersion == newVersion {
return oldVersion, newVersion
}
diffPosition := 0
checkWords := func(str string, index int, words ...string) bool {
for _, word := range words {
wordLength := len(word)
nextIndex := index + 1
if (index < len(str)-wordLength) &&
(str[nextIndex:(nextIndex+wordLength)] == word) {
return true
}
}
return false
}
for index, char := range oldVersion {
charIsSpecial := !(unicode.IsLetter(char) || unicode.IsNumber(char))
if (index >= len(newVersion)) || (char != rune(newVersion[index])) {
if charIsSpecial {
diffPosition = index
}
break
}
if charIsSpecial ||
(((index == len(oldVersion)-1) || (index == len(newVersion)-1)) &&
((len(oldVersion) != len(newVersion)) ||
(oldVersion[index] == newVersion[index]))) ||
checkWords(oldVersion, index, "rc", "pre", "alpha", "beta") {
diffPosition = index + 1
}
}
samePart := oldVersion[0:diffPosition]
left = samePart + red(oldVersion[diffPosition:len(oldVersion)])
right = samePart + green(newVersion[diffPosition:len(newVersion)])
}
return