diff options
author | Robin Jarry <robin@jarry.cc> | 2023-12-04 23:39:49 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-01-20 21:56:25 +0100 |
commit | 159fb38daf5336758abc425447cf2c2ed51de59a (patch) | |
tree | e7be3bea878b12e441332f89d7bc3c63db477c05 /commands/patch | |
parent | d2817371867e94b621de4054b235d53312db8073 (diff) | |
download | aerc-159fb38daf5336758abc425447cf2c2ed51de59a.tar.gz |
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 <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
Tested-by: Johannes Thyssen Tishman <johannes@thyssentishman.com>
Diffstat (limited to 'commands/patch')
-rw-r--r-- | commands/patch/apply.go | 4 | ||||
-rw-r--r-- | commands/patch/cd.go | 5 | ||||
-rw-r--r-- | commands/patch/delete.go | 4 | ||||
-rw-r--r-- | commands/patch/find.go | 4 | ||||
-rw-r--r-- | commands/patch/init.go | 5 | ||||
-rw-r--r-- | commands/patch/list.go | 5 | ||||
-rw-r--r-- | commands/patch/patch.go | 51 | ||||
-rw-r--r-- | commands/patch/rebase.go | 5 | ||||
-rw-r--r-- | commands/patch/remove.go | 4 | ||||
-rw-r--r-- | commands/patch/switch.go | 4 | ||||
-rw-r--r-- | commands/patch/term.go | 4 |
11 files changed, 74 insertions, 21 deletions
diff --git a/commands/patch/apply.go b/commands/patch/apply.go index 2806b7d1..5ffe86d1 100644 --- a/commands/patch/apply.go +++ b/commands/patch/apply.go @@ -24,6 +24,10 @@ func init() { register(Apply{}) } +func (Apply) Context() commands.CommandContext { + return commands.MESSAGE +} + func (Apply) Aliases() []string { return []string{"apply"} } diff --git a/commands/patch/cd.go b/commands/patch/cd.go index 86a2db3e..bbca1eba 100644 --- a/commands/patch/cd.go +++ b/commands/patch/cd.go @@ -6,6 +6,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/lib/pama" ) @@ -15,6 +16,10 @@ func init() { register(Cd{}) } +func (Cd) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Cd) Aliases() []string { return []string{"cd"} } diff --git a/commands/patch/delete.go b/commands/patch/delete.go index 2e51ab7c..24351f7c 100644 --- a/commands/patch/delete.go +++ b/commands/patch/delete.go @@ -18,6 +18,10 @@ func init() { register(Delete{}) } +func (Delete) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Delete) Aliases() []string { return []string{"delete"} } diff --git a/commands/patch/find.go b/commands/patch/find.go index 9579e1f8..a508551d 100644 --- a/commands/patch/find.go +++ b/commands/patch/find.go @@ -23,6 +23,10 @@ func init() { register(Find{}) } +func (Find) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Find) Aliases() []string { return []string{"find"} } diff --git a/commands/patch/init.go b/commands/patch/init.go index 328fcd9f..de0c6e45 100644 --- a/commands/patch/init.go +++ b/commands/patch/init.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/lib/pama" ) @@ -17,6 +18,10 @@ func init() { register(Init{}) } +func (Init) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Init) Aliases() []string { return []string{"init"} } diff --git a/commands/patch/list.go b/commands/patch/list.go index 6dcc06e8..73461f2e 100644 --- a/commands/patch/list.go +++ b/commands/patch/list.go @@ -8,6 +8,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/config" "git.sr.ht/~rjarry/aerc/lib/pama" "git.sr.ht/~rjarry/aerc/lib/pama/models" @@ -24,6 +25,10 @@ func init() { register(List{}) } +func (List) Context() commands.CommandContext { + return commands.GLOBAL +} + func (List) Aliases() []string { return []string{"list", "ls"} } diff --git a/commands/patch/patch.go b/commands/patch/patch.go index 0807c1fa..25d7850a 100644 --- a/commands/patch/patch.go +++ b/commands/patch/patch.go @@ -8,32 +8,31 @@ import ( "git.sr.ht/~rjarry/go-opt" ) -var ( - PatchCommands *commands.Commands - subCommands *commands.Commands -) +var subCommands map[string]commands.Command func register(cmd commands.Command) { if subCommands == nil { - subCommands = commands.NewCommands() + subCommands = make(map[string]commands.Command) + } + for _, alias := range cmd.Aliases() { + if subCommands[alias] != nil { + panic("duplicate sub command alias: " + alias) + } + subCommands[alias] = cmd } - subCommands.Register(cmd) } -func registerPatch(cmd commands.Command) { - if PatchCommands == nil { - PatchCommands = commands.NewCommands() - } - PatchCommands.Register(cmd) +type Patch struct { + SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"` + Args string `opt:"..." required:"false" complete:"CompleteSubArgs"` } func init() { - registerPatch(Patch{}) + commands.Register(Patch{}) } -type Patch struct { - SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"` - Args string `opt:"..." required:"false" complete:"CompleteSubArgs"` +func (Patch) Context() commands.CommandContext { + return commands.GLOBAL } func (Patch) Aliases() []string { @@ -41,16 +40,26 @@ func (Patch) Aliases() []string { } func (p *Patch) ParseSub(arg string) error { - p.SubCmd = subCommands.ByName(arg) - if p.SubCmd == nil { - return fmt.Errorf("%s unknown sub-command", arg) + cmd, ok := subCommands[arg] + if ok { + context := commands.CurrentContext() + if cmd.Context()&context != 0 { + p.SubCmd = cmd + return nil + } } - return nil + return fmt.Errorf("%s unknown sub-command", arg) } func (*Patch) CompleteSubNames(arg string) []string { - options := subCommands.Names() - return commands.FilterList(options, arg, nil) + context := commands.CurrentContext() + options := make([]string, 0, len(subCommands)) + for alias, cmd := range subCommands { + if cmd.Context()&context != 0 { + options = append(options, alias) + } + } + return commands.FilterList(options, arg, commands.QuoteSpace) } func (p *Patch) CompleteSubArgs(arg string) []string { diff --git a/commands/patch/rebase.go b/commands/patch/rebase.go index 10da2a63..6ef43299 100644 --- a/commands/patch/rebase.go +++ b/commands/patch/rebase.go @@ -12,6 +12,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/config" "git.sr.ht/~rjarry/aerc/lib/pama" "git.sr.ht/~rjarry/aerc/lib/pama/models" @@ -27,6 +28,10 @@ func init() { register(Rebase{}) } +func (Rebase) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Rebase) Aliases() []string { return []string{"rebase"} } diff --git a/commands/patch/remove.go b/commands/patch/remove.go index 6697177b..29849e51 100644 --- a/commands/patch/remove.go +++ b/commands/patch/remove.go @@ -18,6 +18,10 @@ func init() { register(Remove{}) } +func (Remove) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Remove) Aliases() []string { return []string{"remove"} } diff --git a/commands/patch/switch.go b/commands/patch/switch.go index b1193be7..aab2acb6 100644 --- a/commands/patch/switch.go +++ b/commands/patch/switch.go @@ -18,6 +18,10 @@ func init() { register(Switch{}) } +func (Switch) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Switch) Aliases() []string { return []string{"switch"} } diff --git a/commands/patch/term.go b/commands/patch/term.go index f2c50b15..f9f79be1 100644 --- a/commands/patch/term.go +++ b/commands/patch/term.go @@ -13,6 +13,10 @@ func init() { register(Term{}) } +func (Term) Context() commands.CommandContext { + return commands.GLOBAL +} + func (Term) Aliases() []string { return []string{"term"} } |