[GH-ISSUE #471] autocomplete doesn't work for single matches #340

Closed
opened 2026-03-04 01:04:09 +03:00 by kerem · 2 comments
Owner

Originally created by @khughitt on GitHub (Jul 17, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/471

Overview

Greetings!

I'm just doing some experimenting with the autocomplete example from the wiki.

While it works quite well when there are >1 matches for the given input, single matches appear to be completely left out.

Example

Ex. (modified version of autocomplete.go from the Wiki):

package main

import (
	"strings"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

// just a few test words
const wordList = "dog,cat,camel"
func main() {
	words := strings.Split(wordList, ",")
	app := tview.NewApplication()
	inputField := tview.NewInputField().
		SetLabel("Enter a word: ").
		SetFieldWidth(30).
		SetDoneFunc(func(key tcell.Key) {
			app.Stop()
		})
	inputField.SetAutocompleteFunc(func(currentText string) (entries []string) {
		if len(currentText) == 0 {
			return
		}
		for _, word := range words {
			if strings.HasPrefix(strings.ToLower(word), strings.ToLower(currentText)) {
				entries = append(entries, word)
			}
		}
		if len(entries) <= 1 {
			entries = nil
		}
		return
	})
	if err := app.SetRoot(inputField, true).Run(); err != nil {
		panic(err)
	}
}

Observed behavior

  • queries starting with "c" work as expected, up until a single match is uniquely identified
  • queries starting with "d" never trigger autocompletion
  • there does not appear to be a way to trigger autocompletion for blank input (i.e. show everything)

To be consistent with how autocompletion behaves in other software (zsh, web browsers, etc.), would it be possible to modify the behavior such that:

  1. autocompletion triggers even for single-matches (i.e. "d" shows "dog")
  2. autocompletion remains open after uniquely identifying a word (otherwise, typing "ca" shows "cat" and "camel", but if you don't select one of the choices right away and add an "m", the autocomplete now disappears, requiring you to either delete a character or manually type the rest of the word from memory)
  3. "tab" / arrow keys can be used to trigger auto-completion, even on an empty input field?

Also, unrelated, but do you think it would be in the desired scope of tview to add support for fzf-style fuzzy input matching in the future? That would be pretty amazing to have support for out-of-the box. If so, I can file a separate issue to keep track of it.

Thanks for all your work on this great library!

System info

  • Arch Linux
  • Go 1.14.5
Originally created by @khughitt on GitHub (Jul 17, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/471 **Overview** Greetings! I'm just doing some experimenting with the [autocomplete example](https://github.com/rivo/tview/blob/master/demos/inputfield/autocomplete.go) from the wiki. While it works quite well when there are >1 matches for the given input, single matches appear to be completely left out. **Example** Ex. (modified version of `autocomplete.go` from the Wiki): ```go package main import ( "strings" "github.com/gdamore/tcell" "github.com/rivo/tview" ) // just a few test words const wordList = "dog,cat,camel" func main() { words := strings.Split(wordList, ",") app := tview.NewApplication() inputField := tview.NewInputField(). SetLabel("Enter a word: "). SetFieldWidth(30). SetDoneFunc(func(key tcell.Key) { app.Stop() }) inputField.SetAutocompleteFunc(func(currentText string) (entries []string) { if len(currentText) == 0 { return } for _, word := range words { if strings.HasPrefix(strings.ToLower(word), strings.ToLower(currentText)) { entries = append(entries, word) } } if len(entries) <= 1 { entries = nil } return }) if err := app.SetRoot(inputField, true).Run(); err != nil { panic(err) } } ``` **Observed behavior** - queries starting with "c" work as expected, up until a single match is uniquely identified - queries starting with "d" never trigger autocompletion - there does not appear to be a way to trigger autocompletion for blank input (i.e. show everything) To be consistent with how autocompletion behaves in other software (zsh, web browsers, etc.), would it be possible to modify the behavior such that: 1. autocompletion triggers even for single-matches (i.e. "d" shows "dog") 2. autocompletion remains open after uniquely identifying a word (otherwise, typing "ca" shows "cat" and "camel", but if you don't select one of the choices right away and add an "m", the autocomplete now disappears, requiring you to either delete a character or manually type the rest of the word from memory) 3. "tab" / arrow keys can be used to trigger auto-completion, even on an empty input field? Also, unrelated, but do you think it would be in the desired scope of tview to add support for fzf-style fuzzy input matching in the future? That would be pretty amazing to have support for out-of-the box. If so, I can file a separate issue to keep track of it. Thanks for all your work on this great library! **System info** - Arch Linux - Go 1.14.5
kerem closed this issue 2026-03-04 01:04:10 +03:00
Author
Owner

@rivo commented on GitHub (Sep 15, 2020):

I think tview already does what you're asking it to do. Points (1) and (2) are due to this part in your code:

if len(entries) <= 1 {
	entries = nil
}

That's why it's not showing anything when you only enter one character.

Also, you may choose to show all autocomplete items if you just return them. For example:

if len(currentText) == 0 {
	return words
}

I don't have any plans to implement fuzzy matching. But the way the autocomplete functionality is implemented here is that users do the matching themselves. tview doesn't match any input. They're free to use any algorithm they wish.

Let me know if this answers your questions.

<!-- gh-comment-id:692656298 --> @rivo commented on GitHub (Sep 15, 2020): I think `tview` already does what you're asking it to do. Points (1) and (2) are due to this part in your code: ```go if len(entries) <= 1 { entries = nil } ``` That's why it's not showing anything when you only enter one character. Also, you may choose to show all autocomplete items if you just return them. For example: ```go if len(currentText) == 0 { return words } ``` I don't have any plans to implement fuzzy matching. But the way the autocomplete functionality is implemented here is that users do the matching themselves. `tview` doesn't match any input. They're free to use any algorithm they wish. Let me know if this answers your questions.
Author
Owner

@khughitt commented on GitHub (Sep 15, 2020):

@rivo Thanks for taking the time to respond to this issue, and for the suggestions/clarifications!

That helps clear everything up! I should have noticed the len(entries) check.

As you suggested, I was also able to get fuzzy matching working using [github.com/sahilm/fuzzy](https://github.com/sahilm/fuzzy].

<!-- gh-comment-id:692883872 --> @khughitt commented on GitHub (Sep 15, 2020): @rivo Thanks for taking the time to respond to this issue, and for the suggestions/clarifications! That helps clear everything up! I should have noticed the `len(entries)` check. As you suggested, I was also able to get fuzzy matching working using [github.com/sahilm/fuzzy](https://github.com/sahilm/fuzzy].
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#340
No description provided.