Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/prompter/accessible_prompter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ func TestAccessiblePrompter(t *testing.T) {
assert.Equal(t, []int{0, 1}, multiSelectValue)
})

t.Run("MultiSelect - default values are respected by being pre-selected", func(t *testing.T) {
console := newTestVirtualTerminal(t)
p := newTestAccessiblePrompter(t, console)

go func() {
// Wait for prompt to appear
_, err := console.ExpectString("Select a number")
require.NoError(t, err)

// Don't select anything because the default should be selected.

// This confirms selections
_, err = console.SendLine("0")
require.NoError(t, err)
}()

multiSelectValue, err := p.MultiSelect("Select a number", []string{"2"}, []string{"1", "2", "3"})
require.NoError(t, err)
assert.Equal(t, []int{1}, multiSelectValue)
})

t.Run("Input", func(t *testing.T) {
console := newTestVirtualTerminal(t)
p := newTestAccessiblePrompter(t, console)
Expand Down
9 changes: 9 additions & 0 deletions internal/prompter/prompter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prompter

import (
"fmt"
"slices"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -100,6 +101,14 @@ func (p *accessiblePrompter) MultiSelect(prompt string, defaults []string, optio
var result []int
formOptions := make([]huh.Option[int], len(options))
for i, o := range options {
// If this option is in the defaults slice,
// let's add it's index to the result slice and huh
// will treat it as a default selection.
// TODO: does an invalid default value constitute a panic?
if slices.Contains(defaults, o) {
result = append(result, i)
}

formOptions[i] = huh.NewOption(o, i)
}

Expand Down
Loading