AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 350 forks source link

Select index #225

Closed AlecAivazis closed 5 years ago

AlecAivazis commented 5 years ago

This PR tweaks the values written by Select to be either the option's index or the value, based on the type of the target field. I also had to change the type of the Default field in both prompts to interface{} to accommodate a user who is thinking in terms of either indices or values.

The old implementation of MultiSelect had an issue when the string values were not unique. In order to address that, I changed the internal tracking of the selected options to a map keys on index. This meant that all of the paginating logic had to hold onto some information that tracked the original index. In order to make this all work nicely, I changed the signature of the Filter function passed around to be func (filterValue string, optValue string, optIndex int) bool. This saves the user from writing the boilerplate that loops over their lists and applies the same function. It also has the extra benefit of allowing us to track which indices passed without forcing the user to provide that information.

fixes #105