aboutsummaryrefslogtreecommitdiffstats
path: root/commands/commands.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-21 16:30:23 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-21 16:30:23 -0400
commit8126d82956636a2525263e2d0d985d721fdb8074 (patch)
tree2c5f5c2e2fb2e5ecfb4c12eb8ef6bc575edc39f8 /commands/commands.go
parentfe79a9a5879936a7f5b16cc6a8be1d93ec1bfae7 (diff)
downloadaerc-8126d82956636a2525263e2d0d985d721fdb8074.tar.gz
Add context-specific commands
Diffstat (limited to 'commands/commands.go')
-rw-r--r--commands/commands.go36
1 files changed, 25 insertions, 11 deletions
diff --git a/commands/commands.go b/commands/commands.go
index 2890cdd4..a2589f87 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -10,18 +10,32 @@ import (
type AercCommand func(aerc *widgets.Aerc, args []string) error
-var (
- commands map[string]AercCommand
-)
+type Commands map[string]AercCommand
-func Register(name string, cmd AercCommand) {
- if commands == nil {
- commands = make(map[string]AercCommand)
- }
- commands[name] = cmd
+func NewCommands() *Commands {
+ cmds := Commands(make(map[string]AercCommand))
+ return &cmds
+}
+
+func (cmds *Commands) dict() map[string]AercCommand {
+ return map[string]AercCommand(*cmds)
+}
+
+func (cmds *Commands) Register(name string, cmd AercCommand) {
+ cmds.dict()[name] = cmd
+}
+
+type NoSuchCommand string
+
+func (err NoSuchCommand) Error() string {
+ return "Unknown command " + string(err)
+}
+
+type CommandSource interface {
+ Commands() *Commands
}
-func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
+func (cmds *Commands) ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
args, err := shlex.Split(cmd)
if err != nil {
return err
@@ -29,8 +43,8 @@ func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
if len(args) == 0 {
return errors.New("Expected a command.")
}
- if fn, ok := commands[args[0]]; ok {
+ if fn, ok := cmds.dict()[args[0]]; ok {
return fn(aerc, args)
}
- return errors.New("Unknown command " + args[0])
+ return NoSuchCommand(args[0])
}