commands: implement prompt completion
This patch implements :prompt completion. The completion mechanism only provides completions when there is at least one argument specified (prompt text). The mechanism is based on other commands' completions and works as follows: 1. Attempts to look up a command by the name specified in args[1]. 2.a On success it uses command.Complete. 2.b Otherwise, if total arguments count is lesser or equals than 2 (i.e. no command arguments specified), it attempts to complete the command's name. Additional effort is made to preserve prompt text, which often contains spaces and formatting. Signed-off-by: Sergey Smirnykh <sergey.smirnykh@siborgium.xyz> Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
parent
80f90c0d41
commit
12e8217d1f
2 changed files with 57 additions and 2 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue