aerc/commands/commands.go

38 lines
648 B
Go
Raw Normal View History

2019-03-11 02:15:24 +01:00
package commands
import (
"errors"
2019-03-11 02:23:22 +01:00
"github.com/google/shlex"
2019-03-11 02:15:24 +01:00
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
2019-03-11 02:23:22 +01:00
type AercCommand func(aerc *widgets.Aerc, args []string) error
2019-03-11 02:15:24 +01:00
var (
commands map[string]AercCommand
)
func init() {
commands = make(map[string]AercCommand)
}
func Register(name string, cmd AercCommand) {
commands[name] = cmd
}
func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
2019-03-11 02:23:22 +01:00
args, err := shlex.Split(cmd)
if err != nil {
return err
}
if len(args) == 0 {
return errors.New("Expected a command.")
}
if fn, ok := commands[args[0]]; ok {
return fn(aerc, args)
2019-03-11 02:15:24 +01:00
}
2019-03-11 02:23:22 +01:00
return errors.New("Unknown command " + args[0])
2019-03-11 02:15:24 +01:00
}