upgrade.go 3.0 KB

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