2021-10-11 22:22:03 +02:00
|
|
|
package menus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-20 01:51:55 +01:00
|
|
|
"io"
|
2021-10-11 22:22:03 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/intrange"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/text"
|
2022-11-20 01:51:55 +01:00
|
|
|
|
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2021-10-11 22:22:03 +02:00
|
|
|
)
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
func pkgbuildNumberMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string, installed mapset.Set[string]) {
|
2021-10-11 22:22:03 +02:00
|
|
|
toPrint := ""
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
for n, pkgBase := range bases {
|
|
|
|
dir := pkgbuildDirs[pkgBase]
|
|
|
|
toPrint += fmt.Sprintf(text.Magenta("%3d")+" %-40s", len(pkgbuildDirs)-n,
|
|
|
|
text.Bold(pkgBase))
|
2021-10-11 22:22:03 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if installed.Contains(pkgBase) {
|
2021-10-11 22:22:03 +02:00
|
|
|
toPrint += text.Bold(text.Green(gotext.Get(" (Installed)")))
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:27:42 +02:00
|
|
|
// TODO: remove or refactor to check if git dir is unclean
|
2021-10-11 22:22:03 +02:00
|
|
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
|
|
|
toPrint += text.Bold(text.Green(gotext.Get(" (Build Files Exist)")))
|
|
|
|
}
|
|
|
|
|
|
|
|
toPrint += "\n"
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
fmt.Fprint(w, toPrint)
|
2021-10-11 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
func selectionMenu(w io.Writer, pkgbuildDirs map[string]string, bases []string, installed mapset.Set[string],
|
|
|
|
message string, noConfirm bool, defaultAnswer string, skipFunc func(string) bool,
|
|
|
|
) ([]string, error) {
|
|
|
|
selected := make([]string, 0)
|
2021-10-11 23:08:18 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
pkgbuildNumberMenu(w, pkgbuildDirs, bases, installed)
|
2021-10-11 22:22:03 +02:00
|
|
|
|
2021-10-11 22:38:51 +02:00
|
|
|
text.Infoln(message)
|
|
|
|
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"))))
|
2021-10-11 22:22:03 +02:00
|
|
|
|
2023-02-17 20:01:26 +01:00
|
|
|
selectInput, err := text.GetInput(os.Stdin, defaultAnswer, noConfirm)
|
2021-10-11 22:38:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-10-11 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 23:08:18 +02:00
|
|
|
eInclude, eExclude, eOtherInclude, eOtherExclude := intrange.ParseNumberMenu(selectInput)
|
2021-10-11 22:22:03 +02:00
|
|
|
eIsInclude := len(eExclude) == 0 && len(eOtherExclude) == 0
|
|
|
|
|
|
|
|
if eOtherInclude.Get("abort") || eOtherInclude.Get("ab") {
|
2021-10-11 23:08:18 +02:00
|
|
|
return nil, settings.ErrUserAbort{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if eOtherInclude.Get("n") || eOtherInclude.Get("none") {
|
|
|
|
return selected, nil
|
2021-10-11 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
for i, pkgBase := range bases {
|
|
|
|
if skipFunc != nil && skipFunc(pkgBase) {
|
2021-10-11 23:08:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
anyInstalled := installed.Contains(pkgBase)
|
2021-10-11 23:08:18 +02:00
|
|
|
|
|
|
|
if !eIsInclude && eExclude.Get(len(bases)-i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyInstalled && (eOtherInclude.Get("i") || eOtherInclude.Get("installed")) {
|
2022-11-20 01:51:55 +01:00
|
|
|
selected = append(selected, pkgBase)
|
2021-10-11 23:08:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !anyInstalled && (eOtherInclude.Get("no") || eOtherInclude.Get("notinstalled")) {
|
2022-11-20 01:51:55 +01:00
|
|
|
selected = append(selected, pkgBase)
|
2021-10-11 23:08:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if eOtherInclude.Get("a") || eOtherInclude.Get("all") {
|
2022-11-20 01:51:55 +01:00
|
|
|
selected = append(selected, pkgBase)
|
2021-10-11 23:08:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if eIsInclude && (eInclude.Get(len(bases)-i) || eOtherInclude.Get(pkgBase)) {
|
|
|
|
selected = append(selected, pkgBase)
|
2021-10-11 23:08:18 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
if !eIsInclude && (!eExclude.Get(len(bases)-i) && !eOtherExclude.Get(pkgBase)) {
|
|
|
|
selected = append(selected, pkgBase)
|
2021-10-11 22:22:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:08:18 +02:00
|
|
|
return selected, nil
|
2021-10-11 22:22:03 +02:00
|
|
|
}
|