upgrade.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package upgrade
  2. import (
  3. "fmt"
  4. "unicode"
  5. "github.com/Jguer/yay/v10/pkg/intrange"
  6. "github.com/Jguer/yay/v10/pkg/text"
  7. alpm "github.com/Jguer/go-alpm/v2"
  8. )
  9. // Filter decides if specific package should be included in theincluded in the results.
  10. type Filter func(Upgrade) bool
  11. // Upgrade type describes a system upgrade.
  12. type Upgrade struct {
  13. Name string
  14. Repository string
  15. LocalVersion string
  16. RemoteVersion string
  17. Reason alpm.PkgReason
  18. }
  19. func (u *Upgrade) StylizedNameWithRepository() string {
  20. return text.Bold(text.ColorHash(u.Repository)) + "/" + text.Bold(u.Name)
  21. }
  22. // upSlice is a slice of Upgrades
  23. type UpSlice []Upgrade
  24. func (u UpSlice) Len() int { return len(u) }
  25. func (u UpSlice) Swap(i, j int) { u[i], u[j] = u[j], u[i] }
  26. func (u UpSlice) Less(i, j int) bool {
  27. if u[i].Repository == u[j].Repository {
  28. iRunes := []rune(u[i].Name)
  29. jRunes := []rune(u[j].Name)
  30. return text.LessRunes(iRunes, jRunes)
  31. }
  32. iRunes := []rune(u[i].Repository)
  33. jRunes := []rune(u[j].Repository)
  34. return text.LessRunes(iRunes, jRunes)
  35. }
  36. func GetVersionDiff(oldVersion, newVersion string) (left, right string) {
  37. if oldVersion == newVersion {
  38. return oldVersion + text.Red(""), newVersion + text.Green("")
  39. }
  40. diffPosition := 0
  41. checkWords := func(str string, index int, words ...string) bool {
  42. for _, word := range words {
  43. wordLength := len(word)
  44. nextIndex := index + 1
  45. if (index < len(str)-wordLength) &&
  46. (str[nextIndex:(nextIndex+wordLength)] == word) {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. for index, char := range oldVersion {
  53. charIsSpecial := !(unicode.IsLetter(char) || unicode.IsNumber(char))
  54. if (index >= len(newVersion)) || (char != rune(newVersion[index])) {
  55. if charIsSpecial {
  56. diffPosition = index
  57. }
  58. break
  59. }
  60. if charIsSpecial ||
  61. (((index == len(oldVersion)-1) || (index == len(newVersion)-1)) &&
  62. ((len(oldVersion) != len(newVersion)) ||
  63. (oldVersion[index] == newVersion[index]))) ||
  64. checkWords(oldVersion, index, "rc", "pre", "alpha", "beta") {
  65. diffPosition = index + 1
  66. }
  67. }
  68. samePart := oldVersion[0:diffPosition]
  69. left = samePart + text.Red(oldVersion[diffPosition:])
  70. right = samePart + text.Green(newVersion[diffPosition:])
  71. return left, right
  72. }
  73. // Print prints the details of the packages to upgrade.
  74. func (u UpSlice) Print() {
  75. longestName, longestVersion := 0, 0
  76. for _, pack := range u {
  77. packNameLen := len(pack.StylizedNameWithRepository())
  78. packVersion, _ := GetVersionDiff(pack.LocalVersion, pack.RemoteVersion)
  79. packVersionLen := len(packVersion)
  80. longestName = intrange.Max(packNameLen, longestName)
  81. longestVersion = intrange.Max(packVersionLen, longestVersion)
  82. }
  83. namePadding := fmt.Sprintf("%%-%ds ", longestName)
  84. versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
  85. numberPadding := fmt.Sprintf("%%%dd ", len(fmt.Sprintf("%v", len(u))))
  86. for k, i := range u {
  87. left, right := GetVersionDiff(i.LocalVersion, i.RemoteVersion)
  88. fmt.Print(text.Magenta(fmt.Sprintf(numberPadding, len(u)-k)))
  89. fmt.Printf(namePadding, i.StylizedNameWithRepository())
  90. fmt.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
  91. }
  92. }