table-select: cleanup

This commit is contained in:
Leon Mika 2022-03-28 21:36:47 +11:00
parent 6f323fa4cf
commit 9709e6aed1
12 changed files with 68 additions and 639 deletions

View file

@ -1,49 +1,45 @@
package commandctrl
import (
"context"
tea "github.com/charmbracelet/bubbletea"
"strings"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/shellwords"
"github.com/pkg/errors"
)
type CommandController struct {
commands map[string]uimodels.Operation
commands map[string]Command
}
func NewCommandController(commands map[string]uimodels.Operation) *CommandController {
func NewCommandController(commands map[string]Command) *CommandController {
return &CommandController{
commands: commands,
}
}
func (c *CommandController) Prompt() uimodels.Operation {
return uimodels.OperationFn(func(ctx context.Context) error {
uiCtx := uimodels.Ctx(ctx)
uiCtx.Send(events.PromptForInputMsg{
func (c *CommandController) Prompt() tea.Cmd {
return func() tea.Msg {
return events.PromptForInputMsg{
Prompt: ":",
// OnDone: c.Execute(),
})
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
})
}
func (c *CommandController) Execute() uimodels.Operation {
return uimodels.OperationFn(func(ctx context.Context) error {
input := strings.TrimSpace(uimodels.PromptValue(ctx))
if input == "" {
return nil
}
tokens := shellwords.Split(input)
command, ok := c.commands[tokens[0]]
if !ok {
return errors.New("no such command: " + tokens[0])
}
return command.Execute(WithCommandArgs(ctx, tokens[1:]))
})
}
tokens := shellwords.Split(input)
command, ok := c.commands[tokens[0]]
if !ok {
return events.SetStatus("no such command: " + tokens[0])
}
return command(tokens)
}

View file

@ -0,0 +1,11 @@
package commandctrl
import tea "github.com/charmbracelet/bubbletea"
type Command func(args []string) tea.Cmd
func NoArgCommand(cmd tea.Cmd) Command {
return func(args []string) tea.Cmd {
return cmd
}
}