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
5 changes: 3 additions & 2 deletions cmd/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ func runCreateCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []
if appNameArg == "" {
if clients.IO.IsTTY() {
defaultName := generateRandomAppName()
cmd.Print(style.Secondary(fmt.Sprintf(" Press Enter to use the generated name: %s", defaultName)), "\n")
name, err := clients.IO.InputPrompt(ctx, "Name your app:", iostreams.InputPromptConfig{})
name, err := clients.IO.InputPrompt(ctx, "Name your app:", iostreams.InputPromptConfig{
Placeholder: defaultName,
})
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/project/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,38 @@ func TestCreateCommand(t *testing.T) {
cm.IO.AssertNotCalled(t, "InputPrompt", mock.Anything, "Name your app:", mock.Anything)
},
},
"name prompt includes placeholder with generated name": {
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
cm.IO.On("IsTTY").Return(true)
cm.IO.On("SelectPrompt", mock.Anything, "Select an app:", mock.Anything, mock.Anything).
Return(
iostreams.SelectPromptResponse{
Prompt: true,
Index: 0,
},
nil,
)
cm.IO.On("SelectPrompt", mock.Anything, "Select a language:", mock.Anything, mock.Anything).
Return(
iostreams.SelectPromptResponse{
Prompt: true,
Index: 0,
},
nil,
)
cm.IO.On("InputPrompt", mock.Anything, "Name your app:", mock.Anything).
Return("my-app", nil)
createClientMock = new(CreateClientMock)
createClientMock.On("Create", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return("", nil)
CreateFunc = createClientMock.Create
},
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
// Verify that InputPrompt was called with a config that has a non-empty Placeholder
cm.IO.AssertCalled(t, "InputPrompt", mock.Anything, "Name your app:", mock.MatchedBy(func(cfg iostreams.InputPromptConfig) bool {
return cfg.Placeholder != ""
}))
},
},
"user accepts default name from prompt": {
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
cm.IO.On("IsTTY").Return(true)
Expand Down
1 change: 1 addition & 0 deletions internal/iostreams/charm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
func buildInputForm(message string, cfg InputPromptConfig, input *string) *huh.Form {
field := huh.NewInput().
Title(message).
Placeholder(cfg.Placeholder).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Using Charm's built-in placeholder option. I chose the Placeholder name to align with huh rather than survey

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍀 praise: Nice call! This ought bring good patterns onward!

Value(input)
if cfg.Required {
field.Validate(huh.ValidateMinLength(1))
Expand Down
9 changes: 9 additions & 0 deletions internal/iostreams/charm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func TestCharmInput(t *testing.T) {
assert.Contains(t, view, "Huh")
})

t.Run("renders placeholder text", func(t *testing.T) {
var input string
f := buildInputForm("Name?", InputPromptConfig{Placeholder: "my-cool-app"}, &input)
f.Update(f.Init())

view := ansi.Strip(f.View())
assert.Contains(t, view, "my-cool-app")
})

t.Run("stores typed value", func(t *testing.T) {
var input string
f := buildInputForm("Name?", InputPromptConfig{}, &input)
Expand Down
8 changes: 5 additions & 3 deletions internal/iostreams/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ var InputQuestionTemplate = fmt.Sprintf(`
{{- if and .Help (not .ShowHelp)}}{{ print .Config.HelpInput }} for help {{- if and .Suggest}}, {{end}}{{end -}}
{{- if and .Suggest }}{{color "cyan"}}{{ print .Config.SuggestInput }} for suggestions{{end -}}
]{{color "reset"}} {{end}}
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`, blue())
{{- if .Default}}{{color "%s"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`, blue(), gray())

// InputPromptConfig holds additional config for an Input prompt
type InputPromptConfig struct {
Required bool // Whether the input must be non-empty
Required bool // Whether the input must be non-empty
Placeholder string // Placeholder text shown when input is empty
Comment on lines 181 to +183
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Using Survey's built-in placeholder option

}

// GetFlags returns all flags for the Input prompt
Expand Down Expand Up @@ -208,6 +209,7 @@ func (io *IOStreams) InputPrompt(ctx context.Context, message string, cfg InputP
var input string
err := survey.AskOne(&survey.Input{
Message: message,
Default: cfg.Placeholder,
}, &input, SurveyOptions(cfg)...)

if err != nil {
Expand Down
Loading