version_diff.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Make sure the word is not part of a longer word
  15. ongoingWord := unicode.IsLetter(rune(str[index]))
  16. if ongoingWord {
  17. return false
  18. }
  19. for _, word := range words {
  20. wordLength := len(word)
  21. nextIndex := index + 1
  22. if (index < len(str)-wordLength) &&
  23. (str[nextIndex:(nextIndex+wordLength)] == word) {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. for index, char := range oldVersion {
  30. charIsSpecial := !(unicode.IsLetter(char) || unicode.IsNumber(char))
  31. if (index >= len(newVersion)) || (char != rune(newVersion[index])) {
  32. if charIsSpecial {
  33. diffPosition = index
  34. }
  35. break
  36. }
  37. if charIsSpecial ||
  38. (((index == len(oldVersion)-1) || (index == len(newVersion)-1)) &&
  39. ((len(oldVersion) != len(newVersion)) ||
  40. (oldVersion[index] == newVersion[index]))) ||
  41. checkWords(oldVersion, index, "rc", "pre", "alpha", "beta") {
  42. diffPosition = index + 1
  43. }
  44. }
  45. samePart := oldVersion[0:diffPosition]
  46. left = samePart + text.Red(oldVersion[diffPosition:])
  47. right = samePart + text.Green(newVersion[diffPosition:])
  48. return left, right
  49. }
  50. func isDevelName(name string) bool {
  51. for _, suffix := range []string{"git", "svn", "hg", "bzr", "nightly", "insiders-bin"} {
  52. if strings.HasSuffix(name, "-"+suffix) {
  53. return true
  54. }
  55. }
  56. return strings.Contains(name, "-always-")
  57. }
  58. func isDevelPackage(pkg alpm.IPackage) bool {
  59. return isDevelName(pkg.Name()) || isDevelName(pkg.Base())
  60. }