master > master: src go - algorithmen + display methoden hinzugefügt
This commit is contained in:
116
code/golang/internal/setup/setup_display.go
Normal file
116
code/golang/internal/setup/setup_display.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package setup
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"ads/internal/core/logging"
|
||||
"ads/internal/core/metrics"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD display case start/end
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func DisplayStartOfCase(index int, descr *string) {
|
||||
DisplayBar(80)
|
||||
if descr == nil || *descr == "" {
|
||||
logging.LogPlain(fmt.Sprintf("\033[92;1mCASE %[1]v\033[0m.", index))
|
||||
} else {
|
||||
logging.LogPlain(fmt.Sprintf("\033[92;1mCASE %[1]v\033[0m (\033[1;2m%[2]s\033[0m).", index, *descr))
|
||||
}
|
||||
}
|
||||
|
||||
func DisplayEndOfCase() {
|
||||
DisplayBar(80)
|
||||
return
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD display value
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func RepresentValue(value interface{}) interface{} {
|
||||
switch reflect.TypeOf(value).Kind() {
|
||||
case reflect.Ptr:
|
||||
obj := reflect.ValueOf(value)
|
||||
if obj.IsNil() {
|
||||
return fmt.Sprintf("%v", nil)
|
||||
} else {
|
||||
return fmt.Sprintf("&%v", RepresentValue(obj.Elem().Interface()))
|
||||
}
|
||||
case reflect.Slice:
|
||||
obj := reflect.ValueOf(value)
|
||||
n := obj.Len()
|
||||
values := []interface{}{}
|
||||
for i := 0; i < n; i++ {
|
||||
x := RepresentValue(obj.Index(i).Interface())
|
||||
values = append(values, x)
|
||||
}
|
||||
if n > 10 {
|
||||
values = append(append(values[:3], "..."), values[n-2:]...)
|
||||
}
|
||||
return fmt.Sprintf("%v", values)
|
||||
case reflect.Map:
|
||||
obj := reflect.ValueOf(value)
|
||||
n := obj.Len()
|
||||
iter := obj.MapRange()
|
||||
m := map[interface{}]interface{}{}
|
||||
parts := []string{}
|
||||
for iter.Next() {
|
||||
key := iter.Key().Interface()
|
||||
x := RepresentValue(iter.Value().Interface())
|
||||
m[key] = x
|
||||
parts = append(parts, fmt.Sprintf("%[1]v: %[2]v", key, x))
|
||||
}
|
||||
if n > 4 {
|
||||
parts = append(append(parts[:3], "..."), parts[n-2:]...)
|
||||
}
|
||||
return fmt.Sprintf("{%s}", strings.Join(parts, ", "))
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD display algorithm start/end
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func DisplayStartOfAlgorithm(name string, inputs map[string]interface{}) {
|
||||
logging.LogPlain(fmt.Sprintf("Ausführung vom Algorithmus: \033[92;1m%[1]s\033[0m", name))
|
||||
logging.LogPlain("INPUTS")
|
||||
for varname, value := range inputs {
|
||||
logging.LogPlain(fmt.Sprintf(" - %[1]s = %[2]v", varname, RepresentValue(value)))
|
||||
}
|
||||
}
|
||||
|
||||
func DisplayEndOfAlgorithm(outputs map[string]interface{}) {
|
||||
logging.LogPlain("OUTPUTS:")
|
||||
for varname, value := range outputs {
|
||||
logging.LogPlain(fmt.Sprintf(" - %[1]s = %[2]v", varname, RepresentValue(value)))
|
||||
}
|
||||
}
|
||||
|
||||
func DisplayMetrics() {
|
||||
logging.LogPlain(fmt.Sprintf("Dauer der Ausführung: t = \033[1m%[1]v\033[0m", metrics.GetTimeElapsed()))
|
||||
logging.LogPlain(fmt.Sprintf("Kosten (Zeit): T(n) = \033[1m%[1]v\033[0m", metrics.GetTimeCost()))
|
||||
logging.LogPlain(fmt.Sprintf("Kosten (Platz): S(n) = \033[1m%[1]v\033[0m", metrics.GetSpaceCost()))
|
||||
return
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS Verschiedenes
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func DisplayBar(options ...int) {
|
||||
n := 80
|
||||
if len(options) > 0 {
|
||||
n = options[0]
|
||||
}
|
||||
logging.LogPlain(fmt.Sprintf("+%[1]s+", strings.Repeat("-", n)))
|
||||
return
|
||||
}
|
||||
30
code/golang/internal/setup/setup_userconfig.go
Normal file
30
code/golang/internal/setup/setup_userconfig.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package setup
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"ads/internal/types"
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
// Erstellt eine Struktur, die die Infos aus der Config-Datei erfasst
|
||||
func NewUserConfig() types.UserConfig {
|
||||
return types.UserConfig{}
|
||||
}
|
||||
|
||||
// Extrahiert Inhalte einer YAML Config und parset dies als Struktur
|
||||
func GetUserConfig(path string, config *types.UserConfig) error {
|
||||
contents, err := ioutil.ReadFile(path)
|
||||
if err == nil {
|
||||
err = yaml.Unmarshal(contents, config)
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user