63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package commandctrl
|
|
|
|
import (
|
|
"context"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"ucl.lmika.dev/ucl"
|
|
)
|
|
|
|
type commandCtlKeyType struct{}
|
|
|
|
var commandCtlKey = commandCtlKeyType{}
|
|
|
|
type execContext struct {
|
|
ctrl *CommandController
|
|
requestRefresh bool
|
|
}
|
|
|
|
func PostMsg(ctx context.Context, msg tea.Msg) {
|
|
cmdCtl, ok := ctx.Value(commandCtlKey).(*execContext)
|
|
if ok {
|
|
cmdCtl.ctrl.postMessage(msg)
|
|
}
|
|
}
|
|
|
|
func SelectedItemIndex(ctx context.Context) (int, bool) {
|
|
cmdCtl, ok := ctx.Value(commandCtlKey).(*execContext)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
|
|
return cmdCtl.ctrl.uiStateProvider.SelectedItemIndex(), true
|
|
}
|
|
|
|
func SetSelectedItemIndex(ctx context.Context, newIdx int) tea.Msg {
|
|
cmdCtl, ok := ctx.Value(commandCtlKey).(*execContext)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return cmdCtl.ctrl.uiStateProvider.SetSelectedItemIndex(newIdx)
|
|
}
|
|
|
|
func GetInvoker(ctx context.Context) Invoker {
|
|
cmdCtl, ok := ctx.Value(commandCtlKey).(*execContext)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return cmdCtl.ctrl
|
|
}
|
|
|
|
func QueueRefresh(ctx context.Context) {
|
|
cmdCtl, ok := ctx.Value(commandCtlKey).(*execContext)
|
|
if !ok {
|
|
return
|
|
}
|
|
cmdCtl.requestRefresh = true
|
|
}
|
|
|
|
type Invoker interface {
|
|
Invoke(invokable ucl.Invokable, args []any) tea.Msg
|
|
}
|