aboutsummaryrefslogtreecommitdiff
path: root/commands/commands.go
blob: 49a8b46e2d25d6e0f4de5252ec30b00dc4a93dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package commands

import (
	"errors"

	"github.com/google/shlex"

	"git.sr.ht/~sircmpwn/aerc2/widgets"
)

type AercCommand func(aerc *widgets.Aerc, args []string) error

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 {
	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)
	}
	return errors.New("Unknown command " + args[0])
}