ads1_2021/code/golang/internal/menus/menus_prompt.go

92 lines
2.3 KiB
Go

package menus
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"reflect"
"ads/internal/core/logging"
"ads/internal/core/utils"
)
/* ---------------------------------------------------------------- *
* METHOD prompt options
* ---------------------------------------------------------------- */
// Zeigt Prompt mit Liste von Optionen an
func PromptListOfOptions(messages []string, options [][2]string, breaks []int, defaultChoice string) (string, bool) {
var n = len(options)
var (
choice string
cancel bool
err error
)
var lines []interface{}
var optionsMap = map[string]string{}
// Erzeuge Messages
lines = []interface{}{}
for _, message := range messages {
lines = append(lines, message)
}
lines = append(lines, "")
for i, obj := range options {
key := obj[0]
label := obj[1]
optionsMap[key] = label
if key == defaultChoice {
lines = append(lines, fmt.Sprintf("\033[91m*\033[0m\033[93;1m%v)\033[0m %v", key, label))
} else {
lines = append(lines, fmt.Sprintf(" \033[93;1m%v)\033[0m %v", key, label))
}
if i < n-1 && utils.ArrayContains(breaks, i) {
lines = append(lines, " \033[93m--------\033[0m")
}
i++
}
if !(defaultChoice == "" || defaultChoice == "-1") {
lines = append(lines, "")
lines = append(lines, "\033[91;2m*\033[0m\033[2m = Default\033[0m")
}
// Wiederhole Prompt, bis gültige Eingabe erreicht
for {
choice, cancel, err = logging.Prompt(lines...)
if cancel {
return "", true
}
if err != nil {
logging.ClearScreen()
logging.LogError(err)
continue
}
if choice == "" {
choice = defaultChoice
break
}
if _, ok := optionsMap[choice]; !ok {
logging.ClearScreen()
logging.LogError("Ungültige eingabe")
continue
}
break
}
return choice, false
}
/* ---------------------------------------------------------------- *
* METHOD prompt values
* ---------------------------------------------------------------- */
func PromptValue(varname string, typename string, example string, t reflect.Type) (bool, error) {
_, cancel, err := logging.Prompt(
fmt.Sprintf("Bitte den Wert von \033[1m%s\033[0m als \033[1m%s\033[0m eingeben.", varname, typename),
example,
)
// TODO: input parsen
return cancel, err
}