Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slice bounds out of range on select printer #420

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions interactive_select_printer_test.go
Expand Up @@ -12,12 +12,12 @@ import (

func TestInteractiveSelectPrinter_Show(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Down)
mponton marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down