39 lines
947 B
Go
39 lines
947 B
Go
package widgets
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
|
"github.com/mum4k/termdash/widgetapi"
|
|
)
|
|
|
|
var Get = make(map[string]widgetapi.Widget)
|
|
|
|
var widgetOptions = make(map[string]func(ctx context.Context, t terminalapi.Terminal, options any) widgetapi.Widget)
|
|
|
|
type Widget struct {
|
|
name string
|
|
options interface{}
|
|
}
|
|
|
|
func New(name string, options interface{}) Widget {
|
|
return Widget{name, options}
|
|
}
|
|
|
|
func Add(name string, widget widgetapi.Widget) {
|
|
Get[name] = widget
|
|
}
|
|
|
|
func Create(ctx context.Context, t terminalapi.Terminal, widgets ...Widget) {
|
|
for _, widget := range widgets {
|
|
typeName := reflect.TypeOf(widget.options).Name()
|
|
factory := widgetOptions[typeName]
|
|
if factory == nil {
|
|
panic(fmt.Errorf("%s: widget factory not found for type: %s", widget.name, typeName))
|
|
}
|
|
Add(widget.name, widgetOptions[reflect.TypeOf(widget.options).Name()](ctx, t, widget.options))
|
|
}
|
|
}
|