go-prompt icon indicating copy to clipboard operation
go-prompt copied to clipboard

[Feature Request] Method of fetching data once and reusing for selection

Open MajesticFalcon opened this issue 5 years ago • 1 comments

Feature Request

I would like to incorporate this package into my program, but one of the main functionalities I would like to add is the ability to search through our inventory system. Basically, there is a spreadsheet with 2000 item ids in it. As the user goes through our program, there is an option to interact with those IDs. This is where go-prompt would come in. If I read in the IDs inside the "completer" function, the program lags because it must open the excel file, filter through thousands of cells, and display them for each key press. I would like to have a method that sets up the array first. Maybe I am thinking about the applicability wrong.

Purposed: prompt.setOptions := []prompt.Suggest{}

MajesticFalcon avatar Nov 15 '19 21:11 MajesticFalcon

Hi @MajesticFalcon. Thank you for your suggestion.

It sounds following code is enough:

package main

import (
	"fmt"

	prompt "github.com/c-bata/go-prompt"
)

type SpreadSheet struct {
	Rows []prompt.Suggest
}

func (sheet *SpreadSheet) completer(in prompt.Document) []prompt.Suggest {
	return prompt.FilterHasPrefix(sheet.Rows, in.GetWordBeforeCursor(), true)
}

func executor(in string) {
	fmt.Println("Your input: " + in)
}

func main() {
	sheet := &SpreadSheet{
		Rows: []prompt.Suggest{
			{Text: "users", Description: "row 1"},
			{Text: "articles", Description: "row 2"},
			{Text: "comments", Description: "row 3"},
			{Text: "groups", Description: "row 4"},
		},
	}
	p := prompt.New(
		executor,
		sheet.completer,
		prompt.OptionPrefix(">>> "),
	)
	p.Run()
}

What do you think?

c-bata avatar Nov 20 '19 15:11 c-bata