first commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user