cded067bc3
This adds tab completion to textinput components. They can be configured with a completion function. This function is called when the user presses <tab>. The first completion is initially shown to the user inserted into the text. Repeated presses of <tab> or <backtab> 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 <tab> is pressed with no current text and similarly for arguments of commands.
51 lines
913 B
Go
51 lines
913 B
Go
package commands
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
)
|
|
|
|
type ChangeTab struct{}
|
|
|
|
func init() {
|
|
register(ChangeTab{})
|
|
}
|
|
|
|
func (_ ChangeTab) Aliases() []string {
|
|
return []string{"ct", "change-tab"}
|
|
}
|
|
|
|
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]) {
|
|
out = append(out, tab)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (_ ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
|
|
if len(args) != 2 {
|
|
return errors.New(fmt.Sprintf("Usage: %s <tab>", args[0]))
|
|
}
|
|
|
|
if args[1] == "-" {
|
|
ok := aerc.SelectPreviousTab()
|
|
if !ok {
|
|
return errors.New("No previous tab to return to")
|
|
}
|
|
} else {
|
|
ok := aerc.SelectTab(args[1])
|
|
if !ok {
|
|
return errors.New("No tab with that name")
|
|
}
|
|
}
|
|
return nil
|
|
}
|