From cded067bc3919a77b17feedd877e4590e7c95f4a Mon Sep 17 00:00:00 2001 From: Jeffas Date: Fri, 26 Jul 2019 14:29:40 +0100 Subject: Add tab completion to textinputs This adds tab completion to textinput components. They can be configured with a completion function. This function is called when the user presses . The first completion is initially shown to the user inserted into the text. Repeated presses of or cycle through the completions list. The completions list is invalidated when any other non-tab-like key is pressed. Also changed is some logic for current completion generation so that all available commands are returned when is pressed with no current text and similarly for arguments of commands. --- commands/commands.go | 17 ++++++++++++++--- commands/ct.go | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'commands') diff --git a/commands/commands.go b/commands/commands.go index c6f149f..3f7fbcd 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -2,6 +2,7 @@ package commands import ( "errors" + "sort" "strings" "unicode" @@ -73,12 +74,19 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string { } if len(args) == 0 { - return nil + names := cmds.Names() + sort.Strings(names) + return names } - if len(args) > 1 { + if len(args) > 1 || cmd[len(cmd)-1] == ' ' { if cmd, ok := cmds.dict()[args[0]]; ok { - completions := cmd.Complete(aerc, args[1:]) + var completions []string + if len(args) > 1 { + completions = cmd.Complete(aerc, args[1:]) + } else { + completions = cmd.Complete(aerc, []string{}) + } if completions != nil && len(completions) == 0 { return nil } @@ -109,6 +117,9 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string { func GetFolders(aerc *widgets.Aerc, args []string) []string { out := make([]string, 0) lower_only := false + if len(args) == 0 { + return aerc.SelectedAccount().Directories().List() + } for _, rune := range args[0] { lower_only = lower_only || unicode.IsLower(rune) } diff --git a/commands/ct.go b/commands/ct.go index ab2993d..19fb63a 100644 --- a/commands/ct.go +++ b/commands/ct.go @@ -19,6 +19,9 @@ func (_ ChangeTab) Aliases() []string { } func (_ ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string { + if len(args) == 0 { + return aerc.TabNames() + } out := make([]string, 0) for _, tab := range aerc.TabNames() { if strings.HasPrefix(tab, args[0]) { -- cgit v1.2.3