upgrade.go 2.9 KB

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