master > master: code go - interactive mode eingerichtet
This commit is contained in:
@@ -6,10 +6,13 @@ package menus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
@@ -22,6 +25,8 @@ func (menu Menu) ShowMenu() (bool, error) {
|
||||
index int
|
||||
meta bool
|
||||
quit bool
|
||||
cancel bool
|
||||
err error
|
||||
)
|
||||
var promptMessages []string
|
||||
var options [][2]string
|
||||
@@ -79,12 +84,18 @@ func (menu Menu) ShowMenu() (bool, error) {
|
||||
opt := menu.Options[index]
|
||||
// Entweder Untermenü öffnen oder Action ausführen
|
||||
if opt.Submenu != nil {
|
||||
quit, _ = opt.Submenu.ShowMenu()
|
||||
quit, err = opt.Submenu.ShowMenu()
|
||||
if quit {
|
||||
return true, nil
|
||||
return true, err
|
||||
}
|
||||
} else if opt.Action != nil {
|
||||
opt.Action()
|
||||
cancel, err = opt.Action()
|
||||
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 {
|
||||
@@ -100,3 +111,69 @@ func (menu Menu) ShowMenu() (bool, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD prompt values
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func (query *PromptValueQuery) Prompt() (bool, error) {
|
||||
var lines = []interface{}{}
|
||||
var responsePtr = query.Response
|
||||
var (
|
||||
line string
|
||||
cancel bool
|
||||
err error
|
||||
)
|
||||
|
||||
// prüfen, dass response ein Ptr auf eine Struct ist:
|
||||
if !(reflect.ValueOf(responsePtr).Kind() == reflect.Ptr) || !(reflect.ValueOf(responsePtr).Elem().Kind() == reflect.Struct) {
|
||||
panic("Input muss ein Pointer auf einen struct sein")
|
||||
}
|
||||
|
||||
// prompt message vorbereiten:
|
||||
lines = append(lines, fmt.Sprintf("%s, \033[1;3m%s\033[0m, als \033[1m%s\033[0m bitte eingeben.", query.Description, query.Name, query.Type))
|
||||
if query.ValidExamples != nil && len(*query.ValidExamples) > 0 {
|
||||
if len(*query.ValidExamples) == 1 {
|
||||
example := (*query.ValidExamples)[0]
|
||||
lines = append(lines, fmt.Sprintf(" \033[3;4mBeispiel von Input im gültigen Format:\033[0m \033[2m%s\033[0m", example))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf(" \033[3;4mBeispiele von Inputs im gültigen Format:\033[0m"))
|
||||
for _, example := range *query.ValidExamples {
|
||||
lines = append(lines, fmt.Sprintf(" - \033[2m%s\033[0m", example))
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf(" - \033[2;3metc.\033[0m"))
|
||||
}
|
||||
}
|
||||
if query.Requirements != nil && len(*query.Requirements) > 0 {
|
||||
if len(*query.Requirements) == 1 {
|
||||
requirement := (*query.Requirements)[0]
|
||||
lines = append(lines, fmt.Sprintf(" \033[3;4mInput muss erfüllen:\033[0m \033[2m%s\033[0m", requirement))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf(" \033[3;4mInput muss erfüllen:\033[0m"))
|
||||
for _, requirement := range *query.Requirements {
|
||||
lines = append(lines, fmt.Sprintf(" - \033[2m%s\033[0m", requirement))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// prompt Eingabe eines Werts:
|
||||
for {
|
||||
line, cancel, err = logging.Prompt(lines...)
|
||||
if cancel {
|
||||
return true, nil
|
||||
}
|
||||
if err != nil {
|
||||
logging.LogError(err, "")
|
||||
continue
|
||||
}
|
||||
line = fmt.Sprintf("\"response\": %s", line)
|
||||
err = yaml.Unmarshal([]byte(line), query.Response)
|
||||
if err != nil {
|
||||
logging.LogError(err, "")
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return cancel, err
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ package menus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/utils"
|
||||
@@ -76,16 +75,3 @@ func PromptListOfOptions(messages []string, options [][2]string, breaks []int, d
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type MenuOption struct {
|
||||
Label string
|
||||
SubLabel string
|
||||
Submenu *Menu
|
||||
Action func() error // NOTE: in go, this is automatically a pointer type
|
||||
Action func() (bool, error) // NOTE: in go, this is automatically a pointer type
|
||||
}
|
||||
|
||||
type Menu struct {
|
||||
@@ -24,3 +24,34 @@ type Menu struct {
|
||||
Options []MenuOption
|
||||
DefaultOption int
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* TYPES - prompt
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
Usage
|
||||
|
||||
- Name: Variablenname
|
||||
|
||||
- Description: Kurze Beschreibung der Variablen
|
||||
|
||||
- Type: Beschreiben des erwarteten Types der Variablen.
|
||||
|
||||
- Requirements: Liste von Anforderungen.
|
||||
|
||||
- Response: Ptr zur Struct, d. h. &responseType{}, wobei responseType eine struct der folgenden Form ist:
|
||||
|
||||
type responseType struct { Response #### `yaml:"response"` }
|
||||
|
||||
wobei #### = ein Typ
|
||||
*/
|
||||
type PromptValueQuery struct {
|
||||
Name string
|
||||
Description string
|
||||
Type string
|
||||
ValidExamples *[]string
|
||||
Requirements *[]string
|
||||
// Response muss ein Ptr auf eine Struct sein:
|
||||
Response interface{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user