menu.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package menus
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/leonelquinteros/gotext"
  7. "github.com/Jguer/yay/v12/pkg/intrange"
  8. "github.com/Jguer/yay/v12/pkg/settings"
  9. "github.com/Jguer/yay/v12/pkg/text"
  10. mapset "github.com/deckarep/golang-set/v2"
  11. )
  12. func pkgbuildNumberMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string, installed mapset.Set[string]) {
  13. toPrint := ""
  14. for n, pkgBase := range bases {
  15. dir := pkgbuildDirs[pkgBase]
  16. toPrint += fmt.Sprintf(text.Magenta("%3d")+" %-40s", len(pkgbuildDirs)-n,
  17. text.Bold(pkgBase))
  18. if installed.Contains(pkgBase) {
  19. toPrint += text.Bold(text.Green(gotext.Get(" (Installed)")))
  20. }
  21. // TODO: remove or refactor to check if git dir is unclean
  22. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  23. toPrint += text.Bold(text.Green(gotext.Get(" (Build Files Exist)")))
  24. }
  25. toPrint += "\n"
  26. }
  27. fmt.Fprint(w, toPrint)
  28. }
  29. func selectionMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string, installed mapset.Set[string],
  30. message string, noConfirm bool, defaultAnswer string, skipFunc func(string) bool,
  31. ) ([]string, error) {
  32. selected := make([]string, 0)
  33. pkgbuildNumberMenu(w, pkgbuildDirs, 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(os.Stdin, defaultAnswer, noConfirm)
  37. if err != nil {
  38. return nil, err
  39. }
  40. eInclude, eExclude, eOtherInclude, eOtherExclude := intrange.ParseNumberMenu(selectInput)
  41. eIsInclude := len(eExclude) == 0 && eOtherExclude.Cardinality() == 0
  42. if eOtherInclude.Contains("abort") || eOtherInclude.Contains("ab") {
  43. return nil, settings.ErrUserAbort{}
  44. }
  45. if eOtherInclude.Contains("n") || eOtherInclude.Contains("none") {
  46. return selected, nil
  47. }
  48. for i, pkgBase := range bases {
  49. if skipFunc != nil && skipFunc(pkgBase) {
  50. continue
  51. }
  52. anyInstalled := installed.Contains(pkgBase)
  53. if !eIsInclude && eExclude.Get(len(bases)-i) {
  54. continue
  55. }
  56. if anyInstalled && (eOtherInclude.Contains("i") || eOtherInclude.Contains("installed")) {
  57. selected = append(selected, pkgBase)
  58. continue
  59. }
  60. if !anyInstalled && (eOtherInclude.Contains("no") || eOtherInclude.Contains("notinstalled")) {
  61. selected = append(selected, pkgBase)
  62. continue
  63. }
  64. if eOtherInclude.Contains("a") || eOtherInclude.Contains("all") {
  65. selected = append(selected, pkgBase)
  66. continue
  67. }
  68. if eIsInclude && (eInclude.Get(len(bases)-i) || eOtherInclude.Contains(pkgBase)) {
  69. selected = append(selected, pkgBase)
  70. }
  71. if !eIsInclude && (!eExclude.Get(len(bases)-i) && !eOtherExclude.Contains(pkgBase)) {
  72. selected = append(selected, pkgBase)
  73. }
  74. }
  75. return selected, nil
  76. }