Skip to content

Commit

Permalink
Merge pull request #420 from mponton/issue-419-out_of_range_select_pr…
Browse files Browse the repository at this point in the history
…inter

Closes #419
Fixes #419
  • Loading branch information
MarvinJWendt committed Nov 22, 2022
2 parents 2014856 + 8ff8b73 commit 9784769
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions interactive_select_printer.go
Expand Up @@ -2,6 +2,7 @@ package pterm

import (
"fmt"
"math"
"sort"

"atomicgo.dev/cursor"
Expand Down Expand Up @@ -106,9 +107,9 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {
for i, option := range p.Options {
if option == p.DefaultOption {
p.selectedOption = i
if i > 0 {
p.displayedOptionsStart = i - 1
p.displayedOptionsEnd = i - 1 + maxHeight
if i > 0 && len(p.Options) > maxHeight {
p.displayedOptionsEnd = int(math.Min(float64(i-1+maxHeight), float64(len(p.Options))))
p.displayedOptionsStart = p.displayedOptionsEnd - maxHeight
} else {
p.displayedOptionsStart = 0
p.displayedOptionsEnd = maxHeight
Expand Down
10 changes: 10 additions & 0 deletions interactive_select_printer_test.go
Expand Up @@ -20,6 +20,16 @@ func TestInteractiveSelectPrinter_Show(t *testing.T) {
testza.AssertEqual(t, "d", result)
}

func TestInteractiveSelectPrinter_Show_MaxHeightSlidingWindow(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveSelect.WithOptions([]string{"a", "b", "c", "d", "e", "f"}).WithDefaultOption("e").Show()
testza.AssertEqual(t, "c", result)
}

func TestInteractiveSelectPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
Expand Down

0 comments on commit 9784769

Please sign in to comment.