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

Default searcher implemetation #212

Open
migueleliasweb opened this issue Nov 15, 2022 · 5 comments
Open

Default searcher implemetation #212

migueleliasweb opened this issue Nov 15, 2022 · 5 comments

Comments

@migueleliasweb
Copy link

migueleliasweb commented Nov 15, 2022

Hi, firstly thanks for the code. It's great.

I've had a bit of problem using the search feature but after digging a little bit, I've found my way.

Have said that, I reckon it would be awesome if there was some kind of default simple implementation of the searcher someone could use that could fit a lot of differet usecases.

This implementation doesn't add any dependencies since it only uses the std library.

Here's the idea:

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

Let me know if you think this is a good idea so I can make a PR :)

@migueleliasweb
Copy link
Author

migueleliasweb commented Nov 15, 2022

The idea is not make the simpleRegexSearcher the default implementation per say. But instead allow users to easily use it as it would be implemented in the same package as the select package.

So the final code would look something like this:

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

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

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

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

@TrayserCassa
Copy link

In case of speed and also error checking I would not use regex for this, just a strings.Contains() I think is the better way.
But that's just my opinion.

@migueleliasweb
Copy link
Author

Fair point. Yeah we could change it to use that instead.

@migueleliasweb
Copy link
Author

Oh, actually... I think I did try this and it turns out you can't make it so it's case insensitive. That was the reason I used regex. =/

@migueleliasweb
Copy link
Author

Ping @jbowes 🔔 .

Hi, James. Do you still support this repo or is it already considered stable (no further changes)?

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

2 participants