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 <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.
This commit is contained in:
Jeffas 2019-07-26 14:29:40 +01:00 committed by Drew DeVault
parent aabe3d9b3a
commit cded067bc3
4 changed files with 92 additions and 20 deletions

View file

@ -2,6 +2,7 @@ package commands
import ( import (
"errors" "errors"
"sort"
"strings" "strings"
"unicode" "unicode"
@ -73,12 +74,19 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
} }
if len(args) == 0 { 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 { 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 { if completions != nil && len(completions) == 0 {
return nil return nil
} }
@ -109,6 +117,9 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
func GetFolders(aerc *widgets.Aerc, args []string) []string { func GetFolders(aerc *widgets.Aerc, args []string) []string {
out := make([]string, 0) out := make([]string, 0)
lower_only := false lower_only := false
if len(args) == 0 {
return aerc.SelectedAccount().Directories().List()
}
for _, rune := range args[0] { for _, rune := range args[0] {
lower_only = lower_only || unicode.IsLower(rune) lower_only = lower_only || unicode.IsLower(rune)
} }

View file

@ -19,6 +19,9 @@ func (_ ChangeTab) Aliases() []string {
} }
func (_ ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string { func (_ ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
if len(args) == 0 {
return aerc.TabNames()
}
out := make([]string, 0) out := make([]string, 0)
for _, tab := range aerc.TabNames() { for _, tab := range aerc.TabNames() {
if strings.HasPrefix(tab, args[0]) { if strings.HasPrefix(tab, args[0]) {

View file

@ -5,7 +5,7 @@ import (
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
) )
// TODO: Attach history and tab completion providers // TODO: Attach history providers
// TODO: scrolling // TODO: scrolling
type TextInput struct { type TextInput struct {
@ -19,6 +19,9 @@ type TextInput struct {
scroll int scroll int
text []rune text []rune
change []func(ti *TextInput) change []func(ti *TextInput)
tabcomplete func(s string) []string
completions []string
completeIndex int
} }
// Creates a new TextInput. TextInputs will render a "textbox" in the entire // Creates a new TextInput. TextInputs will render a "textbox" in the entire
@ -42,6 +45,12 @@ func (ti *TextInput) Prompt(prompt string) *TextInput {
return ti return ti
} }
func (ti *TextInput) TabComplete(
tabcomplete func(s string) []string) *TextInput {
ti.tabcomplete = tabcomplete
return ti
}
func (ti *TextInput) String() string { func (ti *TextInput) String() string {
return string(ti.text) return string(ti.text)
} }
@ -161,6 +170,41 @@ func (ti *TextInput) backspace() {
} }
} }
func (ti *TextInput) nextCompletion() {
if ti.completions == nil {
if ti.tabcomplete == nil {
return
}
ti.completions = ti.tabcomplete(ti.StringLeft())
ti.completeIndex = 0
} else {
ti.completeIndex++
if ti.completeIndex >= len(ti.completions) {
ti.completeIndex = 0
}
}
if len(ti.completions) > 0 {
ti.Set(ti.completions[ti.completeIndex] + ti.StringRight())
}
}
func (ti *TextInput) previousCompletion() {
if ti.completions == nil || len(ti.completions) == 0 {
return
}
ti.completeIndex--
if ti.completeIndex < 0 {
ti.completeIndex = len(ti.completions) - 1
}
if len(ti.completions) > 0 {
ti.Set(ti.completions[ti.completeIndex] + ti.StringRight())
}
}
func (ti *TextInput) invalidateCompletions() {
ti.completions = nil
}
func (ti *TextInput) onChange() { func (ti *TextInput) onChange() {
for _, change := range ti.change { for _, change := range ti.change {
change(ti) change(ti)
@ -176,32 +220,52 @@ func (ti *TextInput) Event(event tcell.Event) bool {
case *tcell.EventKey: case *tcell.EventKey:
switch event.Key() { switch event.Key() {
case tcell.KeyBackspace, tcell.KeyBackspace2: case tcell.KeyBackspace, tcell.KeyBackspace2:
ti.invalidateCompletions()
ti.backspace() ti.backspace()
case tcell.KeyCtrlD, tcell.KeyDelete: case tcell.KeyCtrlD, tcell.KeyDelete:
ti.invalidateCompletions()
ti.deleteChar() ti.deleteChar()
case tcell.KeyCtrlB, tcell.KeyLeft: case tcell.KeyCtrlB, tcell.KeyLeft:
ti.invalidateCompletions()
if ti.index > 0 { if ti.index > 0 {
ti.index-- ti.index--
ti.ensureScroll() ti.ensureScroll()
ti.Invalidate() ti.Invalidate()
} }
case tcell.KeyCtrlF, tcell.KeyRight: case tcell.KeyCtrlF, tcell.KeyRight:
ti.invalidateCompletions()
if ti.index < len(ti.text) { if ti.index < len(ti.text) {
ti.index++ ti.index++
ti.ensureScroll() ti.ensureScroll()
ti.Invalidate() ti.Invalidate()
} }
case tcell.KeyCtrlA, tcell.KeyHome: case tcell.KeyCtrlA, tcell.KeyHome:
ti.invalidateCompletions()
ti.index = 0 ti.index = 0
ti.ensureScroll() ti.ensureScroll()
ti.Invalidate() ti.Invalidate()
case tcell.KeyCtrlE, tcell.KeyEnd: case tcell.KeyCtrlE, tcell.KeyEnd:
ti.invalidateCompletions()
ti.index = len(ti.text) ti.index = len(ti.text)
ti.ensureScroll() ti.ensureScroll()
ti.Invalidate() ti.Invalidate()
case tcell.KeyCtrlW: case tcell.KeyCtrlW:
ti.invalidateCompletions()
ti.deleteWord() ti.deleteWord()
case tcell.KeyTab:
if ti.tabcomplete != nil {
ti.nextCompletion()
} else {
ti.insert('\t')
}
ti.Invalidate()
case tcell.KeyBacktab:
if ti.tabcomplete != nil {
ti.previousCompletion()
}
ti.Invalidate()
case tcell.KeyRune: case tcell.KeyRune:
ti.invalidateCompletions()
ti.insert(event.Rune()) ti.insert(event.Rune())
} }
} }

View file

@ -20,7 +20,7 @@ func NewExLine(commit func(cmd string), cancel func(),
tabcomplete func(cmd string) []string, tabcomplete func(cmd string) []string,
cmdHistory lib.History) *ExLine { cmdHistory lib.History) *ExLine {
input := ui.NewTextInput("").Prompt(":") input := ui.NewTextInput("").Prompt(":").TabComplete(tabcomplete)
exline := &ExLine{ exline := &ExLine{
cancel: cancel, cancel: cancel,
commit: commit, commit: commit,
@ -64,12 +64,6 @@ func (ex *ExLine) Event(event tcell.Event) bool {
ex.input.Focus(false) ex.input.Focus(false)
ex.cmdHistory.Reset() ex.cmdHistory.Reset()
ex.cancel() ex.cancel()
case tcell.KeyTab:
complete := ex.tabcomplete(ex.input.StringLeft())
if len(complete) == 1 {
ex.input.Set(complete[0] + " " + ex.input.StringRight())
}
ex.Invalidate()
default: default:
return ex.input.Event(event) return ex.input.Event(event)
} }