manifoldco/promptui

[feature] Autocomplete prompt

Whoaa512 opened this issue · 3 comments

Hey thanks so much for building this useful library! I'm coming from a JS/TS world and specifically have used a package called prompts in the past and I'd love to see an autocomplete prompt

This would be quite similar to the Select already available but wouldn't require the user to hit a key to get into search mode. Searching would be the default and pressing arrow keys would automatically move the selection cursor.

Happy to make a PR but wanted to check if this would be something you'd accept and what approach would be preferable in implementing it. Like should it be implemented as a wholly new prompt or should it try to re-use the internals of Select as much as possible?

@Whoaa512 this can be done by setting StartInSearchMode: true,

For some reason, that search option doesn't work for me. It makes a "search bar" to show but I' m unable to type anything on it.

Do you have a working example?

I've got a working example that implements a simple "fuzzy search". It's not perfect but will get you going:

package main

import (
	"fmt"
	"regexp"

	"github.com/manifoldco/promptui"
)

func simpleRegexSearcher(items []string) func(input string, index int) bool {
	return func(input string, index int) bool {
		reg, _ := regexp.Compile(fmt.Sprintf("(?i).*%s.*", input))

		return reg.MatchString(items[index])
	}
}

func main() {
	items := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
	prompt := promptui.Select{
		StartInSearchMode: true,
		Label:             "Select Day",
		Items:             items,
		Searcher:          simpleRegexSearcher(items),
	}

	_, result, err := prompt.Run()

	if err != nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}