aboutsummaryrefslogtreecommitdiffstats
path: root/commands/patch/patch.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2023-11-24 16:03:00 +0100
committerRobin Jarry <robin@jarry.cc>2023-12-30 15:42:09 +0100
commit07c1cefcf62dbd094b6f97061c469264ee089898 (patch)
treed4e5469a2365f1589031e6cc9294c61ecd315276 /commands/patch/patch.go
parentbb4db4ea65c0776f3088709e6dc2510612445226 (diff)
downloadaerc-07c1cefcf62dbd094b6f97061c469264ee089898.tar.gz
patch: create sub-command structure
Create the sub-command structure for the :patch command. Make the :patch command accessible from any context. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/patch/patch.go')
-rw-r--r--commands/patch/patch.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/commands/patch/patch.go b/commands/patch/patch.go
new file mode 100644
index 00000000..0807c1fa
--- /dev/null
+++ b/commands/patch/patch.go
@@ -0,0 +1,71 @@
+package patch
+
+import (
+ "errors"
+ "fmt"
+
+ "git.sr.ht/~rjarry/aerc/commands"
+ "git.sr.ht/~rjarry/go-opt"
+)
+
+var (
+ PatchCommands *commands.Commands
+ subCommands *commands.Commands
+)
+
+func register(cmd commands.Command) {
+ if subCommands == nil {
+ subCommands = commands.NewCommands()
+ }
+ subCommands.Register(cmd)
+}
+
+func registerPatch(cmd commands.Command) {
+ if PatchCommands == nil {
+ PatchCommands = commands.NewCommands()
+ }
+ PatchCommands.Register(cmd)
+}
+
+func init() {
+ registerPatch(Patch{})
+}
+
+type Patch struct {
+ SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"`
+ Args string `opt:"..." required:"false" complete:"CompleteSubArgs"`
+}
+
+func (Patch) Aliases() []string {
+ return []string{"patch"}
+}
+
+func (p *Patch) ParseSub(arg string) error {
+ p.SubCmd = subCommands.ByName(arg)
+ if p.SubCmd == nil {
+ return fmt.Errorf("%s unknown sub-command", arg)
+ }
+ return nil
+}
+
+func (*Patch) CompleteSubNames(arg string) []string {
+ options := subCommands.Names()
+ return commands.FilterList(options, arg, nil)
+}
+
+func (p *Patch) CompleteSubArgs(arg string) []string {
+ if p.SubCmd == nil {
+ return nil
+ }
+ // prepend arbitrary string to arg to work with sub-commands
+ options, _ := commands.GetCompletions(p.SubCmd, opt.LexArgs("a "+arg))
+ return options
+}
+
+func (p Patch) Execute(args []string) error {
+ if p.SubCmd == nil {
+ return errors.New("no subcommand found")
+ }
+ a := opt.QuoteArgs(args[1:]...)
+ return commands.ExecuteCommand(p.SubCmd, a.String())
+}