manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.07k stars 336 forks source link

bug: `StartInSearchMode` does overwrite `RunCursorAt` #105

Open buehler opened 5 years ago

buehler commented 5 years ago

Hey there

I found a little bug when using your select prompt and I don't know if it's intended: When one does calculate the page and position of a list of elements and runs the select at a specific cursor, it works like a charm:

elements := []string{"a","b","c","d","e","f","g","h","i"}
// search for actual index that we want to use
actualIndex := 7
page := actualIndex / 5 // will be 1
pos := actualIndex % 5 // will be 2

prompt := promptui.Select{
  Label: "select",
  Items: elements,
  Searcher: func(input string, index int) bool {
    if found := strings.Index(contexts[index], input); found != -1 {
      return true
    }
    return false
  }
}

_, _, _ := prompt.RunCursorAt(position, page)
// *snip

Now, page "two" with the third element will be selected, as expected.

But when I run the same code with StartInSearchMode set to true:

prompt := promptui.Select{
  Label: "select",
  Items: elements,
  StartInSearchMode: true,
  Searcher: func(input string, index int) bool {
    if found := strings.Index(contexts[index], input); found != -1 {
      return true
    }
    return false
  }
}

_, _, _ := prompt.RunCursorAt(position, page)
// *snip

The searcher gets activated (as intended), but the list beneath it is set to page 0 and cursor position 0. As long as no first search is executed, I'd expect the list to be set to the cursor position that is given at the start.

Or does this break some functionality ?

Thanks for the reply, Cheers