From 159fb38daf5336758abc425447cf2c2ed51de59a Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 4 Dec 2023 23:39:49 +0100 Subject: commands: refactor registration Register all commands with the same function and store them in the same map. Use bit flags to determine in which contexts each command should be available. Remove duplicate commands now that the same command can be exposed in multiple contexts. Refactor API to allow executing commands from other commands without import cycles. Signed-off-by: Robin Jarry Tested-by: Bence Ferdinandy Tested-by: Johannes Thyssen Tishman --- commands/compose/abort.go | 7 ++++++- commands/compose/attach-key.go | 7 ++++++- commands/compose/attach.go | 6 +++++- commands/compose/cc-bcc.go | 7 ++++++- commands/compose/compose.go | 14 -------------- commands/compose/detach.go | 6 +++++- commands/compose/edit.go | 7 ++++++- commands/compose/encrypt.go | 7 ++++++- commands/compose/header.go | 6 +++++- commands/compose/multipart.go | 6 +++++- commands/compose/next-field.go | 7 ++++++- commands/compose/postpone.go | 6 +++++- commands/compose/send.go | 6 +++++- commands/compose/sign.go | 7 ++++++- commands/compose/switch.go | 6 +++++- 15 files changed, 77 insertions(+), 28 deletions(-) delete mode 100644 commands/compose/compose.go (limited to 'commands/compose') diff --git a/commands/compose/abort.go b/commands/compose/abort.go index cde43f46..f446c306 100644 --- a/commands/compose/abort.go +++ b/commands/compose/abort.go @@ -2,12 +2,17 @@ package compose import ( "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type Abort struct{} func init() { - register(Abort{}) + commands.Register(Abort{}) +} + +func (Abort) Context() commands.CommandContext { + return commands.COMPOSE } func (Abort) Aliases() []string { diff --git a/commands/compose/attach-key.go b/commands/compose/attach-key.go index 6ba5eae0..047b9c08 100644 --- a/commands/compose/attach-key.go +++ b/commands/compose/attach-key.go @@ -2,12 +2,17 @@ package compose import ( "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type AttachKey struct{} func init() { - register(AttachKey{}) + commands.Register(AttachKey{}) +} + +func (AttachKey) Context() commands.CommandContext { + return commands.COMPOSE } func (AttachKey) Aliases() []string { diff --git a/commands/compose/attach.go b/commands/compose/attach.go index 52ce55cb..cb59eb5f 100644 --- a/commands/compose/attach.go +++ b/commands/compose/attach.go @@ -28,7 +28,11 @@ type Attach struct { } func init() { - register(Attach{}) + commands.Register(Attach{}) +} + +func (Attach) Context() commands.CommandContext { + return commands.COMPOSE } func (Attach) Aliases() []string { diff --git a/commands/compose/cc-bcc.go b/commands/compose/cc-bcc.go index dd5e9614..d54a9895 100644 --- a/commands/compose/cc-bcc.go +++ b/commands/compose/cc-bcc.go @@ -2,6 +2,7 @@ package compose import ( "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type CC struct { @@ -9,7 +10,11 @@ type CC struct { } func init() { - register(CC{}) + commands.Register(CC{}) +} + +func (CC) Context() commands.CommandContext { + return commands.COMPOSE } func (CC) Aliases() []string { diff --git a/commands/compose/compose.go b/commands/compose/compose.go deleted file mode 100644 index f325e4e2..00000000 --- a/commands/compose/compose.go +++ /dev/null @@ -1,14 +0,0 @@ -package compose - -import ( - "git.sr.ht/~rjarry/aerc/commands" -) - -var ComposeCommands *commands.Commands - -func register(cmd commands.Command) { - if ComposeCommands == nil { - ComposeCommands = commands.NewCommands() - } - ComposeCommands.Register(cmd) -} diff --git a/commands/compose/detach.go b/commands/compose/detach.go index 0ac3334e..36c7c314 100644 --- a/commands/compose/detach.go +++ b/commands/compose/detach.go @@ -12,7 +12,11 @@ type Detach struct { } func init() { - register(Detach{}) + commands.Register(Detach{}) +} + +func (Detach) Context() commands.CommandContext { + return commands.COMPOSE } func (Detach) Aliases() []string { diff --git a/commands/compose/edit.go b/commands/compose/edit.go index 1929f45a..45c55046 100644 --- a/commands/compose/edit.go +++ b/commands/compose/edit.go @@ -4,6 +4,7 @@ import ( "errors" "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/config" ) @@ -13,7 +14,11 @@ type Edit struct { } func init() { - register(Edit{}) + commands.Register(Edit{}) +} + +func (Edit) Context() commands.CommandContext { + return commands.COMPOSE } func (Edit) Aliases() []string { diff --git a/commands/compose/encrypt.go b/commands/compose/encrypt.go index ee902f6e..5e52f821 100644 --- a/commands/compose/encrypt.go +++ b/commands/compose/encrypt.go @@ -2,12 +2,17 @@ package compose import ( "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type Encrypt struct{} func init() { - register(Encrypt{}) + commands.Register(Encrypt{}) +} + +func (Encrypt) Context() commands.CommandContext { + return commands.COMPOSE } func (Encrypt) Aliases() []string { diff --git a/commands/compose/header.go b/commands/compose/header.go index 3283d4e3..839896c3 100644 --- a/commands/compose/header.go +++ b/commands/compose/header.go @@ -26,7 +26,11 @@ var headers = []string{ } func init() { - register(Header{}) + commands.Register(Header{}) +} + +func (Header) Context() commands.CommandContext { + return commands.COMPOSE } func (Header) Aliases() []string { diff --git a/commands/compose/multipart.go b/commands/compose/multipart.go index a004ea2b..13fe8ca3 100644 --- a/commands/compose/multipart.go +++ b/commands/compose/multipart.go @@ -15,7 +15,11 @@ type Multipart struct { } func init() { - register(Multipart{}) + commands.Register(Multipart{}) +} + +func (Multipart) Context() commands.CommandContext { + return commands.COMPOSE } func (Multipart) Aliases() []string { diff --git a/commands/compose/next-field.go b/commands/compose/next-field.go index 396c1ce1..6c96cd94 100644 --- a/commands/compose/next-field.go +++ b/commands/compose/next-field.go @@ -2,12 +2,17 @@ package compose import ( "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type NextPrevField struct{} func init() { - register(NextPrevField{}) + commands.Register(NextPrevField{}) +} + +func (NextPrevField) Context() commands.CommandContext { + return commands.COMPOSE } func (NextPrevField) Aliases() []string { diff --git a/commands/compose/postpone.go b/commands/compose/postpone.go index 098b7039..a869417b 100644 --- a/commands/compose/postpone.go +++ b/commands/compose/postpone.go @@ -18,7 +18,11 @@ type Postpone struct { } func init() { - register(Postpone{}) + commands.Register(Postpone{}) +} + +func (Postpone) Context() commands.CommandContext { + return commands.COMPOSE } func (Postpone) Aliases() []string { diff --git a/commands/compose/send.go b/commands/compose/send.go index c642c356..dfd531ee 100644 --- a/commands/compose/send.go +++ b/commands/compose/send.go @@ -33,7 +33,11 @@ type Send struct { } func init() { - register(Send{}) + commands.Register(Send{}) +} + +func (Send) Context() commands.CommandContext { + return commands.COMPOSE } func (Send) Aliases() []string { diff --git a/commands/compose/sign.go b/commands/compose/sign.go index faeceed4..692ef108 100644 --- a/commands/compose/sign.go +++ b/commands/compose/sign.go @@ -4,12 +4,17 @@ import ( "time" "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" ) type Sign struct{} func init() { - register(Sign{}) + commands.Register(Sign{}) +} + +func (Sign) Context() commands.CommandContext { + return commands.COMPOSE } func (Sign) Aliases() []string { diff --git a/commands/compose/switch.go b/commands/compose/switch.go index c71716e0..c9d1013c 100644 --- a/commands/compose/switch.go +++ b/commands/compose/switch.go @@ -18,7 +18,11 @@ type SwitchAccount struct { } func init() { - register(SwitchAccount{}) + commands.Register(SwitchAccount{}) +} + +func (SwitchAccount) Context() commands.CommandContext { + return commands.COMPOSE } func (SwitchAccount) Aliases() []string { -- cgit