version_diff.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package query
  2. import (
  3. "strings"
  4. "unicode"
  5. "github.com/Jguer/yay/v12/pkg/text"
  6. "github.com/Jguer/go-alpm/v2"
  7. )
  8. func GetVersionDiff(oldVersion, newVersion string) (left, right string) {
  9. if oldVersion == newVersion {
  10. return oldVersion + text.Red(""), newVersion + text.Green("")
  11. }
  12. diffPosition := 0
  13. checkWords := func(str string, index int, words ...string) bool {
  14. for _, word := range words {
  15. wordLength := len(word)
  16. nextIndex := index + 1
  17. if (index < len(str)-wordLength) &&
  18. (str[nextIndex:(nextIndex+wordLength)] == word) {
  19. return true
  20. }
  21. }
  22. return false
  23. }
  24. for index, char := range oldVersion {
  25. charIsSpecial := !(unicode.IsLetter(char) || unicode.IsNumber(char))
  26. if (index >= len(newVersion)) || (char != rune(newVersion[index])) {
  27. if charIsSpecial {
  28. diffPosition = index
  29. }
  30. break
  31. }
  32. if charIsSpecial ||
  33. (((index == len(oldVersion)-1) || (index == len(newVersion)-1)) &&
  34. ((len(oldVersion) != len(newVersion)) ||
  35. (oldVersion[index] == newVersion[index]))) ||
  36. checkWords(oldVersion, index, "rc", "pre", "alpha", "beta") {
  37. diffPosition = index + 1
  38. }
  39. }
  40. samePart := oldVersion[0:diffPosition]
  41. left = samePart + text.Red(oldVersion[diffPosition:])
  42. right = samePart + text.Green(newVersion[diffPosition:])
  43. return left, right
  44. }
  45. func isDevelName(name string) bool {
  46. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly", "insiders-bin"} {
  47. if strings.HasSuffix(name, "-"+suffix) {
  48. return true
  49. }
  50. }
  51. return strings.Contains(name, "-always-")
  52. }
  53. func isDevelPackage(pkg alpm.IPackage) bool {
  54. return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
  55. }