dynamo-browse/internal/common/ui/commandctrl/commandctrl.go
Leon Mika cc7ead496f awstools: Fixed some bugs with the item view UI model
- The item view model is now being updated when the item itself is being updated
- Fixed a NPE when the item view model receives a nil item
2022-07-18 08:04:03 +10:00

76 lines
1.6 KiB
Go

package commandctrl
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/pkg/errors"
"log"
"strings"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/shellwords"
)
type CommandController struct {
commandList *CommandContext
}
func NewCommandController() *CommandController {
return &CommandController{
commandList: nil,
}
}
func (c *CommandController) AddCommands(ctx *CommandContext) {
ctx.parent = c.commandList
c.commandList = ctx
}
func (c *CommandController) Prompt() tea.Cmd {
return func() tea.Msg {
return events.PromptForInputMsg{
Prompt: ":",
OnDone: func(value string) tea.Cmd {
return c.Execute(value)
},
}
}
}
func (c *CommandController) Execute(commandInput string) tea.Cmd {
input := strings.TrimSpace(commandInput)
if input == "" {
return nil
}
tokens := shellwords.Split(input)
command := c.lookupCommand(tokens[0])
if command == nil {
log.Println("No such command: ", tokens)
return events.SetError(errors.New("no such command: " + tokens[0]))
}
return command(tokens[1:])
}
func (c *CommandController) Alias(commandName string) Command {
return func(args []string) tea.Cmd {
command := c.lookupCommand(commandName)
if command == nil {
log.Println("No such command: ", commandName)
return events.SetError(errors.New("no such command: " + commandName))
}
return command(args)
}
}
func (c *CommandController) lookupCommand(name string) Command {
for ctx := c.commandList; ctx != nil; ctx = ctx.parent {
log.Printf("Looking in command list: %v", c.commandList)
if cmd, ok := ctx.Commands[name]; ok {
return cmd
}
}
return nil
}