ads1_2021/code/golang/internal/setup/setup_display.go

118 lines
3.5 KiB
Go
Raw Normal View History

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("Dauer der Ausführung: t = \033[1m%[1]v\033[0m", metrics.GetTimeElapsedLongFormat()))
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
}