about summary refs log tree commit diff stats
path: root/lib/ui/textinput.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ui/textinput.go')
-rw-r--r--lib/ui/textinput.go84
1 files changed, 74 insertions, 10 deletions
diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go
index 2feeb84..e5a2337 100644
--- a/lib/ui/textinput.go
+++ b/lib/ui/textinput.go
@@ -5,20 +5,23 @@ import (
 	"github.com/mattn/go-runewidth"
 )
 
-// TODO: Attach history and tab completion providers
+// TODO: Attach history providers
 // TODO: scrolling
 
 type TextInput struct {
 	Invalidatable
-	cells    int
-	ctx      *Context
-	focus    bool
-	index    int
-	password bool
-	prompt   string
-	scroll   int
-	text     []rune
-	change   []func(ti *TextInput)
+	cells         int
+	ctx           *Context
+	focus         bool
+	index         int
+	password      bool
+	prompt        string
+	scroll        int
+	text          []rune
+	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
@@ -42,6 +45,12 @@ func (ti *TextInput) Prompt(prompt string) *TextInput {
 	return ti
 }
 
+func (ti *TextInput) TabComplete(
+	tabcomplete func(s string) []string) *TextInput {
+	ti.tabcomplete = tabcomplete
+	return ti
+}
+
 func (ti *TextInput) String() string {
 	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() {
 	for _, change := range ti.change {
 		change(ti)
@@ -176,32 +220,52 @@ func (ti *TextInput) Event(event tcell.Event) bool {
 	case *tcell.EventKey:
 		switch event.Key() {
 		case tcell.KeyBackspace, tcell.KeyBackspace2:
+			ti.invalidateCompletions()
 			ti.backspace()
 		case tcell.KeyCtrlD, tcell.KeyDelete:
+			ti.invalidateCompletions()
 			ti.deleteChar()
 		case tcell.KeyCtrlB, tcell.KeyLeft:
+			ti.invalidateCompletions()
 			if ti.index > 0 {
 				ti.index--
 				ti.ensureScroll()
 				ti.Invalidate()
 			}
 		case tcell.KeyCtrlF, tcell.KeyRight:
+			ti.invalidateCompletions()
 			if ti.index < len(ti.text) {
 				ti.index++
 				ti.ensureScroll()
 				ti.Invalidate()
 			}
 		case tcell.KeyCtrlA, tcell.KeyHome:
+			ti.invalidateCompletions()
 			ti.index = 0
 			ti.ensureScroll()
 			ti.Invalidate()
 		case tcell.KeyCtrlE, tcell.KeyEnd:
+			ti.invalidateCompletions()
 			ti.index = len(ti.text)
 			ti.ensureScroll()
 			ti.Invalidate()
 		case tcell.KeyCtrlW:
+			ti.invalidateCompletions()
 			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:
+			ti.invalidateCompletions()
 			ti.insert(event.Rune())
 		}
 	}
d='n301' href='#n301'>301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385