Merge pull request #329 from AlexWayfer/improve_versions_output

Highlight diff between old and new versions better
This commit is contained in:
Morgana 2018-04-09 16:15:40 +01:00 committed by GitHub
commit 2aaf5f8694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 42 deletions

View File

@ -370,60 +370,52 @@ func formatTime(i int) string {
return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day()) return fmt.Sprintf("%d/%02d/%02d", t.Year(), int(t.Month()), t.Day())
} }
func red(in string) string { const (
redCode = "\x1b[31m"
greenCode = "\x1b[32m"
yellowCode = "\x1b[33m"
blueCode = "\x1b[34m"
magentaCode = "\x1b[35m"
cyanCode = "\x1b[36m"
boldCode = "\x1b[1m"
resetCode = "\x1b[0m"
)
func stylize(startCode, in string) string {
if useColor { if useColor {
return "\x1b[31m" + in + "\x1b[0m" return startCode + in + resetCode
} }
return in return in
} }
func red(in string) string {
return stylize(redCode, in)
}
func green(in string) string { func green(in string) string {
if useColor { return stylize(greenCode, in)
return "\x1b[32m" + in + "\x1b[0m"
}
return in
} }
func yellow(in string) string { func yellow(in string) string {
if useColor { return stylize(yellowCode, in)
return "\x1b[33m" + in + "\x1b[0m"
}
return in
} }
func blue(in string) string { func blue(in string) string {
if useColor { return stylize(blueCode, in)
return "\x1b[34m" + in + "\x1b[0m"
}
return in
} }
func cyan(in string) string { func cyan(in string) string {
if useColor { return stylize(cyanCode, in)
return "\x1b[36m" + in + "\x1b[0m"
}
return in
} }
func magenta(in string) string { func magenta(in string) string {
if useColor { return stylize(magentaCode, in)
return "\x1b[35m" + in + "\x1b[0m"
}
return in
} }
func bold(in string) string { func bold(in string) string {
if useColor { return stylize(boldCode, in)
return "\x1b[1m" + in + "\x1b[0m"
}
return in
} }
// Colours text using a hashing algorithm. The same text will always produce the // Colours text using a hashing algorithm. The same text will always produce the

View File

@ -5,6 +5,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"unicode"
alpm "github.com/jguer/go-alpm" alpm "github.com/jguer/go-alpm"
rpc "github.com/mikkeloscar/aur" rpc "github.com/mikkeloscar/aur"
@ -54,12 +55,12 @@ func (u upSlice) Less(i, j int) bool {
if found != nil { if found != nil {
return less return less
} else {
iRunes := []rune(u[i].Repository)
jRunes := []rune(u[j].Repository)
return lessRunes(iRunes, jRunes)
} }
iRunes := []rune(u[i].Repository)
jRunes := []rune(u[j].Repository)
return lessRunes(iRunes, jRunes)
} }
func getVersionDiff(oldVersion, newversion string) (left, right string) { func getVersionDiff(oldVersion, newversion string) (left, right string) {
@ -74,13 +75,50 @@ func getVersionDiff(oldVersion, newversion string) (left, right string) {
} }
if errOld == nil && errNew == nil { if errOld == nil && errNew == nil {
if old.Version == new.Version { oldVersion := old.String()
left = string(old.Version) + "-" + red(string(old.Pkgrel)) newVersion := new.String()
right = string(new.Version) + "-" + green(string(new.Pkgrel))
} else { if oldVersion == newVersion {
left = red(string(old.Version)) + "-" + string(old.Pkgrel) return oldVersion, newVersion
right = bold(green(string(new.Version))) + "-" + string(new.Pkgrel)
} }
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 return

56
upgrade_test.go Normal file
View File

@ -0,0 +1,56 @@
package main
import "testing"
func TestGetVersionDiff(t *testing.T) {
useColor = true
type versionPair struct {
Old string
New string
}
in := []versionPair{
{"1-1", "1-1"},
{"1-1", "2-1"},
{"2-1", "1-1"},
{"1-1", "1-2"},
{"1-2", "1-1"},
{"1.2.3-1", "1.2.4-1"},
{"1.8rc1+6+g0f377f94-1", "1.8rc1+1+g7e949283-1"},
{"1.8rc1+6+g0f377f94-1", "1.8rc2+1+g7e949283-1"},
{"1.8rc2", "1.9rc1"},
{"2.99.917+812+g75795523-1", "2.99.917+823+gd9bf46e4-1"},
{"1.2.9-1", "1.2.10-1"},
{"1.2.10-1", "1.2.9-1"},
{"1.2-1", "1.2.1-1"},
{"1.2.1-1", "1.2-1"},
{"0.7-4", "0.7+4+gd8d8c67-1"},
}
out := []versionPair{
{"1-1", "1-1"},
{red("1-1"), green("2-1")},
{red("2-1"), green("1-1")},
{"1-" + red("1"), "1-" + green("2")},
{"1-" + red("2"), "1-" + green("1")},
{"1.2." + red("3-1"), "1.2." + green("4-1")},
{"1.8rc1+" + red("6+g0f377f94-1"), "1.8rc1+" + green("1+g7e949283-1")},
{"1.8" + red("rc1+6+g0f377f94-1"), "1.8" + green("rc2+1+g7e949283-1")},
{"1." + red("8rc2"), "1." + green("9rc1")},
{"2.99.917+" + red("812+g75795523-1"), "2.99.917+" + green("823+gd9bf46e4-1")},
{"1.2." + red("9-1"), "1.2." + green("10-1")},
{"1.2." + red("10-1"), "1.2." + green("9-1")},
{"1.2" + red("-1"), "1.2" + green(".1-1")},
{"1.2" + red(".1-1"), "1.2" + green("-1")},
{"0.7" + red("-4"), "0.7" + green("+4+gd8d8c67-1")},
}
for i, pair := range in {
o, n := getVersionDiff(pair.Old, pair.New)
if o != out[i].Old || n != out[i].New {
t.Errorf("Test %d failed for update: (%s => %s) expected (%s => %s) got (%s => %s)", i+1, in[i].Old, in[i].New, out[i].Old, out[i].New, o, n)
}
}
}