aerc/commands/commands.go

37 lines
657 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 Register(name string, cmd AercCommand) {
2019-03-15 03:34:34 +01:00
if commands == nil {
commands = make(map[string]AercCommand)
}
2019-03-11 02:15:24 +01:00
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
}