From c91ee03fcb4273a6ce8b8a3dc0d0db6763a10178 Mon Sep 17 00:00:00 2001 From: Marco Ponton Date: Fri, 18 Nov 2022 16:18:17 -0500 Subject: [PATCH 1/2] fix: slice bounds out of range on select printer Closes #419 --- interactive_select_printer.go | 7 ++++--- interactive_select_printer_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) 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..b44966012 100644 --- a/interactive_select_printer_test.go +++ b/interactive_select_printer_test.go @@ -12,12 +12,12 @@ import ( func TestInteractiveSelectPrinter_Show(t *testing.T) { go func() { - keyboard.SimulateKeyPress(keys.Down) - keyboard.SimulateKeyPress(keys.Down) + keyboard.SimulateKeyPress(keys.Up) + keyboard.SimulateKeyPress(keys.Up) keyboard.SimulateKeyPress(keys.Enter) }() - result, _ := pterm.DefaultInteractiveSelect.WithOptions([]string{"a", "b", "c", "d", "e"}).WithDefaultOption("b").Show() - testza.AssertEqual(t, "d", result) + 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) { From 8ff8b7347294d218e638e48f4464f2789d781b93 Mon Sep 17 00:00:00 2001 From: Marco Ponton Date: Sun, 20 Nov 2022 11:53:29 -0500 Subject: [PATCH 2/2] fix: revert original test & add new test --- interactive_select_printer_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/interactive_select_printer_test.go b/interactive_select_printer_test.go index b44966012..5a4c5a900 100644 --- a/interactive_select_printer_test.go +++ b/interactive_select_printer_test.go @@ -11,6 +11,16 @@ import ( ) func TestInteractiveSelectPrinter_Show(t *testing.T) { + go func() { + keyboard.SimulateKeyPress(keys.Down) + keyboard.SimulateKeyPress(keys.Down) + keyboard.SimulateKeyPress(keys.Enter) + }() + result, _ := pterm.DefaultInteractiveSelect.WithOptions([]string{"a", "b", "c", "d", "e"}).WithDefaultOption("b").Show() + testza.AssertEqual(t, "d", result) +} + +func TestInteractiveSelectPrinter_Show_MaxHeightSlidingWindow(t *testing.T) { go func() { keyboard.SimulateKeyPress(keys.Up) keyboard.SimulateKeyPress(keys.Up)