upgrade.go 3.0 KB

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