menu.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package menus
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/leonelquinteros/gotext"
  7. "github.com/Jguer/yay/v11/pkg/dep"
  8. "github.com/Jguer/yay/v11/pkg/intrange"
  9. "github.com/Jguer/yay/v11/pkg/settings"
  10. "github.com/Jguer/yay/v11/pkg/stringset"
  11. "github.com/Jguer/yay/v11/pkg/text"
  12. )
  13. func pkgbuildNumberMenu(buildDir string, bases []dep.Base, installed stringset.StringSet) {
  14. toPrint := ""
  15. for n, base := range bases {
  16. pkg := base.Pkgbase()
  17. dir := filepath.Join(buildDir, pkg)
  18. toPrint += fmt.Sprintf(text.Magenta("%3d")+" %-40s", len(bases)-n,
  19. text.Bold(base.String()))
  20. if base.AnyIsInSet(installed) {
  21. toPrint += text.Bold(text.Green(gotext.Get(" (Installed)")))
  22. }
  23. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  24. toPrint += text.Bold(text.Green(gotext.Get(" (Build Files Exist)")))
  25. }
  26. toPrint += "\n"
  27. }
  28. fmt.Print(toPrint)
  29. }
  30. func selectionMenu(buildDir string, bases []dep.Base, installed stringset.StringSet,
  31. message string, noConfirm bool, defaultAnswer string, skipFunc func(string) bool) ([]dep.Base, error) {
  32. selected := make([]dep.Base, 0)
  33. pkgbuildNumberMenu(buildDir, bases, installed)
  34. text.Infoln(message)
  35. text.Infoln(gotext.Get("%s [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)", text.Cyan(gotext.Get("[N]one"))))
  36. selectInput, err := text.GetInput(defaultAnswer, noConfirm)
  37. if err != nil {
  38. return nil, err
  39. }
  40. eInclude, eExclude, eOtherInclude, eOtherExclude := intrange.ParseNumberMenu(selectInput)
  41. eIsInclude := len(eExclude) == 0 && len(eOtherExclude) == 0
  42. if eOtherInclude.Get("abort") || eOtherInclude.Get("ab") {
  43. return nil, settings.ErrUserAbort{}
  44. }
  45. if eOtherInclude.Get("n") || eOtherInclude.Get("none") {
  46. return selected, nil
  47. }
  48. for i, base := range bases {
  49. pkg := base.Pkgbase()
  50. if skipFunc != nil && skipFunc(pkg) {
  51. continue
  52. }
  53. anyInstalled := base.AnyIsInSet(installed)
  54. if !eIsInclude && eExclude.Get(len(bases)-i) {
  55. continue
  56. }
  57. if anyInstalled && (eOtherInclude.Get("i") || eOtherInclude.Get("installed")) {
  58. selected = append(selected, base)
  59. continue
  60. }
  61. if !anyInstalled && (eOtherInclude.Get("no") || eOtherInclude.Get("notinstalled")) {
  62. selected = append(selected, base)
  63. continue
  64. }
  65. if eOtherInclude.Get("a") || eOtherInclude.Get("all") {
  66. selected = append(selected, base)
  67. continue
  68. }
  69. if eIsInclude && (eInclude.Get(len(bases)-i) || eOtherInclude.Get(pkg)) {
  70. selected = append(selected, base)
  71. }
  72. if !eIsInclude && (!eExclude.Get(len(bases)-i) && !eOtherExclude.Get(pkg)) {
  73. selected = append(selected, base)
  74. }
  75. }
  76. return selected, nil
  77. }