diff --git a/commands/commands.go b/commands/commands.go index 2753686..1aa8e1a 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -37,6 +37,13 @@ func (cmds *Commands) Names() []string { return names } +func (cmds *Commands) ByName(name string) Command { + if cmd, ok := cmds.dict()[name]; ok { + return cmd + } + return nil +} + func (cmds *Commands) Register(cmd Command) { // TODO enforce unique aliases, until then, duplicate each if len(cmd.Aliases()) < 1 { diff --git a/commands/prompt.go b/commands/prompt.go index 6714eb3..1d2242c 100644 --- a/commands/prompt.go +++ b/commands/prompt.go @@ -2,8 +2,8 @@ package commands import ( "fmt" - "git.sr.ht/~rjarry/aerc/widgets" + "strings" ) type Prompt struct{} @@ -17,7 +17,55 @@ func (Prompt) Aliases() []string { } func (Prompt) Complete(aerc *widgets.Aerc, args []string) []string { - return nil // TODO: add completions + argc := len(args) + if argc == 0 { + return nil + } + hascommand := argc > 2 + if argc == 1 { + args = append(args, "") + } + + cmd := GlobalCommands.ByName(args[1]) + var cs []string + if cmd != nil { + cs = cmd.Complete(aerc, args[2:]) + hascommand = true + } else { + if hascommand { + return nil + } + cs = GlobalCommands.GetCompletions(aerc, args[1]) + } + if cs == nil { + return nil + } + + var b strings.Builder + // it seems '' quoting is enough + // to keep quoted arguments in one piece + b.WriteRune('\'') + b.WriteString(args[0]) + b.WriteRune('\'') + b.WriteRune(' ') + if hascommand { + b.WriteString(args[1]) + b.WriteRune(' ') + } + + src := b.String() + b.Reset() + + rs := make([]string, 0, len(cs)) + for _, c := range cs { + b.WriteString(src) + b.WriteString(c) + + rs = append(rs, b.String()) + b.Reset() + } + + return rs } func (Prompt) Execute(aerc *widgets.Aerc, args []string) error {