From 2a0961701c4cabecc53d134ed1782e5612e64580 Mon Sep 17 00:00:00 2001 From: Gregory Mullen Date: Thu, 27 Jun 2019 10:33:11 -0700 Subject: Implement basic tab completion support Tab completion currently only works on commands. Contextual completion will be added in the future. --- widgets/exline.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'widgets/exline.go') diff --git a/widgets/exline.go b/widgets/exline.go index ff18d13..e984ee1 100644 --- a/widgets/exline.go +++ b/widgets/exline.go @@ -8,17 +8,21 @@ import ( type ExLine struct { ui.Invalidatable - cancel func() - commit func(cmd string) - input *ui.TextInput + cancel func() + commit func(cmd string) + tabcomplete func(cmd string) []string + input *ui.TextInput } -func NewExLine(commit func(cmd string), cancel func()) *ExLine { +func NewExLine(commit func(cmd string), cancel func(), + tabcomplete func(cmd string) []string) *ExLine { + input := ui.NewTextInput("").Prompt(":") exline := &ExLine{ - cancel: cancel, - commit: commit, - input: input, + cancel: cancel, + commit: commit, + tabcomplete: tabcomplete, + input: input, } input.OnInvalidate(func(d ui.Drawable) { exline.Invalidate() @@ -48,6 +52,12 @@ func (ex *ExLine) Event(event tcell.Event) bool { case tcell.KeyEsc, tcell.KeyCtrlC: ex.input.Focus(false) 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: return ex.input.Event(event) } -- cgit v1.2.3