fix(a11y prompter): select prompt default value is readable
This commit is contained in:
parent
ab4cfb84d2
commit
cab906151f
2 changed files with 32 additions and 5 deletions
|
|
@ -78,6 +78,29 @@ func TestAccessiblePrompter(t *testing.T) {
|
|||
assert.Equal(t, expectedIndex, selectValue)
|
||||
})
|
||||
|
||||
t.Run("Select - default value is in prompt and in readable format", func(t *testing.T) {
|
||||
console := newTestVirtualTerminal(t)
|
||||
p := newTestAccessiblePrompter(t, console)
|
||||
dummyDefaultValue := "12345abcdefg"
|
||||
options := []string{"1", "2", dummyDefaultValue}
|
||||
|
||||
go func() {
|
||||
// Wait for prompt to appear
|
||||
_, err := console.ExpectString("Select a number (default: 12345abcdefg)")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Just press enter to accept the default
|
||||
_, err = console.SendLine("")
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
|
||||
selectValue, err := p.Select("Select a number", dummyDefaultValue, options)
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedIndex := slices.Index(options, dummyDefaultValue)
|
||||
assert.Equal(t, expectedIndex, selectValue)
|
||||
})
|
||||
|
||||
t.Run("MultiSelect", func(t *testing.T) {
|
||||
console := newTestVirtualTerminal(t)
|
||||
p := newTestAccessiblePrompter(t, console)
|
||||
|
|
@ -171,7 +194,7 @@ func TestAccessiblePrompter(t *testing.T) {
|
|||
|
||||
go func() {
|
||||
// Wait for prompt to appear
|
||||
_, err := console.ExpectString("Enter some characters (default: 12345abcdefg):")
|
||||
_, err := console.ExpectString("Enter some characters (default: 12345abcdefg)")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Enter nothing
|
||||
|
|
|
|||
|
|
@ -79,12 +79,15 @@ func (p *accessiblePrompter) newForm(groups ...*huh.Group) *huh.Form {
|
|||
|
||||
// addDefaultsToPrompt adds default values to the prompt string.
|
||||
func (p *accessiblePrompter) addDefaultsToPrompt(prompt string, defaultValues []string) string {
|
||||
// We don't show empty default values in the prompt.
|
||||
defaultValues = slices.DeleteFunc(defaultValues, func(s string) bool {
|
||||
return s == ""
|
||||
})
|
||||
|
||||
if len(defaultValues) == 1 {
|
||||
prompt = fmt.Sprintf("%s (default: %s):", prompt, defaultValues[0])
|
||||
prompt = fmt.Sprintf("%s (default: %s)", prompt, defaultValues[0])
|
||||
} else if len(defaultValues) > 1 {
|
||||
prompt = fmt.Sprintf("%s (defaults: %s):", prompt, strings.Join(defaultValues, ", "))
|
||||
} else {
|
||||
prompt = fmt.Sprintf("%s:", prompt)
|
||||
prompt = fmt.Sprintf("%s (defaults: %s)", prompt, strings.Join(defaultValues, ", "))
|
||||
}
|
||||
|
||||
return prompt
|
||||
|
|
@ -93,6 +96,7 @@ func (p *accessiblePrompter) addDefaultsToPrompt(prompt string, defaultValues []
|
|||
func (p *accessiblePrompter) Select(prompt, defaultValue string, options []string) (int, error) {
|
||||
var result int
|
||||
formOptions := []huh.Option[int]{}
|
||||
prompt = p.addDefaultsToPrompt(prompt, []string{defaultValue})
|
||||
for i, o := range options {
|
||||
// If this option is the default value, assign its index
|
||||
// to the result variable. huh will treat it as a default selection.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue