Skip to content

Commit 2318e16

Browse files
committed
add default placeholders to accessible prompts
1 parent 907f5ec commit 2318e16

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

internal/iostreams/forms.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package iostreams
2121
import (
2222
"context"
2323
"errors"
24+
"fmt"
2425
"slices"
2526

2627
huh "charm.land/huh/v2"
@@ -47,8 +48,12 @@ func newForm(io *IOStreams, field huh.Field) *huh.Form {
4748

4849
// buildInputForm constructs an interactive form for text input prompts.
4950
func buildInputForm(io *IOStreams, message string, cfg InputPromptConfig, input *string) *huh.Form {
51+
title := message
52+
if io != nil && io.config.Accessible && cfg.Placeholder != "" {
53+
title = fmt.Sprintf("%s (default: %s)", message, cfg.Placeholder)
54+
}
5055
field := huh.NewInput().
51-
Title(message).
56+
Title(title).
5257
Prompt(style.Chevron() + " ").
5358
Placeholder(cfg.Placeholder).
5459
Value(input)
@@ -103,8 +108,13 @@ func buildSelectForm(io *IOStreams, msg string, options []string, cfg SelectProm
103108
opts = append(opts, huh.NewOption(key, opt))
104109
}
105110

111+
title := msg
112+
if io != nil && io.config.Accessible && len(options) > 0 {
113+
title = fmt.Sprintf("%s (press Enter for 1)", msg)
114+
}
115+
106116
field := huh.NewSelect[string]().
107-
Title(msg).
117+
Title(title).
108118
Description(cfg.Help).
109119
Options(opts...).
110120
Value(selected)

internal/iostreams/forms_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ func TestFormsAccessible(t *testing.T) {
438438
assert.Contains(t, out.String(), "Enter a number between 1 and 3")
439439
})
440440

441+
t.Run("select form shows default hint in accessible mode", func(t *testing.T) {
442+
var selected string
443+
f := buildSelectForm(io, "Pick one", []string{"Alpha", "Beta"}, SelectPromptConfig{}, &selected)
444+
445+
var out strings.Builder
446+
err := f.WithOutput(&out).WithInput(strings.NewReader("\n")).Run()
447+
448+
assert.NoError(t, err)
449+
assert.Equal(t, "Alpha", selected)
450+
assert.Contains(t, out.String(), `Pick one (press Enter for "Alpha")`)
451+
})
452+
441453
t.Run("confirm form accepts yes/no input", func(t *testing.T) {
442454
var choice bool
443455
f := buildConfirmForm(io, "Continue?", &choice)
@@ -461,6 +473,18 @@ func TestFormsAccessible(t *testing.T) {
461473
assert.Equal(t, "my-app", input)
462474
assert.Contains(t, out.String(), "Name?")
463475
})
476+
477+
t.Run("input form shows default placeholder in accessible mode", func(t *testing.T) {
478+
var input string
479+
f := buildInputForm(io, "Name your app:", InputPromptConfig{Placeholder: "cool-app-123"}, &input)
480+
481+
var out strings.Builder
482+
err := f.WithOutput(&out).WithInput(strings.NewReader("\n")).Run()
483+
484+
assert.NoError(t, err)
485+
assert.Equal(t, "", input)
486+
assert.Contains(t, out.String(), "Name your app: (default: cool-app-123)")
487+
})
464488
}
465489

466490
func TestFormsNoColor(t *testing.T) {

0 commit comments

Comments
 (0)