diff --git a/interactive_select_printer.go b/interactive_select_printer.go index 13e25f7b2..ccc89804b 100644 --- a/interactive_select_printer.go +++ b/interactive_select_printer.go @@ -2,6 +2,7 @@ package pterm import ( "fmt" + "math" "sort" "atomicgo.dev/cursor" @@ -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 diff --git a/interactive_select_printer_test.go b/interactive_select_printer_test.go index 35026f8e3..5a4c5a900 100644 --- a/interactive_select_printer_test.go +++ b/interactive_select_printer_test.go @@ -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")