diff options
-rw-r--r-- | commands/commands.go | 7 | ||||
-rw-r--r-- | commands/patch/patch.go | 71 | ||||
-rw-r--r-- | main.go | 10 |
3 files changed, 86 insertions, 2 deletions
diff --git a/commands/commands.go b/commands/commands.go index 2893aa82..3270bee6 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -185,7 +185,12 @@ func GetCompletions( ) (options []string, prefix string) { // copy zeroed struct tmp := reflect.New(reflect.TypeOf(cmd)).Interface().(Command) - spec := opt.NewCmdSpec(args.Arg(0), tmp) + s, err := args.ArgSafe(0) + if err != nil { + log.Errorf("completions error: %v", err) + return options, prefix + } + spec := opt.NewCmdSpec(s, tmp) return spec.GetCompletions(args) } 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()) +} @@ -22,6 +22,7 @@ import ( "git.sr.ht/~rjarry/aerc/commands/compose" "git.sr.ht/~rjarry/aerc/commands/msg" "git.sr.ht/~rjarry/aerc/commands/msgview" + "git.sr.ht/~rjarry/aerc/commands/patch" "git.sr.ht/~rjarry/aerc/commands/terminal" "git.sr.ht/~rjarry/aerc/config" "git.sr.ht/~rjarry/aerc/lib/crypto" @@ -41,25 +42,32 @@ func getCommands(selected ui.Drawable) []*commands.Commands { account.AccountCommands, msg.MessageCommands, commands.GlobalCommands, + patch.PatchCommands, } case *app.Composer: return []*commands.Commands{ compose.ComposeCommands, commands.GlobalCommands, + patch.PatchCommands, } case *app.MessageViewer: return []*commands.Commands{ msgview.MessageViewCommands, msg.MessageCommands, commands.GlobalCommands, + patch.PatchCommands, } case *app.Terminal: return []*commands.Commands{ terminal.TerminalCommands, commands.GlobalCommands, + patch.PatchCommands, } default: - return []*commands.Commands{commands.GlobalCommands} + return []*commands.Commands{ + commands.GlobalCommands, + patch.PatchCommands, + } } } |