master > master: code go - fügte settings hinzu

This commit is contained in:
RD
2021-11-03 18:43:13 +01:00
parent f55677ed8b
commit f7ef295ec8
11 changed files with 208 additions and 66 deletions

View File

@@ -15,6 +15,14 @@ import (
"gopkg.in/yaml.v3"
)
/* ---------------------------------------------------------------- *
* METHOD menu class methods
* ---------------------------------------------------------------- */
func (menu *Menu) SetDefault(index int) {
menu.Default = index
}
/* ---------------------------------------------------------------- *
* METHOD show menu
* ---------------------------------------------------------------- */
@@ -29,7 +37,8 @@ func (menu Menu) ShowMenu() (bool, error) {
err error
)
var promptMessages []string
var options [][2]string
var optionsFlattened []MenuOption
var optionsKeyValue [][2]string
var defaultOption string
var breaks []int
@@ -40,22 +49,32 @@ func (menu Menu) ShowMenu() (bool, error) {
promptMessages = append([]string{head}, promptMessages...)
// Zurück-Option einfügen
defaultOption = fmt.Sprintf("%v", menu.DefaultOption)
options = []([2]string){}
for i, opt := range menu.Options {
key := fmt.Sprintf("%v", i+1)
subLabel := opt.SubLabel
label := opt.Label
if !(subLabel == "") {
label = fmt.Sprintf("%v (\033[2m%v\033[0m)", opt.Label, subLabel)
defaultOption = ""
if menu.Default >= 0 {
defaultOption = fmt.Sprintf("%v", menu.Default+1)
}
breaks = []int{}
optionsFlattened = []MenuOption{}
optionsKeyValue = []([2]string){}
index = 0
for _, suboptions := range menu.Options {
for _, opt := range suboptions {
optionsFlattened = append(optionsFlattened, opt)
key := fmt.Sprintf("%v", index+1)
subLabel := opt.SubLabel
label := opt.Label
if !(subLabel == "") {
label = fmt.Sprintf("%v (\033[2m%v\033[0m)", opt.Label, subLabel)
}
optionsKeyValue = append(optionsKeyValue, [2]string{key, label})
index++
}
options = append(options, [2]string{key, label})
breaks = append(breaks, index-1)
}
breaks = []int{len(menu.Options) - 1}
if !menu.IsRoot {
options = append(options, [2]string{"z", "Zurück zum vorherigen Menü"})
optionsKeyValue = append(optionsKeyValue, [2]string{"z", "Zurück zum vorherigen Menü"})
}
options = append(options, [2]string{"q", "Programm schließen"})
optionsKeyValue = append(optionsKeyValue, [2]string{"q", "Programm schließen"})
// User Response immer abfragen und abarbeiten, bis quit/return.
performClearScreen := !menu.IsRoot
@@ -65,7 +84,7 @@ func (menu Menu) ShowMenu() (bool, error) {
}
performClearScreen = true
choice, meta = PromptListOfOptions(promptMessages, options, breaks, defaultOption)
choice, meta = promptListOfOptions(promptMessages, optionsKeyValue, breaks, defaultOption)
// Falls quit wählt, -> quit:
if (menu.IsRoot && meta) || choice == "q" {
@@ -80,8 +99,8 @@ func (menu Menu) ShowMenu() (bool, error) {
// sonst führe die assoziierte Methode aus
index64, _ := strconv.ParseInt(choice, 10, 64)
index = int(index64) - 1
if 0 <= index && index < len(menu.Options) {
opt := menu.Options[index]
if 0 <= index && index < len(optionsFlattened) {
opt := optionsFlattened[index]
// Entweder Untermenü öffnen oder Action ausführen
if opt.Submenu != nil {
quit, err = opt.Submenu.ShowMenu()
@@ -93,13 +112,19 @@ func (menu Menu) ShowMenu() (bool, error) {
if err != nil {
logging.LogError(err)
}
if cancel {
continue
}
quit := logging.PromptAnyKeyToContinue()
// Falls während der Action der User Meta+D klickt, -> quit:
if quit {
return true, nil
// Falls ForceReturn, dann nach Ausführung der Action, -> return
if menu.ForceReturn {
return false, nil
} else {
// Falls während der Action der User Meta+D klickt, -> return:
if cancel {
continue
}
quit := logging.PromptAnyKeyToContinue()
// Falls nach der Action der User Meta+D klickt, -> quit:
if quit {
return true, nil
}
}
} else {
logging.LogWarn("Option noch nicht implementiert.")