aerc/commands/commands.go

37 lines
657 B
Go
Raw Normal View History

2019-03-11 01:15:24 +00:00
package commands
import (
"errors"
2019-03-11 01:23:22 +00:00
"github.com/google/shlex"
2019-03-11 01:15:24 +00:00
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
2019-03-11 01:23:22 +00:00
type AercCommand func(aerc *widgets.Aerc, args []string) error
2019-03-11 01:15:24 +00:00
var (
commands map[string]AercCommand
)
func Register(name string, cmd AercCommand) {
2019-03-15 02:34:34 +00:00
if commands == nil {
commands = make(map[string]AercCommand)
}
2019-03-11 01:15:24 +00:00
commands[name] = cmd
}
func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
2019-03-11 01:23:22 +00: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 01:15:24 +00:00
}
2019-03-11 01:23:22 +00:00
return errors.New("Unknown command " + args[0])
2019-03-11 01:15:24 +00:00
}