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/patch.go | |
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/patch.go')
-rw-r--r-- | commands/patch/patch.go | 51 |
1 files changed, 30 insertions, 21 deletions
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 { |