ctrlret: replaced return types of controllers from tea.Cmd to tea.Msg
This dramatically cuts downs the number of closures.
This commit is contained in:
parent
931b11cd0d
commit
5b6bf1f0ae
17 changed files with 472 additions and 562 deletions
|
|
@ -25,18 +25,16 @@ func (c *CommandController) AddCommands(ctx *CommandContext) {
|
|||
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) Prompt() tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: ":",
|
||||
OnDone: func(value string) tea.Msg {
|
||||
return c.Execute(value)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CommandController) Execute(commandInput string) tea.Cmd {
|
||||
func (c *CommandController) Execute(commandInput string) tea.Msg {
|
||||
input := strings.TrimSpace(commandInput)
|
||||
if input == "" {
|
||||
return nil
|
||||
|
|
@ -46,18 +44,18 @@ func (c *CommandController) Execute(commandInput string) tea.Cmd {
|
|||
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 events.Error(errors.New("no such command: " + tokens[0]))
|
||||
}
|
||||
|
||||
return command(tokens[1:])
|
||||
}
|
||||
|
||||
func (c *CommandController) Alias(commandName string) Command {
|
||||
return func(args []string) tea.Cmd {
|
||||
return func(args []string) tea.Msg {
|
||||
command := c.lookupCommand(commandName)
|
||||
if command == nil {
|
||||
log.Println("No such command: ", commandName)
|
||||
return events.SetError(errors.New("no such command: " + commandName))
|
||||
return events.Error(errors.New("no such command: " + commandName))
|
||||
}
|
||||
|
||||
return command(args)
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
package commandctrl_test
|
||||
|
||||
import (
|
||||
"github.com/lmika/audax/internal/common/ui/events"
|
||||
"testing"
|
||||
|
||||
"github.com/lmika/audax/internal/common/ui/commandctrl"
|
||||
"github.com/lmika/audax/internal/common/ui/events"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
|
@ -12,7 +12,7 @@ func TestCommandController_Prompt(t *testing.T) {
|
|||
t.Run("prompt user for a command", func(t *testing.T) {
|
||||
cmd := commandctrl.NewCommandController()
|
||||
|
||||
res := cmd.Prompt()()
|
||||
res := cmd.Prompt()
|
||||
|
||||
promptForInputMsg, ok := res.(events.PromptForInputMsg)
|
||||
assert.True(t, ok)
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@ package commandctrl
|
|||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
type Command func(args []string) tea.Cmd
|
||||
type Command func(args []string) tea.Msg
|
||||
|
||||
func NoArgCommand(cmd tea.Cmd) Command {
|
||||
return func(args []string) tea.Cmd {
|
||||
return cmd
|
||||
return func(args []string) tea.Msg {
|
||||
return cmd()
|
||||
}
|
||||
}
|
||||
|
||||
type CommandContext struct {
|
||||
Commands map[string]Command
|
||||
|
||||
parent *CommandContext
|
||||
parent *CommandContext
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue