Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Autocomplete prompt #196

Open
Whoaa512 opened this issue Dec 1, 2021 · 3 comments
Open

[feature] Autocomplete prompt #196

Whoaa512 opened this issue Dec 1, 2021 · 3 comments

Comments

@Whoaa512
Copy link

Whoaa512 commented Dec 1, 2021

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?

@securisec
Copy link

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

@migueleliasweb
Copy link

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?

@migueleliasweb
Copy link

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants